-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_test.go
348 lines (279 loc) · 9.17 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
package main
import (
"testing"
)
func TestIsGameTerminated(t *testing.T) {
game := Game{gridSize: 4}
actual := game.IsGameTerminated()
expected := false
if actual != expected {
t.Errorf("got %v\nwant %v", actual, expected)
}
}
func TestGameSetup(t *testing.T) {
game := Game{gridSize: 4}
preTileState := [][][]int{[][]int{[]int{0, 0, 3}, []int{0, 1, 2}}}
gInfo := GameInfo{HighScore: 10, CurrentScore: 5, TileState: preTileState}
game.setup(gInfo)
if game.score != 5 {
t.Errorf("score %v expected %v", game.score, 5)
}
if game.highScore != 10 {
t.Errorf("high score %v expected %v", game.score, 5)
}
for x := 0; x < len(game.grid.cells); x++ {
for y := 0; y < len(game.grid.cells[x]); y++ {
if game.grid.cells[x][y].value != preTileState[x][y][2] {
t.Errorf("the value of game cells in %v column %v row is wrong", game.score, 5)
}
}
}
}
func TestGameSetupNotOvertakePre(t *testing.T) {
game := Game{gridSize: 4}
gInfo := GameInfo{}
game.setup(gInfo)
if game.score != 0 {
t.Errorf("score %v expected %v", game.score, 0)
}
if game.highScore != 0 {
t.Errorf("high score %v expected %v", game.score, 0)
}
notEmptyCount := 0
for x := 0; x < len(game.grid.cells); x++ {
for y := 0; y < len(game.grid.cells[x]); y++ {
if game.grid.cells[x][y].isEmpty {
if game.grid.cells[x][y].value != 0 {
t.Errorf("when cells is empty, %v should be equal to zero", game.grid.cells[x][y].value)
}
} else {
if game.grid.cells[x][y].value == 0 {
t.Errorf("when cells is empty, %v mustn't be equals to zero", game.grid.cells[x][y].value)
}
notEmptyCount++
}
}
}
if notEmptyCount != 2 {
t.Errorf("Not empty should be %v", 2)
}
}
func TestGetVector(t *testing.T) {
var game Game
vec := game.getVector(0)
if vec.x != 0 || vec.y != -1 {
t.Error("Vector to up value is expected x = %v, y = %v", 0, -1)
}
vec = game.getVector(1)
if vec.x != 1 || vec.y != 0 {
t.Error("Vector to right value is expected x = %v, y = %v", 0, -1)
}
vec = game.getVector(2)
if vec.x != 0 || vec.y != 1 {
t.Error("Vector to down value is expected x = %v, y = %v", 0, -1)
}
vec = game.getVector(3)
if vec.x != -1 || vec.y != 0 {
t.Error("Vector to left value is expected x = %v, y = %v", 0, -1)
}
}
func TestMoveTile(t *testing.T) {
pos := [][]int{[]int{0, 0}, []int{0, 1}}
game := Game{gridSize: 4}
game.setup(GameInfo{TileState: createTileState(4, pos)})
game.moveTile(&Tile{x: 0, y: 0, value: 4}, &Tile{x: 3, y: 3})
if game.grid.cells[3][3].value != 4 {
t.Error("Moved cell should be equal to 4")
}
}
var up Vector = Vector{x: 0, y: -1}
var right Vector = Vector{x: 1, y: 0}
var down Vector = Vector{x: 0, y: 1}
var left Vector = Vector{x: -1, y: 0}
func TestBuildTraversals(t *testing.T) {
game := Game{gridSize: 4}
var expected PositionTraversal
//up
expected = PositionTraversal{x: []int{0, 1, 2, 3}, y: []int{0, 1, 2, 3}}
checkPositionTraversal(game.BuildTraversals(up), expected, t)
//left
expected = PositionTraversal{x: []int{0, 1, 2, 3}, y: []int{0, 1, 2, 3}}
checkPositionTraversal(game.BuildTraversals(left), expected, t)
//down
expected = PositionTraversal{x: []int{0, 1, 2, 3}, y: []int{3, 2, 1, 0}}
checkPositionTraversal(game.BuildTraversals(down), expected, t)
//right
expected = PositionTraversal{x: []int{3, 2, 1, 0}, y: []int{0, 1, 2, 3}}
checkPositionTraversal(game.BuildTraversals(right), expected, t)
}
func TestFindFarthestPosition(t *testing.T) {
game := Game{gridSize: 4}
game.setup(GameInfo{TileState: createTileState(4, [][]int{[]int{3, 3}, []int{0, 3}})})
actualNext, actualFar := game.FindFarthestPosition(Tile{x: 0, y: 0}, down)
if actualNext.x != 0 || actualNext.y != 3 {
t.Errorf("Next cell should be 0, 3", actualNext)
}
if actualFar.x != 0 || actualFar.y != 4 {
t.Errorf("Next cell should be 0, 4", actualFar)
}
actualNext, actualFar = game.FindFarthestPosition(Tile{x: 0, y: 0}, left)
if actualNext.x != 0 || actualNext.y != 0 {
t.Errorf("Next cell should be 0, 0", actualNext)
}
if actualFar.x != -1 || actualFar.y != 0 {
t.Errorf("Next cell should be -1, 0", actualFar)
}
actualNext, actualFar = game.FindFarthestPosition(Tile{x: 0, y: 0}, right)
if actualNext.x != 2 || actualNext.y != 0 {
t.Errorf("Next cell should be 2, 0", actualNext)
}
if actualFar.x != 3 || actualFar.y != 0 {
t.Errorf("Next cell should be 3, 0", actualFar)
}
}
func TestPositionsEqual(t *testing.T) {
game := Game{gridSize: 4}
if game.positionsEqual(&Tile{x: 1, y: 1}, &Tile{x: 1, y: 1}) != true {
t.Error("positionsEqual should be true")
}
if game.positionsEqual(&Tile{x: 0, y: 1}, &Tile{x: 1, y: 1}) != false {
t.Error("positionsEqual should be false")
}
}
func TestTileMatchesAvailable(t *testing.T) {
game := Game{gridSize: 4}
gameInfo := GameInfo{TileState: createTileState(4, [][]int{[]int{3, 3}, []int{0, 3}})}
game.setup(gameInfo)
if game.tileMatchesAvailable() != false {
t.Error("The game still has empty cells. So it should return false")
}
gameInfo = GameInfo{TileState: createFullTileState(4, []int{2, 2})}
game.setup(gameInfo)
if game.tileMatchesAvailable() != true {
t.Error("The game still has empty cells. So it should return true")
}
gameInfo = GameInfo{
TileState: createFullTileState(4,
[]int{
2, 4, 2, 4,
4, 2, 4, 2,
2, 4, 2, 4,
4, 2, 4, 2,
})}
game.setup(gameInfo)
if game.tileMatchesAvailable() != false {
t.Error("The game still has empty cells. So it should return true")
}
}
func TestMovesAvailable(t *testing.T) {
game := Game{gridSize: 4}
gameInfo := GameInfo{TileState: createTileState(4, [][]int{[]int{3, 3}, []int{0, 3}})}
game.setup(gameInfo)
if game.movesAvailable() != true {
t.Error("The game still has empty cells. So it should return false")
}
gameInfo = GameInfo{TileState: createFullTileState(4, []int{2, 2})}
game.setup(gameInfo)
if game.movesAvailable() != true {
t.Error("The game still has empty cells. So it should return true")
}
gameInfo = GameInfo{
TileState: createFullTileState(4,
[]int{
2, 4, 2, 4,
4, 2, 4, 2,
2, 4, 2, 4,
4, 2, 4, 2,
})}
game.setup(gameInfo)
if game.movesAvailable() != false {
t.Error("The game still has empty cells. So it should return true")
}
}
const moveUp = 0
const moveRight = 1
const moveDown = 2
const moveLeft = 3
func TestUpMoveItDoesntMerge(t *testing.T) {
checkMoveTileDosentMerge(moveUp, [][]int{[]int{0, 0, 2}, []int{3, 3, 4}}, []Tile{Tile{x: 0, y: 0, value: 2}, Tile{x: 3, y: 0, value: 4}}, t)
}
func TestRightMoveItDoesntMerge(t *testing.T) {
checkMoveTileDosentMerge(moveRight, [][]int{[]int{0, 0, 2}, []int{3, 3, 4}}, []Tile{Tile{x: 3, y: 0, value: 2}, Tile{x: 3, y: 3, value: 4}}, t)
}
func TestDownMoveItDoesntMerge(t *testing.T) {
checkMoveTileDosentMerge(moveDown, [][]int{[]int{0, 0, 2}, []int{3, 3, 4}}, []Tile{Tile{x: 0, y: 3, value: 2}, Tile{x: 3, y: 3, value: 4}}, t)
}
func TestLeftMoveItDoesntMerge(t *testing.T) {
checkMoveTileDosentMerge(moveLeft, [][]int{[]int{0, 0, 2}, []int{3, 3, 4}}, []Tile{Tile{x: 0, y: 0, value: 2}, Tile{x: 0, y: 3, value: 4}}, t)
}
func TestMoveItMergeCell(t *testing.T) {
game := Game{gridSize: 4}
gameInfo := GameInfo{TileState: createTileState(4, [][]int{[]int{0, 0, 4}, []int{0, 3, 4}})}
game.setup(gameInfo)
game.move(moveDown)
expect := Tile{x: 0, y: 3, value: 8}
cell, _ := game.grid.CellContent(&expect)
if cell.isEmpty != false && cell.value != expect.value && len(expect.mergedFrom) != 2 {
t.Errorf("%v, %v should be full and it has %v, mergeFrom is %v", expect.x, expect.y, expect.value, expect.mergedFrom)
}
}
func checkMoveTileDosentMerge(move int, d [][]int, expect []Tile, t *testing.T) {
game := Game{gridSize: 4}
var gameInfo GameInfo
gameInfo.load()
gameInfo = GameInfo{TileState: createTileState(4, d)}
game.setup(gameInfo)
game.move(move)
for i := 0; i < len(expect); i++ {
cell, _ := game.grid.CellContent(&expect[i])
if cell.isEmpty != false && cell.value != expect[i].value {
t.Errorf("%v, %v should be full and it has %v", expect[i].x, expect[i].y, expect[i].value)
}
}
}
func checkPositionTraversal(actual PositionTraversal, expected PositionTraversal, t *testing.T) {
for i := 0; i < len(actual.x); i++ {
if actual.x[i] != expected.x[i] {
t.Errorf("The actual posiion x %v should be equal to %v at index %v", actual.x[i], expected.x[i], i)
}
}
for i := 0; i < len(actual.y); i++ {
if actual.y[i] != expected.y[i] {
t.Errorf("The actual position y %v should be equal to %v at index %v", actual.y[i], expected.y[i], i)
}
}
}
func createFullTileState(gridSize int, values []int) [][][]int {
ts := createTileState(gridSize, [][]int{})
for ly := 0; ly < gridSize; ly++ {
for lx := 0; lx < gridSize; lx++ {
if ly*gridSize+lx < len(values) {
ts[ly][lx][2] = values[ly*gridSize+lx]
} else {
ts[ly][lx][2] = 2
}
}
}
return ts
}
func createTileState(gridSize int, pos [][]int) [][][]int {
p := [][][]int{}
for ly := 0; ly < gridSize; ly++ {
r := [][]int{}
for lx := 0; lx < gridSize; lx++ {
var value int = 0
for pi := 0; pi < len(pos); pi++ {
if pos[pi][0] == lx && pos[pi][1] == ly {
if len(pos[pi]) > 2 {
value = pos[pi][2]
} else {
value = 4
}
}
}
r = append(r, []int{ly, lx, value})
}
p = append(p, r)
}
return p
}