87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
|
package main
|
||
|
|
||
|
import "errors"
|
||
|
|
||
|
//Board represents main game board
|
||
|
type Board struct {
|
||
|
board [][]*Tile
|
||
|
size int
|
||
|
}
|
||
|
|
||
|
//NewBoard creates a new board instance
|
||
|
func NewBoard(size int) *Board {
|
||
|
b := make([][]*Tile, size)
|
||
|
var colour Colour
|
||
|
for i := 0; i < size; i++ {
|
||
|
b[i] = make([]*Tile, size)
|
||
|
if i < size/2 {
|
||
|
colour = Blue
|
||
|
} else {
|
||
|
colour = Red
|
||
|
}
|
||
|
for j := 0; j < size; j++ {
|
||
|
b[i][j] = &Tile{i, j, true, nil, colour}
|
||
|
}
|
||
|
}
|
||
|
return &Board{
|
||
|
board: b,
|
||
|
size: size,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (b *Board) validatePoint(x, y int) bool {
|
||
|
if x < 0 || x >= len(b.board) {
|
||
|
return false
|
||
|
}
|
||
|
if y < 0 || y >= len(b.board) {
|
||
|
return false
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
//GetPiece returns the piece at a given location
|
||
|
func (b *Board) GetPiece(x, y int) (*Piece, error) {
|
||
|
if !b.validatePoint(x, y) {
|
||
|
return nil, errors.New("GetPiece invalid location")
|
||
|
}
|
||
|
return b.board[x][y].Piece(), nil
|
||
|
}
|
||
|
|
||
|
//Place a piece on the board; returns false if a piece is already there
|
||
|
func (b *Board) Place(x, y int, p *Piece) (bool, error) {
|
||
|
if !b.validatePoint(x, y) {
|
||
|
return false, errors.New("Place invalid location")
|
||
|
}
|
||
|
if b.board[x][y].Piece() != nil {
|
||
|
return false, nil
|
||
|
}
|
||
|
err := b.board[x][y].Place(p)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
return true, nil
|
||
|
}
|
||
|
|
||
|
//Remove a piece from a tile
|
||
|
func (b *Board) Remove(x, y int) error {
|
||
|
if !b.validatePoint(x, y) {
|
||
|
return errors.New("Remove invalid location")
|
||
|
}
|
||
|
b.board[x][y].Remove()
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
//GetColor returns color of tile
|
||
|
func (b *Board) GetColor(x, y int) Colour {
|
||
|
return b.board[x][y].Colour()
|
||
|
}
|
||
|
|
||
|
//AddRiver puts a river tile at specified location
|
||
|
func (b *Board) AddRiver(x, y int) (bool, error) {
|
||
|
if !b.validatePoint(x, y) {
|
||
|
return false, errors.New("River invalid location")
|
||
|
}
|
||
|
b.board[x][y].AddTerrain()
|
||
|
return true, nil
|
||
|
}
|