From 7f46f2560ed3d5973f6f6056683432557ab1ded9 Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 20 Feb 2022 16:07:20 -0500 Subject: [PATCH] add board tests --- board.go | 13 +++-- board_test.go | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 + main.go | 2 +- tile.go | 2 +- 5 files changed, 165 insertions(+), 7 deletions(-) create mode 100644 board_test.go create mode 100644 go.mod diff --git a/board.go b/board.go index 3c0d5f3..dbf2608 100644 --- a/board.go +++ b/board.go @@ -10,14 +10,17 @@ type Board struct { //NewBoard creates a new board instance func NewBoard(size int) *Board { + if size < 4 || size%2 != 0 { + return nil + } 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 + } else { + colour = Blue } for j := 0; j < size; j++ { b[i][j] = &Tile{i, j, true, nil, colour} @@ -76,11 +79,11 @@ 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) { +//AddTerrain puts a river tile at specified location +func (b *Board) AddTerrain(x, y, t int) (bool, error) { if !b.validatePoint(x, y) { return false, errors.New("River invalid location") } - b.board[x][y].AddTerrain() + b.board[x][y].AddTerrain(t) return true, nil } diff --git a/board_test.go b/board_test.go new file mode 100644 index 0000000..381cb7a --- /dev/null +++ b/board_test.go @@ -0,0 +1,152 @@ +package main + +import "testing" + +func TestNewBoard(t *testing.T) { + b := NewBoard(2) + if b != nil { + t.Error("Able to create too small board") + } + for size := 4; size < 9; size = size + 2 { + b = NewBoard(size) + if len(b.board[0]) != size { + t.Errorf("board col is not size specified: %v != %v", 8, len(b.board[0])) + } + if len(b.board) != size { + t.Errorf("board row is not size specified: %v != %v", 8, len(b.board[0])) + } + rTiles := 0 + bTiles := 0 + for i := 0; i < size; i++ { + for j := 0; j < size; j++ { + ti := b.board[i][j] + if ti == nil { + t.Errorf("no tile at pos %v,%v", i, j) + } + if ti.entity != nil { + t.Errorf("new board but %v,%v has a piece", i, j) + } + if !ti.passable { + t.Errorf("New board but %v %v is inpassible", i, j) + } + if ti.colour == Red { + rTiles++ + } else if ti.colour == Blue { + bTiles++ + } else { + t.Errorf("Tile %v,%v has invalid color %v", i, j, ti.colour) + } + } + } + if rTiles != bTiles { + t.Errorf("Inequal number of coloured tiles: red tiles %v x blue tiles %v", rTiles, bTiles) + } + } + +} + +func TestGetPiece(t *testing.T) { + b := NewBoard(4) + p := NewPiece(2, Red) + b.board[0][0].entity = p + np, err := b.GetPiece(9, 9) + if err == nil { + t.Error("able to get piece from invalid location") + } + np, err = b.GetPiece(0, 0) + if err != nil { + t.Fatalf("GetPiece failed when it shouldn't: %v", err) + } + if np == nil { + t.Errorf("Failed to get piece") + } + if np != p { + t.Errorf("Got different piece from one placed") + } + +} + +func TestPlace(t *testing.T) { + b := NewBoard(4) + p := NewPiece(2, Red) + res, err := b.Place(8, 9, p) + if err == nil { + t.Errorf("able to place in invalid location") + } + res, err = b.Place(0, 0, p) + if err != nil { + t.Fatal(err) + } + if !res { + t.Errorf("Failed to place piece") + } + p2, err := b.GetPiece(0, 0) + if err != nil { + t.Fatal(err) + } + if p2 == nil { + t.Errorf("failed to get placed piece") + } + if p2 != p { + t.Errorf("got different piece from one placed") + } +} + +func TestRemove(t *testing.T) { + b := NewBoard(4) + p := NewPiece(2, Red) + + res, err := b.Place(0, 0, p) + if err != nil { + t.Fatal(err) + } + if !res { + t.Errorf("Failed to place piece") + } + p2, err := b.GetPiece(0, 0) + if err != nil { + t.Fatal(err) + } + if p2 == nil { + t.Errorf("failed to get placed piece") + } + if p2 != p { + t.Errorf("got different piece from one placed") + } + err = b.Remove(-1, 9) + if err == nil { + t.Errorf("able to remove from invalid location") + } + err = b.Remove(0, 0) + if err != nil { + t.Fatal(err) + } + if b.board[0][0].entity != nil { + t.Fatalf("Ran remove but piece remained") + } +} + +func TestGetColor(t *testing.T) { + b := NewBoard(4) + if b.GetColor(0, 0) != Red { + t.Errorf("got wrong color for tile: %v", b.GetColor(0, 0)) + } +} + +func TestAddTerrain(t *testing.T) { + b := NewBoard(4) + res, err := b.AddTerrain(5, 6, 1) + if err == nil { + t.Errorf("added terrain to invalid location") + } + res, err = b.AddTerrain(0, 0, 1) + if err != nil { + t.Fatal(err) + } + if !res { + t.Errorf("unable to add terrain") + } + if b.board[0][0].passable { + t.Errorf("tile still passable even when there's terrain") + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3064264 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.saintnet.tech/freego + +go 1.17 diff --git a/main.go b/main.go index cb52098..0105d38 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,7 @@ func addpiece(game *Game, rank int, c Colour, x int, y int) { } func addriver(game *Game, x int, y int) { - res, err := game.board.AddRiver(x, y) + res, err := game.board.AddTerrain(x, y, 1) if err != nil { panic(err) } diff --git a/tile.go b/tile.go index 654fad8..0e963b2 100644 --- a/tile.go +++ b/tile.go @@ -94,7 +94,7 @@ func (t *Tile) Y() int { } //AddTerrain adds specified terrain to position -func (t *Tile) AddTerrain() bool { +func (t *Tile) AddTerrain(ter int) bool { if t.entity != nil { return false }