-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogic_chess.ps
More file actions
executable file
·1349 lines (984 loc) · 38.5 KB
/
logic_chess.ps
File metadata and controls
executable file
·1349 lines (984 loc) · 38.5 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
(logic_board.ps) run
% moves array contains:
% null - cannot move there
% 1 - can move there
% 2 - error
% 3 - origin
/NewGame {
/GameState
<<
(BOARD) ()
(CURRENT_PLAYER) (white)
(LAST_MOVE) ()
(WHITE_DID_CASTLE) false
(BLACK_DID_CASTLE) false
(CAPTURED_BY_BLACK) (................) _s_Dup_s_
(CAPTURED_BY_WHITE) (................) _s_Dup_s_
(LAST_MESSAGE) ()
(ENDING_HAS_BEGUN) false
(EN_PASSANT_WHITE_CANDIDATE) null
(EN_PASSANT_BLACK_CANDIDATE) null
(PLAYER_TURN_MESSAGE) ()
>> def
GameState (BOARD) CreateBoard_b_ put
GameState (LAST_PLAYER) ( ) _s_Dup_s_ put
GameState (LAST_MOVE) ( ) _s_Dup_s_ put
GameState (LAST_MESSAGE) (white turn ) _s_Dup_s_ put
GameState (ENDING_HAS_BEGUN) false put
} def
/PrintBoardAndGameState {
(/PrintBoardAndGameState) CS_PUT
2 dict begin
/b GameState (BOARD) get def
()=
b 0 8 getinterval =only ( )=only GameState (LAST_MOVE) get =
b 8 8 getinterval =only ( )=only GameState (BOARD) get _b_Eval_score_ =
b 16 8 getinterval =
b 24 8 getinterval =only ( )=only GameState (CAPTURED_BY_BLACK) get =
b 32 8 getinterval =only ( )=only GameState (CAPTURED_BY_WHITE) get =
b 40 8 getinterval =
b 48 8 getinterval =only ( )=only GameState (LAST_MESSAGE) get =
b 56 8 getinterval =only ( )=only GameState (PLAYER_TURN_MESSAGE) get =
()=
end
CS_POP
} def
/_p_a_AddToCaptured {
(/_p_a_AddToCaptured) CS_PUT
5 dict begin
/a exch def
/p exch def
0 1 a length {
/i exch def
%a i 1 getinterval ==
a i 1 getinterval (.) eq {
a i p _s_Dup_s_ putinterval
exit
} if
} for
end
CS_POP
} def
/ChangePlayer_playerColor_ {
(/ChangePlayer_playerColor_) CS_PUT
/s { GameState (CURRENT_PLAYER) get _whiteOrBlack_Invert_s_ } def
GameState (CURRENT_PLAYER) s put
GameState (CURRENT_PLAYER) get
CS_POP
} def
/_board_index_IsWhite_b_ {
(/_board_index_IsWhite_b_) CS_PUT
exch _board_index_Piece_p_ _piece_IsWhite_b_
CS_POP
} def
/_board_index_IsBlack_b_ {
(/_board_index_IsBlack_b_) CS_PUT
exch _board_index_Piece_p_ _piece_IsBlack_b_
CS_POP
} def
/_board_index_IsEmpty_b_ {
(/_board_index_IsEmpty_b_) CS_PUT
exch _board_index_Piece_p_ _piece_IsEmpty_b_
CS_POP
} def
% [null 1 null 2 1] -> [1 4]
/_movesIntsOrNulls_MovesList_a_ {
(/_movesIntsOrNulls_MovesList_a_) CS_PUT
5 dict begin
/m exch def
/a m length array def
/n 0 def % count number of 'true'
0 1 m length 1 sub {
/i exch def
/o m i get def
o 1 eq {
a n i put
/n n 1 add def
} if
} for
a 0 n getinterval
end
CS_POP
} def
/_board_indexFrom_indexTo_AreOpponents_b_ {
(/_board_indexFrom_indexTo_AreOpponents_b_) CS_PUT
12 dict begin
/ret false def
/toIndex exch def
/fromIndex exch def
/board exch def
/fromPiece board fromIndex _board_index_Piece_p_ def
/toPiece board toIndex _board_index_Piece_p_ def
/fromIsWhite fromPiece _piece_IsWhite_b_ def
/toIsWhite toPiece _piece_IsWhite_b_ def
/fromIsBlack fromPiece _piece_IsBlack_b_ def
/toIsBlack toPiece _piece_IsBlack_b_ def
fromIsWhite toIsBlack and fromIsBlack toIsWhite and or % return value
end
CS_POP
} def
/_board_indexFrom_indexTo_AreSameColor_b_ {
(_board_indexFrom_indexTo_AreSameColor_b_) CS_PUT
12 dict begin
/toIndex exch def
/fromIndex exch def
/board exch def
/fromPiece board fromIndex _board_index_Piece_p_ def
/toPiece board toIndex _board_index_Piece_p_ def
/fromIsWhite fromPiece _piece_IsWhite_b_ def
/toIsWhite toPiece _piece_IsWhite_b_ def
/fromIsBlack fromPiece _piece_IsBlack_b_ def
/toIsBlack toPiece _piece_IsBlack_b_ def
fromIsWhite toIsWhite and fromIsBlack toIsBlack and or % return value
end
CS_POP
} def
/DirectionsForPiece
<<
/N -8 def
/E 1 def
/S 8 def
/W -1 def
(P) [N N N add N W add N E add]
(p) [S S S add S E add S W add]
(n) [N N E add add
E N E add add
E S E add add
S S E add add
S S W add add
W S W add add
W N W add add
N N W add add]
(b) [N E add S E add S W add N W add]
(r) [N E S W]
(q) [N E S W N E add S E add S W add N W add]
(k) [N E S W N E add S E add S W add N W add]
(.) []
>> def
/_board_coord_ValidMoves_a_ {
(/_board_coord_ValidMoves_a_) CS_PUT
/legalMoves 64 array def
8 dict begin
/fromCoord exch def
/board exch def
/fromIndex fromCoord _coord_Index_i_ def
/p board fromIndex 1 getinterval def
DirectionsForPiece p p (P) ne { _p_DupLowerCase_p_ } if get
% directions on stack
{
/offset exch def
% if offset < 0, iterate towards 0
% if offset > 0, iterate toward 63
/lastIndex offset 0 gt { 63 } { 0 } ifelse def
/currentCol fromIndex _index_Col_a-h_ def
/currentRow fromIndex _index_Row_1-8_ def
fromIndex offset lastIndex {
/toIndex exch def
fromIndex toIndex eq not {
% don't go out of board
toIndex _index_IsInBoardRange_b_ not {
exit
} if
% stop moving when over- or under-flowing board edges, including knights
/toCol toIndex _index_Col_a-h_ def
/toRow toIndex _index_Row_1-8_ def
currentCol (a) eq toCol (g) eq and { exit } if
currentCol (a) eq toCol (h) eq and { exit } if
currentCol (b) eq toCol (h) eq and { exit } if
currentRow (1) eq toRow (8) eq and { exit } if
currentCol (g) eq toCol (a) eq and { exit } if
currentCol (h) eq toCol (a) eq and { exit } if
currentCol (h) eq toCol (b) eq and { exit } if
currentRow (8) eq toRow (1) eq and { exit } if
p (p) eq p (P) eq or {
% pawns cannot move sideways if they don't capture, unless it's en-passant
currentCol toCol ne {
/isEnPassantWhite false def
/whiteEP GameState (EN_PASSANT_WHITE_CANDIDATE) get def
whiteEP null ne {
toRow 3 eq toCol whiteEP _coord_Index_i_ _index_Col_a-h_ eq and {
/isEnPassantWhite true def
} if
} if
/isEnPassantBlack false def
/blackEP GameState (EN_PASSANT_BLACK_CANDIDATE) get def
blackEP null ne {
toRow 6 eq toCol blackEP _coord_Index_i_ _index_Col_a-h_ eq and {
/isEnPassantBlack true def
} if
} if
/isEnPassant isEnPassantWhite isEnPassantBlack or def
board fromIndex toIndex _board_indexFrom_indexTo_AreOpponents_b_ not isEnPassant not and {
exit
} if
} if
% pawns cannot move +2 if not on their initial raw and +1 square being free
toRow currentRow sub abs 2 eq {
/pIsWhite p (P) eq def
currentRow pIsWhite {2}{7} ifelse ne { exit } if
board fromIndex 8 pIsWhite {sub}{add} ifelse _board_index_Piece_p_ _piece_IsEmpty_b_ not { exit } if
} if
% pawns cannot more forward if square is not empty
currentCol toCol eq {
board toIndex _board_index_Piece_p_ _piece_IsEmpty_b_ not {
exit
} if
} if
} if
board fromIndex toIndex _board_indexFrom_indexTo_AreSameColor_b_ {
exit
} if
%(***** add to legal moves: ) ==only toIndex ==
legalMoves toIndex 1 put
% if to opponent, stop sliding
board fromIndex toIndex _board_indexFrom_indexTo_AreOpponents_b_ {
exit
} if
% exit if is not a sliding piece
(brq) p _p_DupLowerCase_p_ search {
% (-- SLIDING PIECE, CONTINUE: ) ==only p ==
pop pop pop
} {
% (-- NOT SLIDING PIECE, EXIT: ) ==only p ==
pop
exit
} ifelse
/currentCol toIndex _index_Col_a-h_ def
/currentRow toIndex _index_Row_1-8_ def
} if
} for
} forall
% castling
% castling through checked squares is prevented in _board_fc_tc_dry_Move_canMove_squaresStatus_msg_
GameState (BLACK_DID_CASTLE) get not p (k) eq and fromCoord (e8) eq and {
fromCoord (e8) eq board 4 4 getinterval (k..r) eq and { legalMoves fromIndex 2 add 1 put } if
fromCoord (e8) eq board 0 5 getinterval (r...k) eq and { legalMoves fromIndex -2 add 1 put } if
} if
GameState (WHITE_DID_CASTLE) get not p (K) eq and fromCoord (e1) eq and {
fromCoord (e1) eq board 60 4 getinterval (K..R) eq and { legalMoves fromIndex 2 add 1 put } if
fromCoord (e1) eq board 56 5 getinterval (R...K) eq and { legalMoves fromIndex -2 add 1 put } if
} if
%(-- legalMoves: ) ==only legalMoves ==
end
legalMoves
CS_POP
} def
/_board_MaterialValue_white_black_ {
(/_board_MaterialValue_white_black_) CS_PUT
5 dict begin
/b exch def
/whiteValue 0 def
/blackValue 0 def
0 1 63 {
/i exch def
/p b i 1 getinterval def
WhitePieceValueDict p known {
/whiteValue whiteValue WhitePieceValueDict p get add def
} if
BlackPieceValueDict p known {
/blackValue blackValue BlackPieceValueDict p get add def
} if
} for
whiteValue
blackValue
end
CS_POP
} def
/_board_fc_tc_dry_Move_canMove_squaresStatus_msg_ {
(/_board_fc_tc_dry_Move_canMove_squaresStatus_msg_) CS_PUT
/canMove true def
/msg (-) def
/didActuallyMove false def
10 dict begin
/isDry exch def
/toCoord exch def
/fromCoord exch def
/brd exch def
fromCoord _coord_Index_i_ _index_IsInBoardRange_b_ not
toCoord _coord_Index_i_ _index_IsInBoardRange_b_ not or {
%(-- coords out of range: ) ==only fromCoord == toCoord ==
false % can move
64 array % statuses
(bad coords) % msg
false % didActuallyMove
quit
} if
/fromPiece brd fromCoord _board_coord_Piece_p_ def
/toPiece brd toCoord _board_coord_Piece_p_ def
/fromIndex { fromCoord _coord_Index_i_ } def
%fromIndex AssertTypeInt
/toIndex { toCoord _coord_Index_i_ } def
%toIndex AssertTypeInt
/a brd fromCoord _board_coord_ValidMoves_a_ def
/i toCoord _coord_Index_i_ def
a fromCoord _coord_Index_i_ 3 put % origin
{
% if toIndex is not part of valid moves, or fromCoord == toCoord, ...
a i get null eq fromCoord toCoord eq or {
/canMove false def
/msg (invalid move) def
a toCoord _coord_Index_i_ 2 put % 2 is red
exit
} if
GameState (CURRENT_PLAYER) get (white) _a1_a2_HaveEqualContents_b_ fromPiece _piece_IsBlack_b_ and {
/canMove false def
/msg (ERROR: white turn) def
exit
} if
GameState (CURRENT_PLAYER) get (black) _a1_a2_HaveEqualContents_b_ fromPiece _piece_IsWhite_b_ and {
/canMove false def
/msg (ERROR: black turn) def
exit
} if
isDry { exit} if
/futureBoard brd _s_Dup_s_ def
%----- detect checks
% actual move on futureBoard, used to detect checks ahead
futureBoard toIndex futureBoard fromCoord _board_coord_Piece_p_ putinterval
futureBoard fromIndex (.) putinterval
% prevent white from moving when checked
fromPiece _piece_IsWhite_b_ futureBoard (white) _board_whiteOrBlack_KingIsChecked_b_ and {
/canMove false def
/msg (white checked) def
a toCoord _coord_Index_i_ 2 put % 2 is red
exit
} if
% prevent black from moving when checked
fromPiece _piece_IsBlack_b_ futureBoard (black) _board_whiteOrBlack_KingIsChecked_b_ and {
/canMove false def
/msg (black checked) def
a toCoord _coord_Index_i_ 2 put % 2 is red
exit
} if
%----- manage captures
/a 64 array def
a fromCoord _coord_Index_i_ 3 put % origin
a toCoord _coord_Index_i_ 1 put % destination
/fromPiece brd fromCoord _board_coord_Piece_p_ def
/toPiece brd toCoord _board_coord_Piece_p_ def
/doCapture
fromPiece _piece_IsBlack_b_ toPiece _piece_IsWhite_b_ and
fromPiece _piece_IsWhite_b_ toPiece _piece_IsBlack_b_ and
or def
doCapture {
fromPiece _piece_IsBlack_b_ { toPiece GameState (CAPTURED_BY_BLACK) get _p_a_AddToCaptured } if
fromPiece _piece_IsWhite_b_ { toPiece GameState (CAPTURED_BY_WHITE) get _p_a_AddToCaptured } if
} if
%----- add 'en passant' moves
/fromCol fromCoord _coord_Index_i_ _index_Col_a-h_ def
/toCol toCoord _coord_Index_i_ _index_Col_a-h_ def
/toRow toCoord _coord_Index_i_ _index_Row_1-8_ def
[[(EN_PASSANT_BLACK_CANDIDATE) (CAPTURED_BY_WHITE) (P) 6]
[(EN_PASSANT_WHITE_CANDIDATE) (CAPTURED_BY_BLACK) (p) 4]] {
/candidateKey_captureKey_fromPiece exch def
/candidateKey candidateKey_captureKey_fromPiece 0 get def
/captureKey candidateKey_captureKey_fromPiece 1 get def
/fromPawn candidateKey_captureKey_fromPiece 2 get def
/toRowTarget candidateKey_captureKey_fromPiece 3 get def
/candidate GameState candidateKey get def
candidate null ne fromPiece fromPawn eq and {
/candidateCol candidate _coord_Index_i_ _index_Col_a-h_ def
/candidateRow candidate _coord_Index_i_ _index_Row_1-8_ def
candidate 0 get fromCol 0 get sub abs 1 eq
toRow toRowTarget eq and
toCol candidateCol eq and {
brd candidate _board_coord_Piece_p_ GameState captureKey get _p_a_AddToCaptured
brd candidate _coord_Index_i_ (.) putinterval
a fromCoord _coord_Index_i_ 3 put % origin
a toCoord _coord_Index_i_ 1 put % destination
} if
} if
} forall
%----- prepare en passant candidate for next move
GameState (EN_PASSANT_WHITE_CANDIDATE) null put
GameState (EN_PASSANT_BLACK_CANDIDATE) null put
fromPiece (p) eq
fromCoord _coord_Index_i_ _index_Row_1-8_ 7 eq and
toCoord _coord_Index_i_ _index_Row_1-8_ 5 eq and {
GameState (EN_PASSANT_BLACK_CANDIDATE) toCoord put
GameState (EN_PASSANT_WHITE_CANDIDATE) null put
} if
fromPiece (P) eq
fromCoord _coord_Index_i_ _index_Row_1-8_ 2 eq and
toCoord _coord_Index_i_ _index_Row_1-8_ 4 eq and {
GameState (EN_PASSANT_WHITE_CANDIDATE) toCoord put
GameState (EN_PASSANT_BLACK_CANDIDATE) null put
} if
%----- castling
% TODO: refactor..
/JustCastled false def
GameState (BLACK_DID_CASTLE) get not fromPiece (k) eq and fromCoord (e8) eq and toCoord (c8) eq and {
brd (black) [2 3 4] _board_whiteOrBlack_indices_IndicesContainOneChecked_b_ not {
brd 0 (..kr) putinterval
GameState (BLACK_DID_CASTLE) true put
/JustCastled true def
} {
/canMove false def
/msg (ERR: black checked) def
exit
} ifelse
} if
GameState (BLACK_DID_CASTLE) get not fromPiece (k) eq and fromCoord (e8) eq and toCoord (g8) eq and {
brd (black) [4 5 6] _board_whiteOrBlack_indices_IndicesContainOneChecked_b_ not {
brd 4 (rk..) putinterval
GameState (BLACK_DID_CASTLE) true put
/JustCastled true def
} {
/canMove false def
/msg (ERR: black checked) def
exit
} ifelse
} if
GameState (WHITE_DID_CASTLE) get not fromPiece (K) eq and fromCoord (e1) eq and toCoord (g1) eq and {
brd (white) [60 61 62] _board_whiteOrBlack_indices_IndicesContainOneChecked_b_ not {
brd 60 (.RK.) putinterval
GameState (WHITE_DID_CASTLE) true put
/JustCastled true def
} {
/canMove false def
/msg (ERR: white checked) def
exit
} ifelse
} if
GameState (WHITE_DID_CASTLE) get not fromPiece (K) eq and fromCoord (e1) eq and toCoord (c1) eq and {
brd (white) [58 59 60] _board_whiteOrBlack_indices_IndicesContainOneChecked_b_ not {
brd 56 (..KR) putinterval
GameState (WHITE_DID_CASTLE) true put
/JustCastled true def
} {
/canMove false def
/msg (ERR: white checked) def
exit
} ifelse
} if
%----- actual move on actual board
JustCastled not {
/p GameState (BOARD) get fromCoord _board_coord_Piece_p_ def
brd toIndex p putinterval
brd fromIndex (.) putinterval
} if
%----- pawn promotion
/toPiece brd toCoord _board_coord_Piece_p_ def
toPiece (P) eq toIndex _index_Row_1-8_ 8 eq and {
brd toIndex (Q) putinterval
} if
toPiece (p) eq toIndex _index_Row_1-8_ 1 eq and {
brd toIndex (q) putinterval
} if
/didActuallyMove true def
GameState (BOARD) brd _s_Dup_s_ put
%----- update ENDING_HAS_BEGUN flag
% The ending begins if:
% 1. Both sides have no queens or
% 2. Every side which has a queen has additionally no other pieces or one minorpiece maximum.
/whiteQueenOnBoard brd (Q) _board_piece_PieceIndex_i_ null ne def
/blackQueenOnBoard brd (q) _board_piece_PieceIndex_i_ null ne def
/noMoreQueens whiteQueenOnBoard not blackQueenOnBoard not and def
brd _board_MaterialValue_white_black_
/blackValue exch def
/whiteValue exch def
/endingHasBegun
whiteQueenOnBoard 21000 whiteValue le and
blackQueenOnBoard 21000 blackValue le and or
noMoreQueens or def
GameState (ENDING_HAS_BEGUN) endingHasBegun put
exit
} loop
didActuallyMove {
/s ( ) def
s 0 GameState (CURRENT_PLAYER) get putinterval
s 6 fromCoord putinterval
s 9 toCoord putinterval
GameState (LAST_MOVE) s put
} {
/s ( ) def
s 5 fromCoord putinterval
s 8 toCoord putinterval
GameState (LAST_MOVE) s put
} ifelse
GameState (LAST_MESSAGE) msg put
canMove
a
msg
didActuallyMove
end
% didActuallyMove
{ ChangePlayer_playerColor_ pop } if
/playerTurnMessage ( ) def
playerTurnMessage 0 (xxxxx turn) putinterval
playerTurnMessage 0 GameState (CURRENT_PLAYER) get putinterval
GameState (PLAYER_TURN_MESSAGE) playerTurnMessage put
CS_POP
} def
/_board_whiteOrBlack_LegalMoves_d_ {
(/_board_whiteOrBlack_LegalMoves_d_) CS_PUT
% returns a dict such as {(a2): [null 1 1 null ...], }
15 dict begin
/whiteOrBlack exch def
/board exch def
/legalMovesDict << >> def
whiteOrBlack (black) eq not whiteOrBlack (white) eq not and {
(-- bad param, need 'white' or 'black') == quit
} if
/whiteTrueBlackFalse whiteOrBlack (white) _a1_a2_HaveEqualContents_b_ def
% iterate board
0 1 63 {
/i exch def
% for each piece
/p board i 1 getinterval def
/fc i _index_Coord_c_ def
% if piece has the color we're looking for
p _piece_IsWhite_b_ whiteTrueBlackFalse and
p _piece_IsBlack_b_ whiteTrueBlackFalse not and
or {
/moves board fc _board_coord_ValidMoves_a_ def
moves {
% move on stack
1 eq {
legalMovesDict fc moves put
exit
} if
} forall
} if
} for
legalMovesDict
% (legalMoves:) ==
% legalMoves {
% /v exch def
% /k exch def
% ( k:) ==only k ==
% ( v:) ==only v ==
% } forall
end
CS_POP
} def
/_whiteOrBlack_Invert_s_ {
(/_whiteOrBlack_Invert_s_) CS_PUT
/wb exch def
{
wb (white) eq { (black) exit } if
wb (black) eq { (white) exit } if
(-- bad param, need 'white' or 'black') == quit
} loop
CS_POP
} def
/_board_whiteOrBlack_index_IndexIsChecked_b_ {
% tells if a square is checked from player perspective
(/_board_whiteOrBlack_index_IndexIsChecked_b_) CS_PUT
5 dict begin
/idx exch def
/whiteOrBlack exch def
/board exch def
%board ==
%whiteOrBlack ==
%[idx] ==
board whiteOrBlack [ idx ] _board_whiteOrBlack_indices_IndicesContainOneChecked_b_
end
CS_POP
} def
/_board_whiteOrBlack_indices_IndicesContainOneChecked_b_ {
(/_board_whiteOrBlack_indices_IndicesContainOneChecked_b_) CS_PUT
10 dict begin
/indices exch def
/whiteOrBlack exch def
/board exch def
whiteOrBlack (white) _a1_a2_HaveEqualContents_b_ not whiteOrBlack (black) _a1_a2_HaveEqualContents_b_ not and {
(-- bad param, need 'white' or 'black') == quit
} if
/opponentMovesDict board whiteOrBlack _whiteOrBlack_Invert_s_ _board_whiteOrBlack_LegalMoves_d_ def
/indicesContainOneChecked false def
opponentMovesDict {
/v exch def
/k exch def
indices {
/ii exch def
v ii get 1 eq {
/indicesContainOneChecked true def
exit exit
} if
} forall
} forall
indicesContainOneChecked
end
CS_POP
} def
/_board_whiteOrBlack_KingIsChecked_b_ {
(/_board_whiteOrBlack_KingIsChecked_b_) CS_PUT
5 dict begin
/whiteOrBlack exch def
/board exch def
whiteOrBlack (white) _a1_a2_HaveEqualContents_b_ not whiteOrBlack (black) _a1_a2_HaveEqualContents_b_ not and {
(-- bad param, need 'white' or 'black') == quit
} if
/ownKing whiteOrBlack (white) _a1_a2_HaveEqualContents_b_{(K)}{(k)}ifelse def
/ownKingIndex { board ownKing _board_piece_PieceIndex_i_ } def
ownKingIndex null ne {
board whiteOrBlack ownKingIndex _board_whiteOrBlack_index_IndexIsChecked_b_
} {
%(WARNING: ) =only whiteOrBlack =only ( king not found) =
false
}
ifelse
end
CS_POP
} def
% https://www.chessprogramming.org/Simplified_Evaluation_Function
/WhitePieceValueDict
<<
(P) 100
(N) 320
(B) 330
(R) 500
(Q) 900
(K) 20000
(.) 0
>> def
/BlackPieceValueDict << >> def
/WhitePieceSquareDict
<<
(P) [ 0 0 0 0 0 0 0 0
50 50 50 50 50 50 50 50
10 10 20 30 30 20 10 10
5 5 10 25 25 10 5 5
0 0 0 20 20 0 0 0
5 -5 -10 0 0 -10 -5 5
5 10 10 -20 -20 10 10 5
0 0 0 0 0 0 0 0 ]
(N) [ -50 -40 -30 -30 -30 -30 -40 -50
-40 -20 0 0 0 0 -20 -40
-30 0 10 15 15 10 0 -30
-30 5 15 20 20 15 5 -30
-30 0 15 20 20 15 0 -30
-30 5 10 15 15 10 5 -30
-40 -20 0 5 5 0 -20 -40
-50 -40 -30 -30 -30 -30 -40 -50 ]
(B) [ -20 -10 -10 -10 -10 -10 -10 -20
-10 0 0 0 0 0 0 -10
-10 0 5 10 10 5 0 -10
-10 5 5 10 10 5 5 -10
-10 0 10 10 10 10 0 -10
-10 10 10 10 10 10 10 -10
-10 5 0 0 0 0 5 -10
-20 -10 -10 -10 -10 -10 -10 -20 ]
(R) [ 0 0 0 0 0 0 0 0
5 10 10 10 10 10 10 5
-5 0 0 0 0 0 0 -5
-5 0 0 0 0 0 0 -5
-5 0 0 0 0 0 0 -5
-5 0 0 0 0 0 0 -5
-5 0 0 0 0 0 0 -5
0 0 0 5 5 0 0 0 ]
(Q) [ -20 -10 -10 -5 -5 -10 -10 -20
-10 0 0 0 0 0 0 -10
-10 0 5 5 5 5 0 -10
-5 0 5 5 5 5 0 -5
0 0 5 5 5 5 0 -5
-10 5 5 5 5 5 0 -10
-10 0 5 0 0 0 0 -10
-20 -10 -10 -5 -5 -10 -10 -20 ]
(K) [ -30 -40 -40 -50 -50 -40 -40 -30
-30 -40 -40 -50 -50 -40 -40 -30
-30 -40 -40 -50 -50 -40 -40 -30
-30 -40 -40 -50 -50 -40 -40 -30
-20 -30 -30 -40 -40 -30 -30 -20
-10 -20 -20 -20 -20 -20 -20 -10
20 20 0 0 0 0 20 20
20 30 10 0 0 10 30 20 ]
% used for king at the end of the game
(X) [ -50 -40 -30 -20 -20 -30 -40 -50
-30 -20 -10 0 0 -10 -20 -30
-30 -10 20 30 30 20 -10 -30
-30 -10 30 40 40 30 -10 -30
-30 -10 30 40 40 30 -10 -30
-30 -10 20 30 30 20 -10 -30
-30 -30 0 0 0 0 -30 -30
-50 -30 -30 -30 -30 -30 -30 -50 ]
>> def
/BlackPieceSquareDict <<>> def
/initPiecesDicts {
(/initPieceSquareDicts) CS_PUT
WhitePieceValueDict {
/v exch def
/k exch def
BlackPieceValueDict k ( ) cvs _p_DupLowerCase_p_ v put
} forall
WhitePieceSquareDict {
/v exch def
/k exch def
BlackPieceSquareDict k ( ) cvs _p_DupLowerCase_p_ v _a_DupReversed_a_ put
} forall
CS_POP
} def
initPiecesDicts
/_b_Eval_score_ {
(/_b_Eval_score_) CS_PUT
/b exch def
5 dict begin
/totalScore 0 def
0 1 63 {
/i exch def
/p b i 1 getinterval def
GameState (ENDING_HAS_BEGUN) get {
p (k) eq { /p (x) def } if
p (K) eq { /p (X) def } if
} if
p (.) ne {
%(-- p: ) =only p =only ( i: ) =only i ==
/whiteValueScore WhitePieceValueDict p known { WhitePieceValueDict p get } { 0 } ifelse def
/whiteSquareScore WhitePieceSquareDict p known { WhitePieceSquareDict p get i get } { 0 } ifelse def
/blackValueScore BlackPieceValueDict p known { BlackPieceValueDict p get } { 0 } ifelse def
/blackSquareScore BlackPieceSquareDict p known { BlackPieceSquareDict p get i get } { 0 } ifelse def
/totalScore totalScore whiteValueScore add whiteSquareScore add blackValueScore sub blackSquareScore sub def
} if