-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_test.go
409 lines (362 loc) · 13.8 KB
/
game_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package blokus
import (
"fmt"
"strings"
"testing"
)
func newGameOrDie(t *testing.T) *Game {
g, err := NewGame(10, []*Piece{newPieceOrDie(t, []Coord{{0, 0}})})
if err != nil {
t.Fatalf("NewGame(): got %v, want no error", err)
}
return g
}
func newPieceOrDie(t *testing.T, blocks []Coord) *Piece {
p, err := NewPiece(blocks)
if err != nil {
t.Fatalf("NewPiece(): got %v, want no error", err)
}
return p
}
func TestNewGame(t *testing.T) {
p := newPieceOrDie(t, []Coord{Coord{3, 4}})
g, err := NewGame(22, []*Piece{p})
if err != nil {
t.Fatalf("NewGame(): got %v, want no error", err)
}
if g.Board == nil {
t.Fatal("Game board: got nil, want not nil")
}
if got, want := g.Board.Height, 22; got != want {
t.Errorf("Game board height: got %v, want %v", got, want)
}
if got, want := g.Board.Width, 22; got != want {
t.Errorf("Game board width: got %v, want %v", got, want)
}
if got, want := len(g.Pieces), 1; got != want {
t.Fatalf("Game pieces len: got %v, want %v", got, want)
}
if got, want := len(g.Pieces[0].Blocks), 1; got != want {
t.Fatalf("Game pieces[0] blocks len: got %v, want %v", got, want)
}
if got, want := g.Pieces[0].Blocks[0], (Coord{3, 4}); got != want {
t.Errorf("Game pieces[0] blocks[0]: got %v, want %v", got, want)
}
}
func TestAddPlayer(t *testing.T) {
ps := []*Piece{
newPieceOrDie(t, []Coord{{3, 4}}),
newPieceOrDie(t, []Coord{{5, 6}}),
}
g, err := NewGame(22, ps)
if err != nil {
t.Fatalf("NewGame(): got %v, want no error", err)
}
if err := g.AddPlayer("foo", Red, Coord{0, 0}); err != nil {
t.Fatalf("AddPlayer(): got %v, want no error", err)
}
if got, want := len(g.Players), 1; got != want {
t.Fatalf("Game players len: got %v, want %v", got, want)
}
p := g.Players[0]
if got, want := p.Name, "foo"; got != want {
t.Errorf("Player name: got %v, want %v", got, want)
}
if got, want := p.Color, Red; got != want {
t.Errorf("Player color: got %v, want %v", got, want)
}
if got, want := p.StartPos, (Coord{0, 0}); got != want {
t.Errorf("Player start position: got %v, want %v", got, want)
}
if got, want := len(p.PlacedPieces), len(ps); got != want {
t.Errorf("Player placed pieces length: got %v, want %v", got, want)
}
for i, placed := range p.PlacedPieces {
if placed {
t.Errorf("Player placed pieces slice at index %v: got true, want false", i)
}
}
}
func TestAddPlayerInvalidColor(t *testing.T) {
g := newGameOrDie(t)
if err, msg := g.AddPlayer("foo", 99, Coord{9, 9}), "color"; err == nil || !strings.Contains(err.Error(), msg) {
t.Errorf("AddPlayer() with invalid color: got %v, want error to contain %v", err, msg)
}
}
func TestAddPlayerInvalidStartPosition(t *testing.T) {
g := newGameOrDie(t)
if err := g.AddPlayer("foo", Blue, Coord{10, 10}); err == nil || !strings.Contains(err.Error(), "position is out of bounds") {
t.Errorf("AddPlayer() with invalid start position: got %v, want starting position out of bounds error", err)
}
}
func TestAddPlayerDupeName(t *testing.T) {
g := newGameOrDie(t)
name := "foo"
if err := g.AddPlayer(name, Blue, Coord{9, 9}); err != nil {
t.Fatalf("Add first player: got error %v, want no error", err)
}
if err := g.AddPlayer(name, Yellow, Coord{0, 0}); err == nil || !strings.Contains(err.Error(), name) {
t.Errorf("AddPlayer() with duplicate name: got %v, want duplicate name error containing %v", err, name)
}
}
func TestAddPlayerDupeColor(t *testing.T) {
g := newGameOrDie(t)
color := Blue
if err := g.AddPlayer("foo", color, Coord{9, 9}); err != nil {
t.Fatalf("Add first player: got error %v, want no error", err)
}
if err := g.AddPlayer("bar", color, Coord{0, 0}); err == nil || !strings.Contains(err.Error(), color.String()) {
t.Errorf("AddPlayer() with duplicate color: got %v, want duplicate color error containing %v", err, color)
}
}
func TestAddPlayerDupeStartPosition(t *testing.T) {
g := newGameOrDie(t)
if err := g.AddPlayer("foo", Blue, Coord{9, 9}); err != nil {
t.Fatalf("Add first player: got error %v, want no error", err)
}
if err := g.AddPlayer("bar", Yellow, Coord{9, 9}); err == nil || !strings.Contains(err.Error(), "position already occupied") {
t.Errorf("AddPlayer() with duplicate start position: got %v, want position already occupied error", err)
}
}
func TestAddPlayerAutoAssignColor(t *testing.T) {
g := newGameOrDie(t)
if err := g.AddPlayer("foo", colorEmpty, Coord{0, 0}); err != nil {
t.Errorf("Add player with no color: got error %v, want no error", err)
}
if got, want := len(g.Players), 1; got != want {
t.Errorf("Num players after AddPlayer(): got %v, want %v", got, want)
}
if got, want := g.Players[0].Color, Color(1); got != want {
t.Errorf("Player color after AddPlayer(): got %v, want %v", got, want)
}
}
func TestAddPlayerAutoAssignColorNoMoreColors(t *testing.T) {
g := newGameOrDie(t)
for i := 1; i < int(colorEnd); i++ {
if err := g.AddPlayer(fmt.Sprintf("foo_%d", i), colorEmpty, Coord{0, i}); err != nil {
t.Fatalf("Add player %d with no color: got error %v, want no error", i, err)
}
}
colors := make(map[Color]bool)
for _, p := range g.Players {
colors[p.Color] = true
}
if got, want := len(colors), int(colorEnd)-1; got != want {
t.Errorf("Num colors after adding max players: got %v, want %v", got, want)
}
if err := g.AddPlayer("bar", colorEmpty, Coord{1, 0}); err == nil || !strings.Contains(err.Error(), "color") {
t.Errorf("Add extra player with no color: got %v, want error about color", err)
}
}
func newGameWithTwoPlayersAndTwoPieces(t *testing.T, size int) *Game {
tps := []*Piece{
newPieceOrDie(t, []Coord{{0, 0}, {1, 0}, {2, 0}}),
newPieceOrDie(t, []Coord{{0, 0}, {1, 0}, {1, 1}, {2, 1}}),
}
g, err := NewGame(size, tps)
if err != nil {
t.Fatalf("NewGame(ot %v, want no error", err)
}
if err := g.AddPlayer("foo", Blue, Coord{0, 0}); err != nil {
t.Fatalf("AddPlayer(foo): got %v, want no error", err)
}
if err := g.AddPlayer("bar", Yellow, Coord{size - 1, size - 1}); err != nil {
t.Fatalf("AddPlayer(bar): got %v, want no error", err)
}
return g
}
func TestPassTurn(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 10)
if err := g.PassTurn(g.Players[0]); err != nil {
t.Errorf("PassTurn(player0): got %v, want no error", err)
}
if got, want := len(g.Moves), 1; got != want {
t.Fatalf("Num moves after PassTurn(): got %v, want %v", got, want)
}
if got := g.Moves[0]; !got.IsPass() {
t.Errorf("Moves[0]: got %v, want IsPass()=true", got)
}
}
func TestPassTurnNilPlayer(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 10)
if err := g.PassTurn(nil); err == nil || !strings.Contains(err.Error(), "Invalid player") {
t.Errorf("PassTurn(nil): got %v, want error about invalid player", err)
}
}
func TestPassTurnWrongPlayerTurn(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 10)
if err := g.PassTurn(g.Players[1]); err == nil || !strings.Contains(err.Error(), "Turn") {
t.Errorf("PassTurn(player1): got %v, want error about wrong turn", err)
}
}
func TestPlacePieceAlreadyPlaced(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 10)
player := g.Players[0]
if err := g.PlacePiece(player, 0, Orientation{Rot0, false}, Coord{0, 0}); err != nil {
t.Fatalf("PlacePiece 1st time: got %v, want no error", err)
}
if err := g.PlacePiece(player, 0, Orientation{Rot0, false}, Coord{2, 2}); err == nil || !strings.Contains(err.Error(), "already placed") {
t.Errorf("PlacePiece() 2nd time: got %v, want piece already placed error", err)
}
}
func TestPlacePieceOutOfTurn(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 10)
player := g.Players[1]
if err := g.PlacePiece(player, 0, Orientation{Rot0, false}, Coord{0, 0}); err == nil || !strings.Contains(strings.ToLower(err.Error()), "turn") {
t.Errorf("PlacePiece(): got %v, want out of turn error", err)
}
}
func TestPlacePieceValid(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 10)
numMovesBefore := len(g.Moves)
player := g.Players[0]
if err := g.PlacePiece(player, 0, Orientation{Rot270, false}, Coord{0, 0}); err != nil {
t.Fatalf("PlacePiece(): got %v, want no error", err)
}
spaces := []Coord{
{0, 0},
{0, 1},
{0, 2},
}
for _, s := range spaces {
if got := g.Board.Cell(s); got != player.Color {
t.Errorf("Board space [%v]: got %v, want occupied by piece colored %v", s, got, player.Color)
}
}
if !player.PlacedPieces[0] {
t.Errorf("Player placed piece: got false, want true")
}
// Move was recorded in the game.
if got, want := len(g.Moves), numMovesBefore+1; got != want {
t.Errorf("Number of moves: got %v, want %v", got, want)
}
gotMove := g.Moves[len(g.Moves)-1]
wantMove := &Move{
Player: player,
PieceIndex: 0,
Orient: Orientation{Rot270, false},
Loc: Coord{0, 0},
}
if *gotMove != *wantMove {
t.Errorf("Move info: got %v, want %v", *gotMove, *wantMove)
}
if got := gotMove.IsPass(); got {
t.Errorf("Move.IsPass(): got %v, want false", got)
}
// Should not advance turn
if got, want := g.CurPlayerIndex, 0; got != want {
t.Errorf("Player turn after placing piece: got %v, want %v", got, want)
}
}
func TestCheckPiecePlacementAtStartingPosition(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 10)
if err := g.checkPiecePlacement(g.Players[0], g.Pieces[1], Coord{0, 0}); err != nil {
t.Fatalf("checkPiecePlacement(): got %v, want no error", err)
}
}
func TestCheckPiecePlacementWithValidTouchingCorner(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 10)
if err := g.PlacePiece(g.Players[0], 0, Orientation{Rot0, false}, Coord{0, 0}); err != nil {
t.Fatalf("PlacePiece(blue 1st piece): got %v, want no error", err)
}
if err := g.checkPiecePlacement(g.Players[0], g.Pieces[1], Coord{3, 1}); err != nil {
t.Fatalf("checkPiecePlacement(blue 2nd piece): got %v, want no error", err)
}
}
func TestCheckPiecePlacementNoValidCorner(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 10)
if err := g.checkPiecePlacement(g.Players[0], g.Pieces[0], Coord{2, 2}); err == nil || !strings.Contains(err.Error(), "no corner touching") {
t.Errorf("checkPiecePlacement(): got %v, want no corner touching error", err)
}
}
func TestCheckPiecePlacementOutOfBounds(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 10)
if err := g.checkPiecePlacement(g.Players[0], g.Pieces[0], Coord{19, 19}); err == nil || !strings.Contains(err.Error(), "out of bounds") {
t.Errorf("checkPiecePlacement(): got %v, want out of bounds error", err)
}
}
func TestCheckPiecePlacementOverlapExistingPiece(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 3)
// Place an existing piece
if err := g.PlacePiece(g.Players[0], 1, Orientation{Rot0, false}, Coord{0, 0}); err != nil {
t.Fatalf("Place initial piece: got %v, want no error", err)
}
if err := g.checkPiecePlacement(g.Players[1], g.Pieces[1], Coord{0, 1}); err == nil || !strings.Contains(err.Error(), "occupied") {
t.Errorf("checkPiecePlacement(): got %v, want occupied error", err)
}
}
func TestCheckPiecePlacementTouchingSameColor(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 10)
if err := g.PlacePiece(g.Players[0], 0, Orientation{Rot0, false}, Coord{0, 0}); err != nil {
t.Fatalf("PlacePiece(blue 1st piece): got %v, want no error", err)
}
if err := g.checkPiecePlacement(g.Players[0], g.Pieces[1], Coord{2, 1}); err == nil || !strings.Contains(err.Error(), "another blue piece") {
t.Errorf("checkPiecePlacement(blue 2nd piece): got error %v, want next to another blue piece error", err)
}
}
func TestCheckPiecePlacementTouchingAnotherColor(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 3)
if err := g.PlacePiece(g.Players[0], 0, Orientation{Rot0, false}, Coord{0, 0}); err != nil {
t.Fatalf("PlacePiece(blue 1st piece): got %v, want no error", err)
}
if err := g.checkPiecePlacement(g.Players[1], g.Pieces[0], Coord{0, 2}); err != nil {
t.Fatalf("PlacePiece(yellow 1st piece): got %v, want no error", err)
}
}
func TestAdvanceTurnNoPlayers(t *testing.T) {
g := newGameOrDie(t)
if err := g.AdvanceTurn(); err == nil || !strings.Contains(err.Error(), "no players") {
t.Errorf("AdvanceTurn() with no players: got %v, want no players error", err)
}
if got := g.CurPlayerIndex; got != 0 {
t.Fatalf("After AdvanceTurn() returned error, curPlayerIndex: got %v, want 0", got)
}
}
func TestAdvanceTurn(t *testing.T) {
g := newGameOrDie(t)
if err := g.AddPlayer("foo", Red, Coord{0, 0}); err != nil {
t.Fatalf("Error adding player 'foo': %v", err)
}
if err := g.AddPlayer("bar", Blue, Coord{0, 9}); err != nil {
t.Fatalf("Error adding player 'bar': %v", err)
}
if got, want := g.Players[g.CurPlayerIndex].Name, "foo"; got != want {
t.Fatalf("Before AdvanceTurn(): got player %v, want player %v", got, want)
}
if err := g.AdvanceTurn(); err != nil {
t.Fatalf("AdvanceTurn() with players: got %v, want no error", err)
}
if got, want := g.Players[g.CurPlayerIndex].Name, "bar"; got != want {
t.Errorf("After AdvanceTurn(): got player %v, want player %v", got, want)
}
}
func TestIsGameEndNoPlayers(t *testing.T) {
g := newGameOrDie(t)
if got := g.IsGameEnd(); got {
t.Errorf("IsGameEnd() with no players: got %v, want false", got)
}
}
func TestIsGameEnd(t *testing.T) {
g := newGameWithTwoPlayersAndTwoPieces(t, 5)
if got := g.IsGameEnd(); got {
t.Errorf("IsGameEnd() with no moves: got %v, want false", got)
}
if err := g.PassTurn(g.Players[0]); err != nil {
t.Fatalf("PassTurn(player0): got %v, want no error", err)
}
if err := g.AdvanceTurn(); err != nil {
t.Fatalf("AdvanceTurn(player0): got %v, want no error", err)
}
if got := g.IsGameEnd(); got {
t.Errorf("IsGameEnd() with 1 pass: got %v, want false", got)
}
if err := g.PassTurn(g.Players[1]); err != nil {
t.Fatalf("PassTurn(player1): got %v, want no error", err)
}
if err := g.AdvanceTurn(); err != nil {
t.Fatalf("AdvanceTurn(player1): got %v, want no error", err)
}
if got := g.IsGameEnd(); !got {
t.Errorf("IsGameEnd() with 2 passes: got %v, want true", got)
}
}