Skip to content

Commit

Permalink
Initial board rendering with borders, buildings and capacities, refs #61
Browse files Browse the repository at this point in the history
  • Loading branch information
beefsack committed Aug 3, 2015
1 parent 14c16c8 commit 78b74d1
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 6 deletions.
22 changes: 22 additions & 0 deletions game/agricola_2p/building.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package agricola_2p

import "encoding/gob"

func init() {
gob.Register(Cottage{})
}

type Building interface {
Capacity() int
String() string
}

type Cottage struct{}

func (c Cottage) Capacity() int {
return 1
}

func (c Cottage) String() string {
return "Cottage"
}
26 changes: 20 additions & 6 deletions game/agricola_2p/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@ import (
"github.com/Miniand/brdg.me/game/log"
)

const (
ResourceWood = iota
ResourceStone
ResourceReed
ResourceBorder
ResourceWorker
)

type Game struct {
Players []string
Log *log.Log
Players []string
PBoards [2]*PBoard
StartPlayer int
Log *log.Log
}

func (g *Game) Commands() []command.Command {
Expand All @@ -33,16 +43,16 @@ func (g *Game) Decode(data []byte) error {
return helper.Decode(g, data)
}

func (g *Game) RenderForPlayer(player string) (string, error) {
return "", nil
}

func (g *Game) Start(players []string) error {
if len(players) != 2 {
return errors.New("only for 2 players")
}
g.Players = players
g.Log = log.New()
g.PBoards = [2]*PBoard{}
for p := range players {
g.PBoards[p] = NewPBoard()
}
return nil
}

Expand All @@ -65,3 +75,7 @@ func (g *Game) WhoseTurn() []string {
func (g *Game) GameLog() *log.Log {
return g.Log
}

func (g *Game) PlayerNum(player string) (int, bool) {
return helper.StringInStrings(player, g.Players)
}
42 changes: 42 additions & 0 deletions game/agricola_2p/pboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package agricola_2p

import "github.com/Miniand/brdg.me/game/cost"

type PBoard struct {
Resources cost.Cost
Tiles Tiles
XStart, XEnd int
}

func NewPBoard() *PBoard {
return &PBoard{
Resources: cost.Cost{
ResourceBorder: 9,
},
Tiles: Tiles{
Loc{0, 2}: {
Building: Cottage{},
},
// Testing
Loc{0, 0}: {
Borders: Up | Left,
},
Loc{0, 1}: {
Borders: Left,
},
Loc{1, 1}: {
Borders: Up,
},
Loc{2, 1}: {
Borders: Left,
},
Loc{1, 2}: {
Borders: Up,
},
Loc{1, 0}: {
Borders: Left,
},
},
XEnd: 1,
}
}
94 changes: 94 additions & 0 deletions game/agricola_2p/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package agricola_2p

import (
"bytes"
"errors"
"fmt"
"strconv"
"strings"

"github.com/Miniand/brdg.me/render"
)

const (
TileWidth = 12
)

var EmptyTileLine = strings.Repeat(" ", TileWidth)

var Corner = `{{bg "green"}} {{_bg}}`

func (g *Game) RenderForPlayer(player string) (string, error) {
pNum, ok := g.PlayerNum(player)
if !ok {
return "", errors.New("could not find player")
}
return g.PBoards[pNum].Render(), nil
}

func (pb *PBoard) Render() string {
lines := []string{}
for y := 0; y < 4; y++ {
for tLine := 0; tLine < 6; tLine++ {
if y == 3 && tLine > 0 {
// Bottom border, we don't care about the tile.
break
}
row := bytes.Buffer{}
for x := pb.XStart; x <= pb.XEnd+1; x++ {
l := Loc{x, y}
if tLine == 0 {
// Top border
row.WriteString(Corner)
if x <= pb.XEnd {
borderCol := render.Gray
if pb.Tiles.Border(l, Up) {
borderCol = render.Yellow
}
row.WriteString(fmt.Sprintf(
`{{bg "%s"}}%s{{_bg}}`,
borderCol,
EmptyTileLine,
))
}
} else {
// Tile Body
borderCol := render.Gray
if pb.Tiles.Border(l, Left) {
borderCol = render.Yellow
}
row.WriteString(fmt.Sprintf(`{{bg "%s"}} {{_bg}}`, borderCol))
if x <= pb.XEnd {
t := pb.Tiles.At(l)
tileCol := render.Green
text := EmptyTileLine
if t.Building != nil {
tileCol = render.Black
if tLine == 2 {
text = render.Centre(
render.Bold(t.Building.String()),
TileWidth,
)
}
capacity := t.Building.Capacity()
if tLine == 5 && capacity > 0 {
text = render.Right(render.Markup(
strconv.Itoa(capacity),
render.Red,
true,
), TileWidth)
}
}
row.WriteString(fmt.Sprintf(
`{{bg "%s"}}%s{{_bg}}`,
tileCol,
text,
))
}
}
}
lines = append(lines, row.String())
}
}
return strings.Join(lines, "\n")
}
78 changes: 78 additions & 0 deletions game/agricola_2p/tile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package agricola_2p

const (
Up = 1 << iota
Right
Down
Left
)

type Loc struct {
X, Y int
}

func (l Loc) Move(dir int) Loc {
newX, newY := Move(l.X, l.Y, dir)
return Loc{
X: newX,
Y: newY,
}
}

var OppDir = map[int]int{
Up: Down,
Down: Up,
Left: Right,
Right: Left,
}

func Move(x, y, dir int) (newX, newY int) {
newX, newY = x, y
if dir&Up != 0 {
newY--
}
if dir&Down != 0 {
newY++
}
if dir&Left != 0 {
newX--
}
if dir&Right != 0 {
newX++
}
return
}

type Tile struct {
Borders int
Building Building
Trough bool
}

type Tiles map[Loc]*Tile

func (t Tiles) At(l Loc) *Tile {
if t[l] == nil {
return &Tile{}
}
return t[l]
}

func (t Tiles) Neighbour(l Loc, dir int) *Tile {
return t.At(l.Move(dir))
}

func (t Tiles) Border(l Loc, dir int) bool {
til := t.At(l)
nei := t.Neighbour(l, dir)
if til.Building != nil || nei.Building != nil {
return true
}
switch dir {
case Up, Left:
return til.Borders&dir != 0
case Down, Right:
return nei.Borders&OppDir[dir] != 0
}
return false
}

0 comments on commit 78b74d1

Please sign in to comment.