52 lines
1011 B
Go
52 lines
1011 B
Go
package structs
|
|
|
|
// Board is a generic 2D grid for turn-based games.
|
|
// Cell data is stored as strings, but can represent anything (piece, move, state).
|
|
type Board struct {
|
|
Rows int `json:"rows"`
|
|
Cols int `json:"cols"`
|
|
Grid [][]string `json:"grid"`
|
|
}
|
|
|
|
// NewBoard creates a grid of empty strings.
|
|
func NewBoard(rows, cols int) *Board {
|
|
b := &Board{
|
|
Rows: rows,
|
|
Cols: cols,
|
|
Grid: make([][]string, rows),
|
|
}
|
|
|
|
for r := 0; r < rows; r++ {
|
|
b.Grid[r] = make([]string, cols)
|
|
}
|
|
|
|
return b
|
|
}
|
|
|
|
func (b *Board) InBounds(row, col int) bool {
|
|
return row >= 0 && row < b.Rows && col >= 0 && col < b.Cols
|
|
}
|
|
|
|
func (b *Board) Get(row, col int) string {
|
|
return b.Grid[row][col]
|
|
}
|
|
|
|
func (b *Board) Set(row, col int, value string) {
|
|
b.Grid[row][col] = value
|
|
}
|
|
|
|
func (b *Board) IsEmpty(row, col int) bool {
|
|
return b.Grid[row][col] == ""
|
|
}
|
|
|
|
func (b *Board) Full() bool {
|
|
for r := 0; r < b.Rows; r++ {
|
|
for c := 0; c < b.Cols; c++ {
|
|
if b.Grid[r][c] == "" {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|