1
0
mirror of https://github.com/maaslalani/gambit.git synced 2025-01-07 23:01:38 +02:00
gambit/board.go

51 lines
1.1 KiB
Go
Raw Normal View History

2021-11-27 05:21:39 +02:00
package main
2021-11-27 06:41:19 +02:00
const (
dimensions = 8
firstRow = 0
firstCol = 0
lastRow = dimensions - 1
lastCol = dimensions - 1
)
const (
header = "\n ┌───┬───┬───┬───┬───┬───┬───┬───┐ \n"
middle = "\n ├───┼───┼───┼───┼───┼───┼───┼───┤ \n"
footer = "\n └───┴───┴───┴───┴───┴───┴───┴───┘ \n"
divider = " │ "
)
2021-11-27 06:16:58 +02:00
2021-11-27 05:21:39 +02:00
type Board struct {
Players []Player
2021-11-27 06:16:58 +02:00
Grid [dimensions][dimensions]string
2021-11-27 05:21:39 +02:00
}
2021-11-27 05:52:33 +02:00
func (b *Board) Draw() {
2021-11-27 05:21:39 +02:00
for _, player := range b.Players {
for _, piece := range player.Pieces {
2021-11-27 05:52:33 +02:00
b.Grid[piece.Position[0]-1][piece.Position[1]-1] = piece.String()
}
}
}
func (b *Board) String() string {
2021-11-27 06:41:19 +02:00
var s = header
for row := firstRow; row < dimensions; row++ {
for col := firstCol; col < dimensions; col++ {
s += divider
2021-11-27 05:52:33 +02:00
if b.Grid[row][col] == "" {
s += " "
} else {
s += b.Grid[row][col]
}
2021-11-27 06:16:58 +02:00
if col == lastCol {
2021-11-27 06:41:19 +02:00
s += divider
2021-11-27 06:12:32 +02:00
}
}
2021-11-27 06:41:19 +02:00
if row != lastRow {
s += middle
2021-11-27 05:21:39 +02:00
}
}
2021-11-27 06:41:19 +02:00
return s + footer
2021-11-27 05:21:39 +02:00
}