forked from notnil/chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_test.go
310 lines (288 loc) · 7.24 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
package chess
import (
"log"
"strings"
"testing"
)
func TestCheckmate(t *testing.T) {
fenStr := "rn1qkbnr/pbpp1ppp/1p6/4p3/2B1P3/5Q2/PPPP1PPP/RNB1K1NR w KQkq - 0 1"
fen, err := FEN(fenStr)
if err != nil {
t.Fatal(err)
}
g := NewGame(fen)
if err := g.MoveStr("Qxf7#"); err != nil {
t.Fatal(err)
}
if g.Method() != Checkmate {
t.Fatalf("expected method %s but got %s", Checkmate, g.Method())
}
if g.Outcome() != WhiteWon {
t.Fatalf("expected outcome %s but got %s", WhiteWon, g.Outcome())
}
// Checkmate on castle
fenStr = "Q7/5Qp1/3k2N1/7p/8/4B3/PP3PPP/R3K2R w KQ - 0 31"
fen, err = FEN(fenStr)
if err != nil {
t.Fatal(err)
}
g = NewGame(fen)
if err := g.MoveStr("O-O-O"); err != nil {
t.Fatal(err)
}
if g.Method() != Checkmate {
t.Fatalf("expected method %s but got %s", Checkmate, g.Method())
}
if g.Outcome() != WhiteWon {
t.Fatalf("expected outcome %s but got %s", WhiteWon, g.Outcome())
}
}
func TestCheckmateFromFen(t *testing.T) {
fenStr := "rn1qkbnr/pbpp1Qpp/1p6/4p3/2B1P3/8/PPPP1PPP/RNB1K1NR b KQkq - 0 1"
fen, err := FEN(fenStr)
if err != nil {
t.Fatal(err)
}
g := NewGame(fen)
if g.Method() != Checkmate {
t.Error(g.Position().Board().Draw())
t.Fatalf("expected method %s but got %s", Checkmate, g.Method())
}
if g.Outcome() != WhiteWon {
t.Fatalf("expected outcome %s but got %s", WhiteWon, g.Outcome())
}
}
func TestStalemate(t *testing.T) {
fenStr := "k1K5/8/8/8/8/8/8/1Q6 w - - 0 1"
fen, err := FEN(fenStr)
if err != nil {
t.Fatal(err)
}
g := NewGame(fen)
if err := g.MoveStr("Qb6"); err != nil {
t.Fatal(err)
}
if g.Method() != Stalemate {
t.Fatalf("expected method %s but got %s", Stalemate, g.Method())
}
if g.Outcome() != Draw {
t.Fatalf("expected outcome %s but got %s", Draw, g.Outcome())
}
}
// position shouldn't result in stalemate because pawn can move http://en.lichess.org/Pc6mJDZN#138
func TestInvalidStalemate(t *testing.T) {
fenStr := "8/3P4/8/8/8/7k/7p/7K w - - 2 70"
fen, err := FEN(fenStr)
if err != nil {
t.Fatal(err)
}
g := NewGame(fen)
if err := g.MoveStr("d8=Q"); err != nil {
t.Fatal(err)
}
if g.Outcome() != NoOutcome {
t.Fatalf("expected outcome %s but got %s", NoOutcome, g.Outcome())
}
}
func TestThreeFoldRepition(t *testing.T) {
g := NewGame()
moves := []string{
"Nf3", "Nf6", "Ng1", "Ng8",
"Nf3", "Nf6", "Ng1", "Ng8",
}
for _, m := range moves {
if err := g.MoveStr(m); err != nil {
t.Fatal(err)
}
}
if err := g.Draw(ThreefoldRepetition); err != nil {
for _, pos := range g.Positions() {
log.Println(pos.String())
}
t.Fatalf("%s - %d reps", err.Error(), g.numOfRepitions())
}
}
func TestInvalidThreeFoldRepition(t *testing.T) {
g := NewGame()
moves := []string{
"Nf3", "Nf6", "Ng1", "Ng8",
}
for _, m := range moves {
if err := g.MoveStr(m); err != nil {
t.Fatal(err)
}
}
if err := g.Draw(ThreefoldRepetition); err == nil {
t.Fatal("should require three repeated board states")
}
}
func TestFiveFoldRepition(t *testing.T) {
g := NewGame()
moves := []string{
"Nf3", "Nf6", "Ng1", "Ng8",
"Nf3", "Nf6", "Ng1", "Ng8",
"Nf3", "Nf6", "Ng1", "Ng8",
"Nf3", "Nf6", "Ng1", "Ng8",
}
for _, m := range moves {
if err := g.MoveStr(m); err != nil {
t.Fatal(err)
}
}
if g.Outcome() != Draw || g.Method() != FivefoldRepetition {
t.Fatal("should automatically draw after five repetitions")
}
}
func TestFiftyMoveRule(t *testing.T) {
fen, _ := FEN("2r3k1/1q1nbppp/r3p3/3pP3/pPpP4/P1Q2N2/2RN1PPP/2R4K b - b3 100 60")
g := NewGame(fen)
if err := g.Draw(FiftyMoveRule); err != nil {
t.Fatal(err)
}
}
func TestInvalidFiftyMoveRule(t *testing.T) {
fen, _ := FEN("2r3k1/1q1nbppp/r3p3/3pP3/pPpP4/P1Q2N2/2RN1PPP/2R4K b - b3 99 60")
g := NewGame(fen)
if err := g.Draw(FiftyMoveRule); err == nil {
t.Fatal("should require fifty moves")
}
}
func TestSeventyFiveMoveRule(t *testing.T) {
fen, _ := FEN("2r3k1/1q1nbppp/r3p3/3pP3/pPpP4/P1Q2N2/2RN1PPP/2R4K b - b3 149 80")
g := NewGame(fen)
if err := g.MoveStr("Kf8"); err != nil {
t.Fatal(err)
}
if g.Outcome() != Draw || g.Method() != SeventyFiveMoveRule {
t.Fatal("should automatically draw after seventy five moves w/ no pawn move or capture")
}
}
func TestInsufficentMaterial(t *testing.T) {
fens := []string{
"8/2k5/8/8/8/3K4/8/8 w - - 1 1",
"8/2k5/8/8/8/3K1N2/8/8 w - - 1 1",
"8/2k5/8/8/8/3K1B2/8/8 w - - 1 1",
"8/2k5/2b5/8/8/3K1B2/8/8 w - - 1 1",
"4b3/2k5/2b5/8/8/3K1B2/8/8 w - - 1 1",
}
for _, f := range fens {
fen, err := FEN(f)
if err != nil {
t.Fatal(err)
}
g := NewGame(fen)
if g.Outcome() != Draw || g.Method() != InsufficientMaterial {
log.Println(g.Position().Board().Draw())
t.Fatalf("%s should automatically draw by insufficent material", f)
}
}
}
func TestSufficentMaterial(t *testing.T) {
fens := []string{
"8/2k5/8/8/8/3K1B2/4N3/8 w - - 1 1",
"8/2k5/8/8/8/3KBB2/8/8 w - - 1 1",
"8/2k1b3/8/8/8/3K1B2/8/8 w - - 1 1",
"8/2k5/8/8/4P3/3K4/8/8 w - - 1 1",
"8/2k5/8/8/8/3KQ3/8/8 w - - 1 1",
"8/2k5/8/8/8/3KR3/8/8 w - - 1 1",
}
for _, f := range fens {
fen, err := FEN(f)
if err != nil {
t.Fatal(err)
}
g := NewGame(fen)
if g.Outcome() != NoOutcome {
log.Println(g.Position().Board().Draw())
t.Fatalf("%s should not find insufficent material", f)
}
}
}
func TestSerializationCycle(t *testing.T) {
g := NewGame()
pgn, err := PGN(strings.NewReader(g.String()))
if err != nil {
t.Fatal(err)
}
cp := NewGame(pgn)
if cp.String() != g.String() {
t.Fatalf("expected %s but got %s", g.String(), cp.String())
}
}
func TestInitialNumOfValidMoves(t *testing.T) {
g := NewGame()
if len(g.ValidMoves()) != 20 {
t.Fatal("should find 20 valid moves from the initial position")
}
}
func TestTagPairs(t *testing.T) {
g := NewGame()
g.AddTagPair("Draw Offer", "White")
tagPair := g.GetTagPair("Draw Offer")
if tagPair == nil {
t.Fatalf("expected %s but got %s", "White", "nil")
}
if tagPair.Value != "White" {
t.Fatalf("expected %s but got %s", "White", tagPair.Value)
}
g.RemoveTagPair("Draw Offer")
tagPair = g.GetTagPair("Draw Offer")
if tagPair != nil {
t.Fatalf("expected %s but got %s", "nil", "not nil")
}
}
func TestPositionHash(t *testing.T) {
g1 := NewGame()
for _, s := range []string{"Nc3", "e5", "Nf3"} {
g1.MoveStr(s)
}
g2 := NewGame()
for _, s := range []string{"Nf3", "e5", "Nc3"} {
g2.MoveStr(s)
}
if g1.Position().Hash() != g2.Position().Hash() {
t.Fatalf("expected position hashes to be equal but got %s and %s", g1.Position().Hash(), g2.Position().Hash())
}
}
func BenchmarkStalemateStatus(b *testing.B) {
fenStr := "k1K5/8/8/8/8/8/8/1Q6 w - - 0 1"
fen, err := FEN(fenStr)
if err != nil {
b.Fatal(err)
}
g := NewGame(fen)
if err := g.MoveStr("Qb6"); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
g.Position().Status()
}
}
func BenchmarkInvalidStalemateStatus(b *testing.B) {
fenStr := "8/3P4/8/8/8/7k/7p/7K w - - 2 70"
fen, err := FEN(fenStr)
if err != nil {
b.Fatal(err)
}
g := NewGame(fen)
if err := g.MoveStr("d8=Q"); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
g.Position().Status()
}
}
func BenchmarkPositionHash(b *testing.B) {
fenStr := "8/3P4/8/8/8/7k/7p/7K w - - 2 70"
fen, err := FEN(fenStr)
if err != nil {
b.Fatal(err)
}
g := NewGame(fen)
b.ResetTimer()
for n := 0; n < b.N; n++ {
g.Position().Hash()
}
}