-
Notifications
You must be signed in to change notification settings - Fork 14
/
position_moves_test.go
619 lines (507 loc) · 19 KB
/
position_moves_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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
// Copyright (c) 2014-2018 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.
//
// I am making my contributions/submissions to this project solely in my
// personal capacity and am not conveying any rights to any intellectual
// property of any third parties.
package donna
import(`github.com/michaeldv/donna/expect`; `testing`)
func TestPositionMoves010(t *testing.T) {
p := NewGame(`Ke1,e2`, `Kg8,d7,f7`).start()
expect.Eq(t, p.enpassant, uint8(0))
p = p.makeMove(NewMove(p, E2, E4))
expect.Eq(t, p.enpassant, uint8(0))
p = p.makeMove(NewMove(p, D7, D5))
expect.Eq(t, p.enpassant, uint8(0))
p = p.makeMove(NewMove(p, E4, E5))
expect.Eq(t, p.enpassant, uint8(0))
p = p.makeMove(NewEnpassant(p, F7, F5))
expect.Eq(t, p.enpassant, uint8(F6))
}
// Castle tests.
func TestPositionMoves030(t *testing.T) { // Everything is OK.
p := NewGame(`Ke1,Ra1,Rh1`, `Ke8`).start()
kingside, queenside := p.canCastle(p.color)
expect.True(t, kingside)
expect.True(t, queenside)
p = NewGame(`Ke1`, `M,Ke8,Ra8,Rh8`).start()
kingside, queenside = p.canCastle(p.color)
expect.True(t, kingside)
expect.True(t, queenside)
}
func TestPositionMoves040(t *testing.T) { // King checked.
p := NewGame(`Ke1,Ra1,Rh1`, `Ke8,Bg3`).start()
kingside, queenside := p.canCastle(p.color)
expect.False(t, kingside)
expect.False(t, queenside)
p = NewGame(`Ke1,Bg6`, `M,Ke8,Ra8,Rh8`).start()
kingside, queenside = p.canCastle(p.color)
expect.False(t, kingside)
expect.False(t, queenside)
}
func TestPositionMoves050(t *testing.T) { // Attacked square.
p := NewGame(`Ke1,Ra1,Rh1`, `Ke8,Bb3,Bh3`).start()
kingside, queenside := p.canCastle(p.color)
expect.False(t, kingside)
expect.False(t, queenside)
p = NewGame(`Ke1,Bb6,Bh6`, `M,Ke8,Ra8,Rh8`).start()
kingside, queenside = p.canCastle(p.color)
expect.False(t, kingside)
expect.False(t, queenside)
}
func TestPositionMoves060(t *testing.T) { // Wrong square.
p := NewGame(`Ke1,Ra8,Rh8`, `Ke5`).start()
kingside, queenside := p.canCastle(p.color)
expect.False(t, kingside)
expect.False(t, queenside)
p = NewGame(`Ke2,Ra1,Rh1`, `Ke8`).start()
kingside, queenside = p.canCastle(p.color)
expect.False(t, kingside)
expect.False(t, queenside)
p = NewGame(`Ke4`, `M,Ke8,Ra1,Rh1`).start()
kingside, queenside = p.canCastle(p.color)
expect.False(t, kingside)
expect.False(t, queenside)
p = NewGame(`Ke4`, `M,Ke7,Ra8,Rh8`).start()
kingside, queenside = p.canCastle(p.color)
expect.False(t, kingside)
expect.False(t, queenside)
}
func TestPositionMoves070(t *testing.T) { // Missing rooks.
p := NewGame(`Ke1`, `Ke8`).start()
kingside, queenside := p.canCastle(p.color)
expect.False(t, kingside)
expect.False(t, queenside)
p = NewGame(`Ke1`, `M,Ke8`).start()
kingside, queenside = p.canCastle(p.color)
expect.False(t, kingside)
expect.False(t, queenside)
}
func TestPositionMoves080(t *testing.T) { // Rooks on wrong squares.
p := NewGame(`Ke1,Rb1`, `Ke8`).start()
kingside, queenside := p.canCastle(p.color)
expect.False(t, kingside)
expect.False(t, queenside)
p = NewGame(`Ke1,Rb1,Rh1`, `Ke8`).start()
kingside, queenside = p.canCastle(p.color)
expect.True(t, kingside)
expect.False(t, queenside)
p = NewGame(`Ke1,Ra1,Rf1`, `Ke8`).start()
kingside, queenside = p.canCastle(p.color)
expect.False(t, kingside)
expect.True(t, queenside)
}
func TestPositionMoves081(t *testing.T) { // Rook has moved.
p := NewGame(`Ke1,Ra1,Rh1`, `Ke8`).start()
p = p.makeMove(NewMove(p, A1, A2))
p = p.makeMove(NewMove(p, E8, E7))
p = p.makeMove(NewMove(p, A2, A1))
kingside, queenside := p.canCastle(White)
expect.True(t, kingside)
expect.False(t, queenside)
}
func TestPositionMoves082(t *testing.T) { // King has moved.
p := NewGame(`Ke1`, `M,Ke8,Ra8,Rh8`).start()
p = p.makeMove(NewMove(p, E8, E7))
p = p.makeMove(NewMove(p, E1, E2))
p = p.makeMove(NewMove(p, E7, E8))
kingside, queenside := p.canCastle(Black)
expect.False(t, kingside)
expect.False(t, queenside)
}
func TestPositionMoves083(t *testing.T) { // Rook is taken.
p := NewGame(`Ke1,Nb6`, `Ke8,Ra8,Rh8`).start()
p = p.makeMove(NewMove(p, B6, A8))
kingside, queenside := p.canCastle(Black)
expect.True(t, kingside)
expect.False(t, queenside)
}
// Blocking kingside knight.
func TestPositionMoves084(t *testing.T) {
p := NewGame(`Ke1`, `M,Ke8,Ra8,Rh8,Ng8`).start()
kingside, queenside := p.canCastle(Black)
expect.False(t, kingside)
expect.True(t, queenside)
}
// Blocking queenside knight.
func TestPositionMoves085(t *testing.T) {
p := NewGame(`Ke1`, `M,Ke8,Ra8,Rh8,Nb8`).start()
kingside, queenside := p.canCastle(Black)
expect.True(t, kingside)
expect.False(t, queenside)
}
// Straight repetition.
func TestPositionMoves100(t *testing.T) {
p := NewGame().start() // Initial 1.
p = p.makeMove(NewMove(p, G1, F3))
p = p.makeMove(NewMove(p, G8, F6)) // 1.
expect.False(t, p.repetition())
expect.False(t, p.thirdRepetition())
p = p.makeMove(NewMove(p, F3, G1))
p = p.makeMove(NewMove(p, F6, G8)) // Initial 2.
expect.True(t, p.repetition())
expect.False(t, p.thirdRepetition())
p = p.makeMove(NewMove(p, G1, F3))
p = p.makeMove(NewMove(p, G8, F6)) // 2.
expect.True(t, p.repetition())
expect.False(t, p.thirdRepetition())
p = p.makeMove(NewMove(p, F3, G1))
p = p.makeMove(NewMove(p, F6, G8)) // Initial 3.
expect.True(t, p.repetition())
expect.True(t, p.thirdRepetition())
p = p.makeMove(NewMove(p, G1, F3))
p = p.makeMove(NewMove(p, G8, F6)) // 3.
expect.True(t, p.repetition())
expect.True(t, p.thirdRepetition())
}
// Repetition with some moves in between.
func TestPositionMoves110(t *testing.T) {
p := NewGame().start()
p = p.makeMove(NewMove(p, E2, E4))
p = p.makeMove(NewMove(p, E7, E5))
p = p.makeMove(NewMove(p, G1, F3))
p = p.makeMove(NewMove(p, G8, F6)) // 1.
p = p.makeMove(NewMove(p, B1, C3))
p = p.makeMove(NewMove(p, B8, C6))
p = p.makeMove(NewMove(p, F1, C4))
p = p.makeMove(NewMove(p, F8, C5))
p = p.makeMove(NewMove(p, C3, B1))
p = p.makeMove(NewMove(p, C6, B8))
p = p.makeMove(NewMove(p, C4, F1))
p = p.makeMove(NewMove(p, C5, F8)) // 2.
expect.True(t, p.repetition())
expect.False(t, p.thirdRepetition())
p = p.makeMove(NewMove(p, F1, C4))
p = p.makeMove(NewMove(p, F8, C5))
p = p.makeMove(NewMove(p, B1, C3))
p = p.makeMove(NewMove(p, B8, C6))
p = p.makeMove(NewMove(p, C4, F1))
p = p.makeMove(NewMove(p, C5, F8))
p = p.makeMove(NewMove(p, C3, B1))
p = p.makeMove(NewMove(p, C6, B8)) // 3.
expect.True(t, p.repetition())
expect.True(t, p.thirdRepetition())
}
// Irreversible 0-0.
func TestPositionMoves120(t *testing.T) {
p := NewGame(`Ke1,Rh1,h2`, `Ke8,Ra8,a7`).start()
p = p.makeMove(NewMove(p, H2, H4))
p = p.makeMove(NewMove(p, A7, A5)) // 1.
p = p.makeMove(NewMove(p, E1, E2))
p = p.makeMove(NewMove(p, E8, E7)) // King has moved.
p = p.makeMove(NewMove(p, E2, E1))
p = p.makeMove(NewMove(p, E7, E8)) // 2.
p = p.makeMove(NewMove(p, E1, E2))
p = p.makeMove(NewMove(p, E8, E7)) // King has moved again.
p = p.makeMove(NewMove(p, E2, E1))
p = p.makeMove(NewMove(p, E7, E8)) // 3.
expect.True(t, p.repetition())
expect.False(t, p.thirdRepetition()) // <-- Lost 0-0 right.
p = p.makeMove(NewMove(p, E1, E2))
p = p.makeMove(NewMove(p, E8, E7)) // King has moved again.
p = p.makeMove(NewMove(p, E2, E1))
p = p.makeMove(NewMove(p, E7, E8)) // 4.
expect.True(t, p.repetition())
expect.True(t, p.thirdRepetition()) // <-- 3 time repetioion with lost 0-0 right.
}
// 50 moves draw (no captures, no pawn moves).
func TestPositionMoves130(t *testing.T) {
p := NewGame(`Kh8,Ra1`, `Ka8,a7,b7`).start()
squares := [64]int{
A1, B1, C1, D1, E1, F1, G1, H1,
H2, G2, F2, E2, D2, C2, B2, A2,
A3, B3, C3, D3, E3, F3, G3, H3,
H4, G4, F4, E4, D4, C4, B4, A4,
A5, B5, C5, D5, E5, F5, G5, H5,
H6, G6, F6, E6, D6, C6, B6, A6,
A5, B5, C5, D5, E5, F5, G5, H5,
H4, G4, F4, E4, D4, C4, B4, A4,
}
// White rook is zigzaging while black king bounces back and forth.
for move := 1; move < len(squares); move++ {
p = p.makeMove(NewMove(p, squares[move-1], squares[move]))
if p.king[Black] == A8 {
p = p.makeMove(NewMove(p, A8, B8))
} else {
p = p.makeMove(NewMove(p, B8, A8))
}
expect.Eq(t, p.fifty(), move >= 50)
}
}
// Incremental hash recalculation tests (see book_test.go).
func TestPositionMoves200(t *testing.T) { // 1. e4
p := NewGame().start()
p = p.makeMove(NewMove(p, E2, E4))
hash, pawnHash := p.polyglot()
expect.Eq(t, hash, uint64(0x823C9B50FD114196))
expect.Eq(t, hash, p.id)
expect.Eq(t, pawnHash, uint64(0x0B2D6B38C0B92E91))
expect.Eq(t, pawnHash, p.pawnId)
expect.Eq(t, p.balance, len(materialBase) - 1)
expect.Eq(t, p.enpassant, uint8(0))
expect.Eq(t, p.castles, uint8(0x0F))
}
func TestPositionMoves210(t *testing.T) { // 1. e4 d5
p := NewGame().start()
p = p.makeMove(NewMove(p, E2, E4))
p = p.makeMove(NewMove(p, D7, D5))
hash, pawnHash := p.polyglot()
expect.Eq(t, hash, uint64(0x0756B94461C50FB0))
expect.Eq(t, hash, p.id)
expect.Eq(t, pawnHash, uint64(0x76916F86F34AE5BE))
expect.Eq(t, pawnHash, p.pawnId)
expect.Eq(t, p.balance, len(materialBase) - 1)
expect.Eq(t, p.enpassant, uint8(0))
expect.Eq(t, p.castles, uint8(0x0F))
}
// 1. e4 d5 2. e5
func TestPositionMoves220(t *testing.T) {
p := NewGame().start()
p = p.makeMove(NewMove(p, E2, E4))
p = p.makeMove(NewMove(p, D7, D5))
p = p.makeMove(NewMove(p, E4, E5))
hash, pawnHash := p.polyglot()
expect.Eq(t, hash, uint64(0x662FAFB965DB29D4))
expect.Eq(t, hash, p.id)
expect.Eq(t, pawnHash, uint64(0xEF3E5FD1587346D3))
expect.Eq(t, pawnHash, p.pawnId)
expect.Eq(t, p.balance, len(materialBase) - 1)
expect.Eq(t, p.enpassant, uint8(0))
expect.Eq(t, p.castles, uint8(0x0F))
}
// 1. e4 d5 2. e5 f5 <-- Enpassant
func TestPositionMoves230(t *testing.T) {
p := NewGame().start()
p = p.makeMove(NewMove(p, E2, E4))
p = p.makeMove(NewMove(p, D7, D5))
p = p.makeMove(NewMove(p, E4, E5))
p = p.makeMove(NewEnpassant(p, F7, F5))
hash, pawnHash := p.polyglot()
expect.Eq(t, hash, uint64(0x22A48B5A8E47FF78))
expect.Eq(t, hash, p.id)
expect.Eq(t, pawnHash, uint64(0x83871FE249DCEE04))
expect.Eq(t, pawnHash, p.pawnId)
expect.Eq(t, p.balance, len(materialBase) - 1)
expect.Eq(t, p.enpassant, uint8(F6))
expect.Eq(t, p.castles, uint8(0x0F))
}
// 1. e4 d5 2. e5 f5 3. Ke2 <-- White Castle
func TestPositionMoves240(t *testing.T) {
p := NewGame().start()
p = p.makeMove(NewMove(p, E2, E4))
p = p.makeMove(NewMove(p, D7, D5))
p = p.makeMove(NewMove(p, E4, E5))
p = p.makeMove(NewMove(p, F7, F5))
p = p.makeMove(NewMove(p, E1, E2))
hash, pawnHash := p.polyglot()
expect.Eq(t, hash, uint64(0x652A607CA3F242C1))
expect.Eq(t, hash, p.id)
expect.Eq(t, pawnHash, uint64(0x83871FE249DCEE04))
expect.Eq(t, pawnHash, p.pawnId)
expect.Eq(t, p.balance, len(materialBase) - 1)
expect.Eq(t, p.enpassant, uint8(0))
expect.Eq(t, p.castles, castleKingside[Black]|castleQueenside[Black])
}
// 1. e4 d5 2. e5 f5 3. Ke2 Kf7 <-- Black Castle
func TestPositionMoves250(t *testing.T) {
p := NewGame().start()
p = p.makeMove(NewMove(p, E2, E4))
p = p.makeMove(NewMove(p, D7, D5))
p = p.makeMove(NewMove(p, E4, E5))
p = p.makeMove(NewMove(p, F7, F5))
p = p.makeMove(NewMove(p, E1, E2))
p = p.makeMove(NewMove(p, E8, F7))
hash, pawnHash := p.polyglot()
expect.Eq(t, hash, uint64(0x00FDD303C946BDD9))
expect.Eq(t, hash, p.id)
expect.Eq(t, pawnHash, uint64(0x83871FE249DCEE04))
expect.Eq(t, pawnHash, p.pawnId)
expect.Eq(t, p.balance, len(materialBase) - 1)
expect.Eq(t, p.enpassant, uint8(0))
expect.Eq(t, p.castles, uint8(0))
}
// 1. a2a4 b7b5 2. h2h4 b5b4 3. c2c4 <-- Enpassant
func TestPositionMoves260(t *testing.T) {
p := NewGame().start()
p = p.makeMove(NewMove(p, A2, A4))
p = p.makeMove(NewMove(p, B7, B5))
p = p.makeMove(NewMove(p, H2, H4))
p = p.makeMove(NewMove(p, B5, B4))
p = p.makeMove(NewEnpassant(p, C2, C4))
hash, pawnHash := p.polyglot()
expect.Eq(t, hash, uint64(0x3C8123EA7B067637))
expect.Eq(t, hash, p.id)
expect.Eq(t, pawnHash, uint64(0xB5AA405AF42E7052))
expect.Eq(t, pawnHash, p.pawnId)
expect.Eq(t, p.balance, len(materialBase) - 1)
expect.Eq(t, p.enpassant, uint8(C3))
expect.Eq(t, p.castles, uint8(0x0F))
}
// 1. a2a4 b7b5 2. h2h4 b5b4 3. c2c4 b4xc3 4. Ra1a3 <-- Enpassant/Castle
func TestPositionMoves270(t *testing.T) {
p := NewGame().start()
p = p.makeMove(NewMove(p, A2, A4))
p = p.makeMove(NewMove(p, B7, B5))
p = p.makeMove(NewMove(p, H2, H4))
p = p.makeMove(NewMove(p, B5, B4))
p = p.makeMove(NewEnpassant(p, C2, C4))
p = p.makeMove(NewMove(p, B4, C3))
p = p.makeMove(NewMove(p, A1, A3))
hash, pawnHash := p.polyglot()
expect.Eq(t, hash, uint64(0x5C3F9B829B279560))
expect.Eq(t, hash, p.id)
expect.Eq(t, pawnHash, uint64(0xE214F040EAA135A0))
expect.Eq(t, pawnHash, p.pawnId)
expect.Eq(t, p.balance, len(materialBase) - 1 - materialBalance[Pawn])
expect.Eq(t, p.enpassant, uint8(0))
expect.Eq(t, p.castles, castleKingside[White] | castleKingside[Black] | castleQueenside[Black])
}
// Incremental material hash calculation.
// 1. e4 d5 2. e4xd5
func TestPositionMoves280(t *testing.T) {
p := NewGame().start()
p = p.makeMove(NewMove(p, E2, E4)); p = p.makeMove(NewMove(p, D7, D5))
p = p.makeMove(NewMove(p, E4, D5))
expect.Eq(t, p.balance, len(materialBase) - 1 - materialBalance[BlackPawn])
}
// 1. e4 d5 2. e4xd5 Ng8-f6 3. Nb1-c3 Nf6xd5
func TestPositionMoves281(t *testing.T) {
p := NewGame().start()
p = p.makeMove(NewMove(p, E2, E4)); p = p.makeMove(NewMove(p, D7, D5))
p = p.makeMove(NewMove(p, E4, D5)); p = p.makeMove(NewMove(p, G8, F6))
p = p.makeMove(NewMove(p, B1, C3)); p = p.makeMove(NewMove(p, F6, D5))
expect.Eq(t, p.balance, len(materialBase) - 1 - materialBalance[Pawn] - materialBalance[BlackPawn])
}
// 1. e4 d5 2. e4xd5 Ng8-f6 3. Nb1-c3 Nf6xd5 4. Nc3xd5 Qd8xd5
func TestPositionMoves282(t *testing.T) {
p := NewGame().start()
p = p.makeMove(NewMove(p, E2, E4)); p = p.makeMove(NewMove(p, D7, D5))
p = p.makeMove(NewMove(p, E4, D5)); p = p.makeMove(NewMove(p, G8, F6))
p = p.makeMove(NewMove(p, B1, C3)); p = p.makeMove(NewMove(p, F6, D5))
p = p.makeMove(NewMove(p, C3, D5)); p = p.makeMove(NewMove(p, D8, D5))
expect.Eq(t, p.balance, len(materialBase) - 1 - materialBalance[Pawn] - materialBalance[Knight] - materialBalance[BlackPawn] - materialBalance[BlackKnight])
}
// Pawn promotion.
func TestPositionMoves283(t *testing.T) {
p := NewGame(`Kh1`, `M,Ka8,a2,b7`).start()
expect.Eq(t, p.balance, 2 * materialBalance[BlackPawn])
p = p.makeMove(NewMove(p, A2, A1).promote(Rook))
expect.Eq(t, p.balance, materialBalance[BlackPawn] + materialBalance[BlackRook])
}
// Last pawn promotion.
func TestPositionMoves284(t *testing.T) {
p := NewGame(`Kh1`, `M,Ka8,a2`).start()
expect.Eq(t, p.balance, materialBalance[BlackPawn])
p = p.makeMove(NewMove(p, A2, A1).promote(Rook))
expect.Eq(t, p.balance, materialBalance[BlackRook])
}
// Pawn promotion with capture.
func TestPositionMoves285(t *testing.T) {
p := NewGame(`Kh1,Nb1,Ng1`, `M,Ka8,a2,b7`).start()
expect.Eq(t, p.balance, 2 * materialBalance[Knight] + 2 * materialBalance[BlackPawn])
p = p.makeMove(NewMove(p, A2, B1).promote(Queen))
expect.Eq(t, p.balance, materialBalance[Knight] + materialBalance[BlackPawn] + materialBalance[BlackQueen])
}
// Pawn promotion with last piece capture.
func TestPositionMoves286(t *testing.T) {
p := NewGame(`Kh1,Nb1`, `M,Ka8,a2,b7`).start()
expect.Eq(t, p.balance, materialBalance[Knight] + 2 * materialBalance[BlackPawn])
p = p.makeMove(NewMove(p, A2, B1).promote(Queen))
expect.Eq(t, p.balance, materialBalance[BlackPawn] + materialBalance[BlackQueen])
}
// Last pawn promotion with capture.
func TestPositionMoves287(t *testing.T) {
p := NewGame(`Kh1,Nb1,Ng1`, `M,Ka8,a2`).start()
expect.Eq(t, p.balance, 2 * materialBalance[Knight] + materialBalance[BlackPawn])
p = p.makeMove(NewMove(p, A2, B1).promote(Queen))
expect.Eq(t, p.balance, materialBalance[Knight] + materialBalance[BlackQueen])
}
// Last pawn promotion with last piece capture.
func TestPositionMoves288(t *testing.T) {
p := NewGame(`Kh1,Nb1`, `M,Ka8,a2`).start()
expect.Eq(t, p.balance, materialBalance[Knight] + materialBalance[BlackPawn])
p = p.makeMove(NewMove(p, A2, B1).promote(Queen))
expect.Eq(t, p.balance, materialBalance[BlackQueen])
}
// Capture.
func TestPositionMoves289(t *testing.T) {
p := NewGame(`Kh1,Nc3,Nf3`, `M,Ka8,d4,e4`).start()
expect.Eq(t, p.balance, 2 * materialBalance[Knight] + 2 * materialBalance[BlackPawn])
p = p.makeMove(NewMove(p, D4, C3))
expect.Eq(t, p.balance, materialBalance[Knight] + 2 * materialBalance[BlackPawn])
}
// Last piece capture.
func TestPositionMoves290(t *testing.T) {
p := NewGame(`Kh1,Nc3`, `M,Ka8,d4,e4`).start()
expect.Eq(t, p.balance, materialBalance[Knight] + 2 * materialBalance[BlackPawn])
p = p.makeMove(NewMove(p, D4, C3))
expect.Eq(t, p.balance, 2 * materialBalance[BlackPawn])
}
// En-passant capture: 1. e2-e4 e7-e6 2. e4-e5 d7-d5 3. e4xd5
func TestPositionMoves291(t *testing.T) {
p := NewGame().start()
expect.Eq(t, p.balance, len(materialBase) - 1)
p = p.makeMove(NewMove(p, E2, E4)); p = p.makeMove(NewMove(p, E7, E6))
p = p.makeMove(NewMove(p, E4, E5)); p = p.makeMove(NewEnpassant(p, D7, D5))
p = p.makeMove(NewMove(p, E5, D6))
expect.Eq(t, p.balance, len(materialBase) - 1 - materialBalance[BlackPawn])
}
// Last pawn en-passant capture.
func TestPositionMoves292(t *testing.T) {
p := NewGame(`Kh1,c2`, `Ka8,d4`).start()
expect.Eq(t, p.balance, materialBalance[Pawn] + materialBalance[BlackPawn])
p = p.makeMove(NewEnpassant(p, C2, C4)); p = p.makeMove(NewMove(p, D4, C3))
expect.Eq(t, p.balance, materialBalance[BlackPawn])
}
// Unobstructed pins.
func TestPositionMoves300(t *testing.T) {
p := NewGame(`Ka1,Qe1,Ra8,Rh8,Bb5`, `Ke8,Re7,Bc8,Bf8,Nc6`).start()
pinned := p.pins(E8)
expect.Eq(t, pinned, bit[C6] | bit[C8] | bit[E7] | bit[F8])
}
func TestPositionMoves310(t *testing.T) {
p := NewGame(`Ke4,Qe5,Rd5,Nd4,Nf4`, `M,Ka7,Qe8,Ra4,Rh4,Ba8`).start()
pinned := p.pins(E4)
expect.Eq(t, pinned, bit[D5] | bit[E5] | bit[D4] | bit[F4])
}
// Not a pin (friendly blockers).
func TestPositionMoves320(t *testing.T) {
p := NewGame(`Ka1,Qe1,Ra8,Rh8,Bb5,Nb8,Ng8,e4`, `Ke8,Re7,Bc8,Bf8,Nc6`).start()
pinned := p.pins(E8)
expect.Eq(t, pinned, bit[C6])
}
func TestPositionMoves330(t *testing.T) {
p := NewGame(`Ke4,Qe7,Rc6,Nb4,Ng4`, `M,Ka7,Qe8,Ra4,Rh4,Ba8,c4,e6,f4`).start()
pinned := p.pins(E4)
expect.Eq(t, pinned, bit[C6])
}
// Not a pin (enemy blockers).
func TestPositionMoves340(t *testing.T) {
p := NewGame(`Ka1,Qe1,Ra8,Rh8,Bb5`, `Ke8,Re7,Rg8,Bc8,Bf8,Nc6,Nb8,e4`).start()
pinned := p.pins(E8)
expect.Eq(t, pinned, bit[C6])
}
func TestPositionMoves350(t *testing.T) {
p := NewGame(`Ke4,Qe7,Rc6,Nb4,Ng4,c4,e5,f4`, `M,Ka7,Qe8,Ra4,Rh4,Ba8`).start()
pinned := p.pins(E4)
expect.Eq(t, pinned, bit[C6])
}
// Position after null move.
func TestPositionMoves400(t *testing.T) {
p := NewGame(`Ke1,Qd1,d2,e2`, `Kg8,Qf8,f7,g7`).start()
p = p.makeNullMove()
expect.True(t, p.isNull())
p = p.undoLastMove()
p = p.makeMove(NewMove(p, E2, E4))
expect.False(t, p.isNull())
}
// isInCheck
func TestPositionMoves410(t *testing.T) {
p := NewGame().start()
p = p.makeMove(NewMove(p, E2, E4))
p = p.makeMove(NewMove(p, F7, F6))
position := p.makeMove(NewMove(p, D1, H5))
expect.True(t, position.isInCheck(position.color))
expect.True(t, position.isInCheck(p.color^1))
}