-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial board rendering with borders, buildings and capacities, refs #61
- Loading branch information
Showing
5 changed files
with
256 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |