-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests_logic.ps
More file actions
executable file
·1044 lines (743 loc) · 23.2 KB
/
tests_logic.ps
File metadata and controls
executable file
·1044 lines (743 loc) · 23.2 KB
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% Nicolas Seriot
% 2024-03-23
% https://github.com/nst/PSChess
% https://seriot.ch/projects/pschess.html
% gs -DNOSAFER tests_logic.ps
(logic_chess.ps) run
/AssertTypeName {
1 dict begin
/x exch def
x type /nametype ne { (type error, expect nametype, got) x type == quit } if
end
} bind def
/AssertTypeBool {
1 dict begin
/x exch def
x type /booleantype ne { (type error, expect boolean, got) x type == quit } if
end
} bind def
/AssertTypeString {
1 dict begin
/x exch def
x type /stringtype ne { (type error, expect string, got) == x type == quit } if
end
} bind def
/AssertTypeInt {
1 dict begin
/x exch def
x type /integertype ne { (type error, expect int, got) == x type == quit } if
end
} bind def
/AssertTypeArray {
1 dict begin
/x exch def
x type /arraytype ne { (type error, expect array, got) == x type == quit } if
end
} bind def
/AssertEmptyStack {
count 0 ne { (ERROR: stack should be empty, not ) =only count == (--------------) == pstack (--------------) == quit } if
} def
/_s_proc_AssertTrue {
/proc exch def
/s exch def
proc exec true eq { (--) =only s =only (: PASS) = }{ s =only (: FAIL) = quit } ifelse
} def
/_s_proc_AssertFalse {
/proc exch def
/s exch def
proc exec false eq { (--) =only s =only (: PASS) = }{ s =only (: FAIL) = quit } ifelse
} def
/_s_proc_AssertNull {
/proc exch def
/s exch def
proc exec null eq { (--) =only s =only (: PASS) = }{ s =only (: FAIL) = quit } ifelse
} def
/_s_proc_AssertNotNull {
/proc exch def
/s exch def
proc exec null ne { (--) =only s =only (: PASS) = }{ s =only (: FAIL) = quit } ifelse
} def
/_s_p1_p2_AssertEqual {
/p2 exch def
/p1 exch def
/s exch def
p1 exec p2 exec eq { (--) =only s =only (: PASS) = }{ s =only (: FAIL) = p1 =only ( ne ) =only p2 = quit } ifelse
} def
/_s_p1_p2_AssertNotEqual {
/p2 exch def
/p1 exch def
/s exch def
p1 exec p2 exec ne { (--) =only s =only (: PASS) = }{ s =only (: FAIL) = p1 =only ( eq ) =only p2 = quit } ifelse
} def
/_s_p1_p2_AssertArrayHaveEqualContents {
/p2 exch def
/p1 exch def
/s exch def
p1 exec p2 exec _a1_a2_HaveEqualContents_b_ { (--) =only s =only (: PASS) = }{ s =only (: FAIL) = quit } ifelse
} def
/_movesDict_MovesList_a_ {
(/_movesDict_MovesList_a_) CS_PUT
/a null def
5 dict begin
/d exch def
/m 64 array def
d {
/v exch def
/k exch def
0 1 v length 1 sub {
/i exch def
/o v i get def
o 1 eq {
m i 1 put
} if
} for
} forall
m _movesIntsOrNulls_MovesList_a_
end
CS_POP
} def
% -- test cases
/TestPawnIntoQueen {
(/TestPawnIntoQueen) CS_PUT
save
NewGame
GameState (BOARD) (\
........\
P.......\
n.......\
........\
........\
........\
........\
........\
) _s_Dup_s_ put
(TEST TestPawnIntoQueen.1) { GameState (CURRENT_PLAYER) get } { (white) } _s_p1_p2_AssertArrayHaveEqualContents
(TEST TestPawnIntoQueen.2) { GameState (BOARD) get (a8) _board_coord_Piece_p_ _piece_IsEmpty_b_ } _s_proc_AssertTrue
GameState (BOARD) get (a7) (a8) false _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
/msg exch def
/validMoves exch def
/canMove exch def
(TEST TestPawnIntoQueen.3) { GameState (CURRENT_PLAYER) get } { (black) } _s_p1_p2_AssertArrayHaveEqualContents
(TEST TestPawnIntoQueen.4) { GameState (BOARD) get (a8) _board_coord_Piece_p_ } { (Q) } _s_p1_p2_AssertEqual
GameState (BOARD) get (a6) (b4) false _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
/msg exch def
/validMoves exch def
/canMove exch def
(TEST TestPawnIntoQueen.5) { GameState (CURRENT_PLAYER) get } { (white) } _s_p1_p2_AssertArrayHaveEqualContents
restore
CS_POP
} def
/TestIndexInBoardRange {
(TestIndexInBoardRange) CS_PUT
10 dict begin
(TEST TestIndexInBoardRange.1) { -1 _index_IsInBoardRange_b_ } _s_proc_AssertFalse
(TEST TestIndexInBoardRange.2) { 0 _index_IsInBoardRange_b_ } _s_proc_AssertTrue
(TEST TestIndexInBoardRange.3) { 63 _index_IsInBoardRange_b_ } _s_proc_AssertTrue
(TEST TestIndexInBoardRange.4) { 64 _index_IsInBoardRange_b_ } _s_proc_AssertFalse
end
CS_POP
} def
/TestIsChecked {
(/TestIsChecked) CS_PUT
save
/b3 CreateBoard_b_ def
b3 36 (q) putinterval % add black queen in e4
b3 52 (.) putinterval % remove pawn
(-- TEST TestIsChecked.1) { b3 (white) [60] _board_whiteOrBlack_indices_IndicesContainOneChecked_b_ } _s_proc_AssertTrue
(-- TEST TestIsChecked.2) { b3 (white) _board_whiteOrBlack_KingIsChecked_b_ } _s_proc_AssertTrue
b3 52 (P) putinterval % add pawn back
(-- TEST TestIsChecked.3) { b3 (white) _board_whiteOrBlack_KingIsChecked_b_ } _s_proc_AssertFalse
restore
CS_POP
} def
/TestBoard {
(TestBoard) CS_PUT
save
/testBoard (\
r...k..K\
Ppn.p.np\
kK.b.Q..\
..b....n\
..p..rpb\
P.k...nn\
Bn.PbPnp\
....K..R\
) _s_Dup_s_ def
(TEST TestBoard.1) { testBoard (h1) _board_coord_Piece_p_ } { (R) } _s_p1_p2_AssertEqual
(TEST TestBoard.2) { testBoard 63 _board_index_Piece_p_ } { (R) } _s_p1_p2_AssertEqual
(TEST TestBoard.3) { 23 _index_Coord_c_ } { (h6) } _s_p1_p2_AssertEqual
(TEST TestBoard.4) { (h6) _coord_Index_i_ } { 23 } _s_p1_p2_AssertEqual
restore
CS_POP
} def
/TestCoords {
(TestCoords) CS_PUT
save
(TEST TestCoords.1) { 0 _index_Row_1-8_ } { 8 } _s_p1_p2_AssertEqual
(TEST TestCoords.2) { 7 _index_Row_1-8_ } { 8 } _s_p1_p2_AssertEqual
(TEST TestCoords.3) { 8 _index_Row_1-8_ } { 7 } _s_p1_p2_AssertEqual
(TEST TestCoords.4) { 55 _index_Row_1-8_ } { 2 } _s_p1_p2_AssertEqual
(TEST TestCoords.5) { 56 _index_Row_1-8_ } { 1 } _s_p1_p2_AssertEqual
(TEST TestCoords.6) { 63 _index_Row_1-8_ } { 1 } _s_p1_p2_AssertEqual
(TEST TestCoords.1) { 0 _index_Col_a-h_ } { (a) } _s_p1_p2_AssertEqual
(TEST TestCoords.2) { 7 _index_Col_a-h_ } { (h) } _s_p1_p2_AssertEqual
(TEST TestCoords.3) { 8 _index_Col_a-h_ } { (a) } _s_p1_p2_AssertEqual
(TEST TestCoords.4) { 55 _index_Col_a-h_ } { (h) } _s_p1_p2_AssertEqual
(TEST TestCoords.5) { 56 _index_Col_a-h_ } { (a) } _s_p1_p2_AssertEqual
(TEST TestCoords.6) { 63 _index_Col_a-h_ } { (h) } _s_p1_p2_AssertEqual
(TEST TestCoords.1) { (asd) _c_IsValidCoordinate_b_ } _s_proc_AssertFalse
(TEST TestCoords.2) { (a1) _c_IsValidCoordinate_b_ } _s_proc_AssertTrue
(TEST TestCoords.3) { (z1) _c_IsValidCoordinate_b_ } _s_proc_AssertFalse
(TEST TestCoords.4) { (h8) _c_IsValidCoordinate_b_ } _s_proc_AssertTrue
(TEST TestCoords.5) { (a9) _c_IsValidCoordinate_b_ } _s_proc_AssertFalse
(TEST TestCoords.6) { () _c_IsValidCoordinate_b_ } _s_proc_AssertFalse
(TEST TestCoords.5) { (K) _piece_IsWhite_b_ } _s_proc_AssertTrue
(TEST TestCoords.6) { (k) _piece_IsWhite_b_ } _s_proc_AssertFalse
(TEST TestCoords.7) { (K) _piece_IsBlack_b_ } _s_proc_AssertFalse
(TEST TestCoords.8) { (k) _piece_IsBlack_b_ } _s_proc_AssertTrue
restore
CS_POP
} def
/TestArraysEquality {
(TestArraysEquality) CS_PUT
(TEST TestArraysEquality.1) { [0 1 2] [0 0 0] _a1_a2_HaveEqualContents_b_ } _s_proc_AssertFalse
(TEST TestArraysEquality.2) { [0 1 2] [0 1] _a1_a2_HaveEqualContents_b_ } _s_proc_AssertFalse
(TEST TestArraysEquality.3) { [0 1 2] [0 1 2 3] _a1_a2_HaveEqualContents_b_ } _s_proc_AssertFalse
(TEST TestArraysEquality.4) { [0 1 2] [0 1 2] _a1_a2_HaveEqualContents_b_ } _s_proc_AssertTrue
CS_POP
} def
/TestMoves {
(/TestMoves) CS_PUT
save
/b CreateBoard_b_ def
/moves { b (g1) _board_coord_ValidMoves_a_ } def
(TEST TestMoves.1) { moves 52 get } { null } _s_p1_p2_AssertEqual
(TEST TestMoves.2) { moves 45 get } { 1 } _s_p1_p2_AssertEqual
(TEST TestMoves.3) { moves 47 get } { 1 } _s_p1_p2_AssertEqual
(TEST TestMoves.4) { moves 62 get } { null } _s_p1_p2_AssertEqual
restore
CS_POP
} def
/TestMoves2 {
(TestMoves2) CS_PUT
save
/b CreateBoard_b_ def
/testBoard (\
r...k..K\
Ppn.p.np\
kK.b.Q..\
..b....n\
..p..rpb\
P.k...nn\
Bn.PbPnp\
....K..R\
) _s_Dup_s_ def
(TEST TestMoves2.1) { [1 1 null 1] _movesIntsOrNulls_MovesList_a_ } { [0 1 3] } _s_p1_p2_AssertArrayHaveEqualContents
(TEST TestMoves2.2) { b (a2) _board_coord_ValidMoves_a_ _movesIntsOrNulls_MovesList_a_ } { [32 40] } _s_p1_p2_AssertArrayHaveEqualContents
(TEST TestMoves2.3) { b (e1) _board_coord_ValidMoves_a_ _movesIntsOrNulls_MovesList_a_ } { [] } _s_p1_p2_AssertArrayHaveEqualContents
(TEST TestMoves2.4) { b (g8) _board_coord_ValidMoves_a_ _movesIntsOrNulls_MovesList_a_ } { [21 23] } _s_p1_p2_AssertArrayHaveEqualContents
(TEST TestMoves2.5) { testBoard (a6) _board_coord_ValidMoves_a_ _movesIntsOrNulls_MovesList_a_ } { [8 17 24 25] } _s_p1_p2_AssertArrayHaveEqualContents
restore
CS_POP
} def
/TestQueenMovesAndChecks {
(TestQueenMovesAndChecks) CS_PUT
save
% queen moves in board 2 include (k) in 60
/b CreateBoard_b_ def
(TEST TestQueenMovesAndChecks.1) { b (e4) _board_coord_ValidMoves_a_ _movesIntsOrNulls_MovesList_a_ } { [] } _s_p1_p2_AssertArrayHaveEqualContents
(TEST TestQueenMovesAndChecks.2) { b (white) _board_whiteOrBlack_KingIsChecked_b_ } _s_proc_AssertFalse
b 36 (q) putinterval % add black queen
b 52 (.) putinterval % remove pawn
(TEST TestQueenMovesAndChecks.3) { b (e4) _board_coord_ValidMoves_a_ _movesIntsOrNulls_MovesList_a_ } { [18 20 22 27 28 29 32 33 34 35 37 38 39 43 44 45 50 52 54 60] } _s_p1_p2_AssertArrayHaveEqualContents
% b _board_PrintBoard quit
(TEST TestQueenMovesAndChecks.4) { b (white) _board_whiteOrBlack_LegalMoves_d_ _movesDict_MovesList_a_ } { [16 25 31 32 33 34 35 37 38 39 40 41 42 43 45 46 47 52] } _s_p1_p2_AssertArrayHaveEqualContents
(TEST TestQueenMovesAndChecks.5) { b (white) _board_whiteOrBlack_KingIsChecked_b_ } _s_proc_AssertTrue
restore
CS_POP
} def
/TestCapture1 {
(TestCapture1) CS_PUT
save
NewGame % resets captured lists
(TEST TestCapture1.1) { GameState (CAPTURED_BY_WHITE) get 0 1 getinterval } { (.) } _s_p1_p2_AssertEqual
(p) GameState (CAPTURED_BY_WHITE) get _p_a_AddToCaptured
(TEST TestCapture1.2) { GameState (CAPTURED_BY_WHITE) get 0 1 getinterval } { (p) } _s_p1_p2_AssertEqual
restore
CS_POP
} def
/TestCapture2 {
(TestCapture2) CS_PUT
save
NewGame % resets captured lists
/testBoard (\
........\
........\
........\
........\
........\
...r..q.\
..P...N.\
........\
) _s_Dup_s_ def
(TEST TestCapture2.1) { GameState (CAPTURED_BY_WHITE) get 0 1 getinterval } { (.) } _s_p1_p2_AssertEqual
testBoard (c2) (d3) false _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
/msg exch def
/validMoves exch def
/canMove exch def
(TEST TestCapture2.2) { GameState (CAPTURED_BY_WHITE) get 0 1 getinterval } { (r) } _s_p1_p2_AssertEqual
testBoard (g3) (g2) false _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
/msg exch def
/validMoves exch def
/canMove exch def
(TEST TestCapture2.3) { GameState (CAPTURED_BY_BLACK) get 0 1 getinterval } { (N) } _s_p1_p2_AssertEqual
restore
CS_POP
} def
/TestMoveFromToOrigin1 {
(TestMoveFromToOrigin1) CS_PUT
save
NewGame % resets captured lists
/b CreateBoard_b_ def
(TEST TestMoveFromToOrigin.1) { b (a1) _board_coord_ValidMoves_a_ _movesIntsOrNulls_MovesList_a_ } { [] } _s_p1_p2_AssertArrayHaveEqualContents
restore
CS_POP
} def
/TestMoveFromToOrigin2 {
(TestMoveFromToOrigin2) CS_PUT
save
NewGame % resets captured lists
/b CreateBoard_b_ def
b (a1) (a1) false _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
/msg exch def
/validMoves exch def
/canMove exch def
validMoves ==
(TEST TestMoveFromToOrigin2.1) { canMove } _s_proc_AssertFalse
restore
CS_POP
} def
/TestArrayReversed {
(TestArrayReversed) CS_PUT
save
(TEST TestArrayReversed.1) { [1 2 3] _a_DupReversed_a_ } { [3 2 1] } _s_p1_p2_AssertArrayHaveEqualContents
restore
} def
/TestEval {
(TestEval) CS_PUT
save
GameState (BOARD) (\
........\
P.......\
........\
........\
........\
........\
.rr.....\
........\
) _s_Dup_s_ put
(TEST TestEval.1) { GameState (BOARD) get _b_Eval_score_ } { 150 1020 sub } _s_p1_p2_AssertEqual
restore
CS_POP
} def
/TestBlackAutoMove {
(TestBlackAutoMove) CS_PUT
save
GameState (BOARD) (\
.......k\
.Q......\
Q.......\
........\
........\
........\
........\
........\
) _s_Dup_s_ put
GameState (CURRENT_PLAYER) (black) put
BlackAutoMoveAndCheckGameState_highlights_gameIsOver_
/gameIsOver exch def
/highlights exch def
(TEST TestBlackAutoMove.1) { gameIsOver } _s_proc_AssertFalse
% black have played
(TEST TestBlackAutoMove.2) { GameState (CURRENT_PLAYER) get } { (white) } _s_p1_p2_AssertEqual
% black had only one possible move
(TEST TestBlackAutoMove.3) { GameState (BOARD) get 6 1 getinterval } { (k) } _s_p1_p2_AssertEqual
% white does check mate
GameState (BOARD) get (a6) (a8) false _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
/msg exch def
/validMoves exch def
/canMove exch def
BlackAutoMoveAndCheckGameState_highlights_gameIsOver_
/gameIsOver exch def
/highlights exch def
(TEST TestBlackAutoMove.4) { gameIsOver } _s_proc_AssertTrue
restore
CS_POP
} def
/TestCanMove {
(TestCanMove) CS_PUT
save
% black checked, can move
/black_checked_board (\
k.......\
........\
Q.......\
........\
........\
........\
........\
........\
) _s_Dup_s_ def
% shorter version
/canMove (black) black_checked_board _player_board_CanPlay_b_ def
(TEST TestCanMove.1) { canMove } _s_proc_AssertTrue
% longer version
(black) black_checked_board false _player_board_useMinMax_FindBestMove_bestTo_bestFrom_bestScore_
/bestScore exch def
/bestFrom exch def
/bestTo exch def
(-- black bestScore:) =only bestScore ==
(-- black bestFrom:) =only bestFrom ==
(-- black bestTo:) =only bestTo ==
(TEST TestCanMove.2) { bestTo } _s_proc_AssertNotNull
restore
CS_POP
} def
/TestCannotMove {
(TestCannotMove) CS_PUT
save
% black checked, cannot move, check mate
/black_checked_mate_board (\
k.......\
.Q......\
..Q.....\
........\
........\
........\
........\
........\
) _s_Dup_s_ def
% shorter version
/canMove (black) black_checked_mate_board _player_board_CanPlay_b_ def
(TEST TestCannotMove.2) { canMove } _s_proc_AssertFalse
% longer version
(black) black_checked_mate_board false _player_board_useMinMax_FindBestMove_bestTo_bestFrom_bestScore_
/bestScore exch def
/bestFrom exch def
/bestTo exch def
(-- black bestScore:) =only bestScore ==
(-- black bestFrom:) =only bestFrom ==
(-- black bestTo:) =only bestTo ==
(TEST TestCannotMove.2) { bestTo } _s_proc_AssertNull
restore
CS_POP
} def
/TestEnPassant {
(TestEnPassant) CS_PUT
% https://en.wikipedia.org/wiki/En_passant
save
/board (\
........\
...p....\
........\
....P...\
........\
........\
........\
........\
) _s_Dup_s_ def
% black pawn moves 2 steps forward
% white takes it "en-passant"
GameState (CURRENT_PLAYER) (black) put
(TEST TestEnPassant.1) { GameState (EN_PASSANT_BLACK_CANDIDATE) get } _s_proc_AssertNull
board (d7) (d5) false _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
/_canMove exch def
/_highlights exch def
/_msg exch def
(TEST TestEnPassant.2) { GameState (EN_PASSANT_BLACK_CANDIDATE) get } { (d5) } _s_p1_p2_AssertEqual
(TEST TestEnPassant.3) { board (e5) _board_coord_ValidMoves_a_ _movesIntsOrNulls_MovesList_a_ } { [19 20] } _s_p1_p2_AssertArrayHaveEqualContents
% perform actual move
board (e5) (d6) false _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
/msg exch def
/validMoves exch def
/canMove exch def
(TEST TestEnPassant.3) { canMove } _s_proc_AssertTrue
% test captures state
GameState (CAPTURED_BY_WHITE) get ==
(TEST TestEnPassant.4) { GameState (CAPTURED_BY_WHITE) get 0 1 getinterval } { (p) } _s_p1_p2_AssertEqual
% test board after capture
/b2 (\
........\
........\
...P....\
........\
........\
........\
........\
........\
) _s_Dup_s_ def
(TEST TestEnPassant.5) { board } { b2 } _s_p1_p2_AssertArrayHaveEqualContents
% no more black candidate
(TEST TestEnPassant.6) { GameState (EN_PASSANT_BLACK_CANDIDATE) get } _s_proc_AssertNull
restore
CS_POP
} def
/TestCastling {
(TestCastling) CS_PUT
save
NewGame
/b1 (\
........\
....k...\
........\
........\
........\
........\
........\
....K..R\
) _s_Dup_s_ def
b1 (e1) (g1) false _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
/_canMove exch def
/_highlights exch def
/_msg exch def
/b2 (\
........\
....k...\
........\
........\
........\
........\
........\
.....RK.\
) _s_Dup_s_ def
(TEST TestCastling.1) { b1 } { b2 } _s_p1_p2_AssertArrayHaveEqualContents
restore
CS_POP
} def
/TestCheckCastling {
(TestCheckCastling) CS_PUT
save
NewGame
/b (\
........\
....q...\
........\
........\
........\
........\
....P...\
....K..R\
) _s_Dup_s_ def
(TEST TestNotCheckCastling.1) { b (white) _board_whiteOrBlack_KingIsChecked_b_ } _s_proc_AssertFalse
(TEST TestNotCheckCastling.2) { b (e1) _board_coord_ValidMoves_a_ _movesIntsOrNulls_MovesList_a_ } { [51 53 59 61 62] } _s_p1_p2_AssertArrayHaveEqualContents
b (e1) (g1) false _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
/msg exch def
/validMoves exch def
/canMove exch def
%(msg: ) ==only msg ==
%(validMoves: ) ==only validMoves ==
%(canMove: ) ==only canMove ==
(TEST TestNotCheckCastling.3) { canMove } _s_proc_AssertTrue
% remove pawn
b 52 (.) putinterval
b (e1) (g1) false _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
/msg exch def
/validMoves exch def
/canMove exch def
%(msg: ) ==only msg ==
%(validMoves: ) ==only validMoves ==
%(canMove: ) ==only canMove ==
(TEST TestNotCheckCastling.4) { canMove } _s_proc_AssertFalse
% move (q) so that (K) is not checked, but still cannot castle
b 12 (.) putinterval
b 13 (q) putinterval
b (e1) (g1) false _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
/msg exch def
/validMoves exch def
/canMove exch def
%(msg: ) ==only msg ==
%(validMoves: ) ==only validMoves ==
%(canMove: ) ==only canMove ==
(TEST TestNotCheckCastling.5) { canMove } _s_proc_AssertFalse
restore
CS_POP
} def
/TestKingIndex {
(TestKingIndex) CS_PUT
save
/b (\
........\
....q...\
........\
........\
........\
........\
........\
....K..R\
) _s_Dup_s_ def
(TEST TestKingIndex.1) { b (K) _board_piece_PieceIndex_i_ } { 60 } _s_p1_p2_AssertEqual
b 60 (.) putinterval
(TEST TestKingIndex.2) { b (K) _board_piece_PieceIndex_i_ } { null } _s_p1_p2_AssertEqual
restore
CS_POP
} def
/TestBoardEval {
(TestBoardEval) CS_PUT
save
/b (\
........\
...Rn...\
........\
....Q...\
........\
........\
.P......\
........\
) _s_Dup_s_ def
(TEST TestBoardEval.1) { b _b_Eval_score_ } { 110 510 add 325 sub 905 add } _s_p1_p2_AssertEqual
restore
CS_POP
} def
/TestAutoMove1 {
(TestAutoMove1) CS_PUT
save
/b (\
k.......\
........\
........\
........\
........\
........\
........\
Q.......\
) _s_Dup_s_ def
(white) b _player_board_MovesAndEvals_d_highestEval_highestMove_lowestEval_lowestMove_
/lowestMove exch def
/lowestEval exch def
/highestMove exch def
/highestEval exch def
/movesDict exch def
(-- lowestMove: ) =only lowestMove ==
(-- lowestEval: ) =only lowestEval ==
(-- highestMove: ) =only highestMove ==
(-- highestEval: ) =only highestEval ==
(-- movesDict: ) =only movesDict == % _d_PrintDict
(TEST TestAutoMove.1) { highestMove } { (a1a8) } _s_p1_p2_AssertEqual
restore
CS_POP
} def
/TestAutoMove2 {
(TestAutoMove2) CS_PUT
save
NewGame
/b (\
........\
.p......\
r.......\
........\
........\
........\
........\
Q.......\
) _s_Dup_s_ def
%GameDict (BOARD) b put
(white) b false _player_board_useMinMax_FindBestMove_bestTo_bestFrom_bestScore_
/bestScore exch def
/bestFrom exch def
/bestTo exch def
%(-- bestScore: ) =only bestScore ==
%(-- bestFrom: ) =only bestFrom ==
%(-- bestTo: ) =only bestTo ==
(TEST TestAutoMove.2.1) { bestTo } { (a6) } _s_p1_p2_AssertNotEqual
restore
CS_POP
} def
/TestAutoMove3 {
(TestAutoMove3) CS_PUT
save
NewGame
/b (\
........\
.P......\
........\
........\
........\
......r.\
.......P\
k......K\
) _s_Dup_s_ def
%GameDict (BOARD) b put
(white) b false _player_board_useMinMax_FindBestMove_bestTo_bestFrom_bestScore_
/bestScore exch def
/bestFrom exch def
/bestTo exch def
(-- bestScore: ) =only bestScore ==
(-- bestFrom: ) =only bestFrom ==
(-- bestTo: ) =only bestTo ==
(TEST TestAutoMove.2.3) { bestTo } { (b8) } _s_p1_p2_AssertEqual
restore
CS_POP
} def
/TestEndingPhase {
(TestEndingPhase) CS_PUT
save
NewGame
/b (\
........\
r...q...\
.....P..\
........\
........\
........\
........\
....k..K\
) _s_Dup_s_ def
(TEST TestEndingPhase.1) { GameState (ENDING_HAS_BEGUN) get } _s_proc_AssertFalse
%(--1) ==
%b _board_MaterialValue_white_black_ ==
%== ==
b (f6) (e7) false _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
/msg exch def
/validMoves exch def
/canMove exch def
%(--2) ==
%b _board_MaterialValue_white_black_ ==
%== ==
(TEST TestEndingPhase.2) { GameState (ENDING_HAS_BEGUN) get } _s_proc_AssertTrue
restore