-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_test.go
238 lines (231 loc) · 5.91 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
package gothello
import "testing"
func TestGame(t *testing.T) {
g := InitGame(NewNullControl(), NewNullControl())
g.String()
if !g.PlayTurn() {
t.Errorf("Incorrectly reporting game has ended on first move.")
}
// populate white with all of the empty squares
g.white |= ^g.black
if !g.IsEnd() {
t.Errorf("Incorrectly reporting game continues with full board.")
}
}
func testmove(black, white, move, bresult, wresult Bitboard, cp int) bool {
g := &Game{white, black, cp, 0, NewNullControl(), NewNullControl()}
g.MakeMove(move)
return g.black == bresult && g.white == wresult
}
func TestMakeMove(t *testing.T) {
// flip right
if !testmove(
A3|A5|A7|A6,
A8,
A4,
A3,
A4|A5|A6|A7|A8,
WHITE) {
t.Errorf("Flip right: A4 fails to flip A5-7 when A8 is owned.")
}
// flip left
if !testmove(
B1|B8,
B2|B4|B5|B7,
B3,
B1|B2|B3|B8,
B4|B5|B7,
BLACK) {
t.Errorf("Flip left: B3 fails to flip B2 when B1 is owned.")
}
// flip left and right
if !testmove(
H1|H6,
H2|H3|H5,
H4,
H1|H2|H3|H4|H5|H6,
Bitboard(0),
BLACK) {
t.Errorf("Flip L and R: H4 fails to flip H2, H3, H5 with H1, H6 owned.")
}
// no flip
if !testmove(
E1|E3|E4|E5|E6,
D2|F2|G2,
E2,
E1|E3|E4|E5|E6,
D2|F2|G2|E2,
WHITE) {
t.Errorf("No flip: E2 should flip nothing when none of E is owned.")
}
//flip up
if !testmove(
B2|G2|H2,
C2|D2|H8,
E2,
B2|C2|D2|H2|E2|G2,
H8,
BLACK) {
t.Errorf("Flip up: E2 should flip C2 and D2 when B2 owned.")
}
//flip down
if !testmove(
F4|G4,
H4,
E4,
Bitboard(0),
F4|E4|G4|H4,
WHITE) {
t.Errorf("Flip down: E4 should flip F4 and G4 when H4 owned.")
}
//flip up and down
if !testmove(
A8|F8,
B8|D8|E8,
C8,
C8|B8|D8|E8|A8|F8,
Bitboard(0),
BLACK) {
t.Errorf("Flip U & D: C8 should flip B8, D8, E8 with A8, F8 owned.")
}
//no flip
if !testmove(
cols[0]^G1,
rows[0]^A1,
G1,
cols[0]^G1,
rows[0]^A1|G1,
WHITE) {
t.Errorf("No flip: G1 should flip nothing with none of col 1 owned.")
}
//flip 45
if !testmove(
B4,
D2|C3,
E1,
B4|D2|C3|E1,
Bitboard(0),
BLACK) {
t.Errorf("Flip 45: E1 should flip D2 and C3 with B4 owned.")
}
//flip 180+45=225
if !testmove(
D6|E5,
F4,
C7,
Bitboard(0),
C7|D6|E5|F4,
WHITE) {
t.Errorf("Flip 225: C7 should flip D6 and E5 with F4 owned.")
}
//flip 45 and 225
if !testmove(
G2|B7,
A1|F3|D5|C6,
E4,
E4|F3|D5|C6|G2|B7,
A1,
BLACK) {
t.Errorf("Flip 45+225: E4 should flip F3, D5, and C6 with G2 and B7 owned.")
}
//no flip
if !testmove(
diag45[4],
diag45[6],
E2,
diag45[4],
diag45[6]|E2,
WHITE) {
t.Errorf("No Flip: E2 should flip nothing when none of its diagonal owned.")
}
//flip 135
if !testmove(
A2,
C4|B3,
D5,
A2|C4|B3|D5,
Bitboard(0),
BLACK) {
t.Errorf("Flip 135: D5 should flip C4 and B3 with A2 owned")
}
//flip 180+135=315
if !testmove(
G3,
H4,
F2,
Bitboard(0),
F2|H4|G3,
WHITE) {
t.Errorf("Flip 315: F2 should flip G3 with H4 owned.")
}
//flip 135 and 315
if !testmove(
A4|E8,
B5|D7,
C6,
diag135[4],
Bitboard(0),
BLACK) {
t.Errorf("Flip 135 and 315: C6 should flip B5 and D7 with A4 and E8 owned.")
}
//no flip
if !testmove(
diag135[9],
diag135[11],
E2,
diag135[9],
diag135[11]|E2,
WHITE) {
t.Errorf("No flip: E2 should not flip anything when none of its diagonal owned.")
}
//test all directions
if !testmove(
A1|C1|E1|E3|E5|C5|A5|A3,
B2|B3|B4|C4|D4|D3|D2|C2,
C3,
C3|A1|C1|E1|E3|E5|C5|A5|A3|B2|B3|B4|C4|D4|D3|D2|C2,
Bitboard(0),
BLACK) {
t.Errorf("Flip all: C3 should have flipped all its neighbors.")
}
}
func TestScore(t *testing.T) {
game := InitGame(NewNullControl(), NewNullControl())
if game.Score(game.CurPlayer()) != 2 {
t.Errorf("Black's score should be 2 at start of game.")
}
game.MakeMove(D3)
game.NextPlayer()
if game.Score(game.CurPlayer()) != 1 {
t.Errorf("White's score should be 1 after Black does D3.")
}
}
func TestLegalMoves(t *testing.T) {
game := InitGame(NewNullControl(), NewNullControl())
if game.LegalMoves()&C4 == 0 {
t.Errorf("Black to C4 is legal at the start of the game.")
}
game.NextPlayer()
if game.LegalMoves()&C4 != 0 {
t.Errorf("White to C4 is illegal at the start of the game.")
}
}
func BenchmarkMakeMove(b *testing.B) {
// let's see how fast this shiz is
game1 := &Game{cols[1], cols[3], BLACK,
0, NewNullControl(), NewNullControl()}
game2 := &Game{B1 | E1 | H1 | A8 | E8 | H7, diag45[7] |
diag135[8] | rows[4] | cols[3] ^ (B1 | E1 | H1 | A8 | E8 | H7), BLACK,
0, NewNullControl(), NewNullControl()}
game3 := &Game{B3 | B8 | E6 | D4, B4 | B5 | B7 | C5 | C6 | D6, BLACK,
0, NewNullControl(), NewNullControl()}
for i := 0; i < b.N; i++ {
game1.MakeMove(E3)
game2.MakeMove(E4)
game3.MakeMove(B6)
game2.black = B1 | E1 | H1 | A8 | E8 | H7
game2.white = diag45[7] | diag135[8] | rows[4] | cols[3] ^ (B1 | E1 | H1 | A8 | E8 | H7)
game3.black = B3 | B8 | E6 | D4
game3.white = B4 | B5 | B7 | C5 | C6 | D6
}
}