more tests
This commit is contained in:
parent
7f46f2560e
commit
719b7626c9
4
board.go
4
board.go
@ -18,9 +18,9 @@ func NewBoard(size int) *Board {
|
||||
for i := 0; i < size; i++ {
|
||||
b[i] = make([]*Tile, size)
|
||||
if i < size/2 {
|
||||
colour = Red
|
||||
} else {
|
||||
colour = Blue
|
||||
} else {
|
||||
colour = Red
|
||||
}
|
||||
for j := 0; j < size; j++ {
|
||||
b[i][j] = &Tile{i, j, true, nil, colour}
|
||||
|
@ -128,7 +128,7 @@ func TestRemove(t *testing.T) {
|
||||
|
||||
func TestGetColor(t *testing.T) {
|
||||
b := NewBoard(4)
|
||||
if b.GetColor(0, 0) != Red {
|
||||
if b.GetColor(0, 0) != Blue {
|
||||
t.Errorf("got wrong color for tile: %v", b.GetColor(0, 0))
|
||||
}
|
||||
}
|
||||
|
26
command.go
26
command.go
@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
var cmdRegxp = regexp.MustCompile("([a-zA-Z])([1-9])(x|-)([a-zA-Z])([1-9])")
|
||||
var ranks = "ABCDEFHI"
|
||||
var ranks = "ABCDEFHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
//RawCommand is a game command, converted from algebraic notation
|
||||
type RawCommand struct {
|
||||
@ -22,21 +22,31 @@ type RawCommand struct {
|
||||
|
||||
//NewRawCommand creates a RawCommand struct from a algebraic notation string
|
||||
func NewRawCommand(cmd string) (*RawCommand, error) {
|
||||
res := cmdRegxp.FindAllString(cmd, -1)
|
||||
if res == nil {
|
||||
rawRes := cmdRegxp.FindAllStringSubmatch(cmd, -1)
|
||||
if rawRes == nil {
|
||||
return nil, errors.New("error creating command from string")
|
||||
}
|
||||
sx := strings.Index(ranks, res[0])
|
||||
dx := strings.Index(ranks, res[3])
|
||||
sy, err := strconv.Atoi(res[1])
|
||||
res := rawRes[0]
|
||||
if len(res) != 6 {
|
||||
return nil, fmt.Errorf("expected more fields from command string 5!=%v, %v", len(res), res)
|
||||
}
|
||||
sx := strings.Index(ranks, strings.ToUpper(res[1]))
|
||||
if sx == -1 {
|
||||
return nil, fmt.Errorf("bad rank value: %v", res[1])
|
||||
}
|
||||
dx := strings.Index(ranks, strings.ToUpper(res[4]))
|
||||
if dx == -1 {
|
||||
return nil, fmt.Errorf("bad rank value: %v", strings.ToUpper(res[4]))
|
||||
}
|
||||
sy, err := strconv.Atoi(res[2])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dy, err := strconv.Atoi(res[4])
|
||||
dy, err := strconv.Atoi(res[5])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &RawCommand{sx, sy, dx, dy, res[2]}, nil
|
||||
return &RawCommand{sx, sy, dx, dy, res[3]}, nil
|
||||
}
|
||||
|
||||
func (c *RawCommand) String() string {
|
||||
|
49
command_test.go
Normal file
49
command_test.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewRawCommand(t *testing.T) {
|
||||
var tests = []struct {
|
||||
input string
|
||||
res bool
|
||||
sx int
|
||||
sy int
|
||||
dx int
|
||||
dy int
|
||||
act string
|
||||
}{
|
||||
{"A1xC3", true, 0, 1, 2, 3, "x"},
|
||||
{"d7-d1", true, 3, 7, 3, 1, "-"},
|
||||
{"AA-k3", false, 0, 0, 0, 0, ""},
|
||||
{"a1/b4", false, 0, 0, 0, 0, ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tname := fmt.Sprintf("input: %v", tt.input)
|
||||
t.Run(tname, func(t *testing.T) {
|
||||
res, err := NewRawCommand(tt.input)
|
||||
if tt.res && err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tt.res {
|
||||
if tt.sx != res.srcX {
|
||||
t.Errorf("bad sourceX: %v != %v", tt.sx, res.srcX)
|
||||
}
|
||||
if tt.sy != res.srcY {
|
||||
t.Errorf("bad sourceY: %v != %v", tt.sy, res.srcY)
|
||||
}
|
||||
if tt.dx != res.dstX {
|
||||
t.Errorf("bad destX: %v != %v", tt.dx, res.dstX)
|
||||
}
|
||||
if tt.dy != res.dstY {
|
||||
t.Errorf("bad destY: %v != %v", tt.dy, res.dstY)
|
||||
}
|
||||
if tt.act != res.act {
|
||||
t.Errorf("bad action: %v != %v", tt.act, res.act)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
8
game.go
8
game.go
@ -130,8 +130,12 @@ func (g *Game) SetupPiece(x, y int, p *Piece) (bool, error) {
|
||||
}
|
||||
|
||||
//Start start the game
|
||||
func (g *Game) Start() {
|
||||
g.state = gameTurnRed
|
||||
func (g *Game) Start() bool {
|
||||
if g.state == gameSetup {
|
||||
g.state = gameTurnRed
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (g *Game) move(x, y, s, t int) (bool, error) {
|
||||
|
77
game_test.go
Normal file
77
game_test.go
Normal file
@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func dummyMiniGame() *Game {
|
||||
g := &Game{
|
||||
board: NewBoard(4),
|
||||
state: gameSetup,
|
||||
}
|
||||
//Setup terrain
|
||||
g.board.AddTerrain(1, 1, 1)
|
||||
g.board.AddTerrain(2, 2, 1)
|
||||
//Setup blue (5 pieces)
|
||||
g.SetupPiece(0, 0, NewPiece(0, Blue))
|
||||
g.SetupPiece(0, 1, NewPiece(1, Blue))
|
||||
g.SetupPiece(0, 2, NewPiece(2, Blue))
|
||||
g.SetupPiece(0, 3, NewPiece(3, Blue))
|
||||
g.SetupPiece(1, 3, NewPiece(4, Blue))
|
||||
//Setup red (5 pieces)
|
||||
g.SetupPiece(3, 0, NewPiece(0, Red))
|
||||
g.SetupPiece(3, 1, NewPiece(1, Red))
|
||||
g.SetupPiece(3, 2, NewPiece(2, Red))
|
||||
g.SetupPiece(3, 3, NewPiece(3, Red))
|
||||
g.SetupPiece(2, 1, NewPiece(4, Red))
|
||||
|
||||
return g
|
||||
}
|
||||
|
||||
func TestNewGame(t *testing.T) {
|
||||
g := NewGame()
|
||||
if g == nil {
|
||||
t.Fatal("couldn't create game")
|
||||
}
|
||||
if g.state != gameLobby {
|
||||
t.Error("Game created with weird state")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupPiece(t *testing.T) {
|
||||
g := NewGame()
|
||||
p1 := NewPiece(0, Blue)
|
||||
p2 := NewPiece(0, Red)
|
||||
res, err := g.SetupPiece(0, 0, p1)
|
||||
if err == nil || res == true {
|
||||
t.Errorf("Expected to fail setup piece but didn't")
|
||||
}
|
||||
g.state = gameSetup
|
||||
res, err = g.SetupPiece(0, 0, p2)
|
||||
if err == nil || res == true {
|
||||
t.Error("Expected to fail putting red piece on blue board")
|
||||
}
|
||||
res, err = g.SetupPiece(0, 0, p1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res, err = g.SetupPiece(9, 9, p2)
|
||||
if err == nil {
|
||||
t.Error("expected to fail setting up piece in invalid spot")
|
||||
}
|
||||
res, err = g.SetupPiece(4, 0, p2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStart(t *testing.T) {
|
||||
g := NewGame()
|
||||
res := g.Start()
|
||||
if res {
|
||||
t.Fatal("expected to fail starting game due to state")
|
||||
}
|
||||
g.state = gameSetup
|
||||
res = g.Start()
|
||||
if !res {
|
||||
t.Fatal("expected game to start")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user