forked from fuzzball-muck/fuzzball-muf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-chess.muf
1869 lines (1628 loc) · 48.5 KB
/
game-chess.muf
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
@prog game-chess.muf
1 99999 d
1 i
( An MCP-aware game of chess. )
lvar piecenames
$def }join }list "" array_join
$def CHESS_PKGNAME "org-fuzzball-chess"
( Game Data management -----------------------------)
: gamedata_get[ str:game str:key -- any:val ]
prog { "_gamedata/game-" game @ "/" key @ }join
getprop
;
: gamedata_set[ str:game str:key any:val -- ]
prog { "_gamedata/game-" game @ "/" key @ }join
val @ setprop
;
: gamedata_del[ str:game str:key -- ]
prog { "_gamedata/game-" game @ "/" key @ }join
remove_prop
;
: gamedata_listget[ str:game str:key -- list:val ]
prog { "_gamedata/game-" game @ "/" key @ }join
array_get_proplist
;
: gamedata_listset[ str:game str:key list:val -- ]
prog { "_gamedata/game-" game @ "/" key @ }join
over over "#" strcat remove_prop
val @ array_put_proplist
;
: gamedata_listdel[ str:game str:key -- ]
game @ key @ { }list gamedata_listset
prog { "_gamedata/game-" game @ "/" key @ "#" }join
remove_prop
;
: gamedata_reflistget[ str:game str:key -- list:val ]
prog { "_gamedata/game-" game @ "/" key @ }join
array_get_reflist
;
: gamedata_reflistset[ str:game str:key list:val -- ]
prog { "_gamedata/game-" game @ "/" key @ }join
val @ array_put_reflist
;
: gamedata_reflistdel[ str:game str:key -- ]
prog { "_gamedata/game-" game @ "/" key @ }join
remove_prop
;
: gamedata_clear[ str:game -- ]
prog { "_gamedata/game-" game @ "/" }join
begin
over swap nextprop
dup while
over over remove_prop
repeat
pop pop
;
( Player Data management -----------------------------)
: playerdata_get[ ref:who str:game str:key -- any:val ]
game @ { "player" who @ "/" key @ }join gamedata_get
;
: playerdata_set[ ref:who str:game str:key any:val -- ]
game @ { "player" who @ "/" key @ }join val @ gamedata_set
;
: playerdata_del[ ref:who str:game str:key -- ]
game @ { "player" who @ "/" key @ }join gamedata_del
;
: playerdata_clear[ ref:who str:game -- ]
prog { "_gamedata/game-" game @ "/player" who @ }join
remove_prop
;
( Team management -----------------------------)
: teams_get[ str:game -- list:teams ]
game @ "teams" gamedata_listget
dup not if pop { }list then
;
: teams_set[ list:teams str:game -- ]
game @ "teams" teams @ gamedata_listset
;
: teams_clear[ str:game -- ]
game @ "teams" gamedata_clear
;
: team_players_get[ str:game str:team -- list:players ]
game @ { "team-" team @ tolower }join
gamedata_reflistget
dup not if pop { }list then
;
: team_players_set[ list:plist str:game str:team -- ]
game @ teams_get
dup not if pop { }list then
{ team @ }list swap array_union
game @ teams_set
game @ { "team-" team @ tolower }join
plist @ gamedata_reflistset
;
: team_players_clear[ str:game str:team -- ]
game @ teams_get
{ team @ }list swap array_diff
game @ teams_set
game @ { "team-" team @ tolower }join
gamedata_reflistdel
;
: team_player_add[ ref:who str:game str:team -- ]
game @ team @ team_players_get
dup not if pop { }list then
{ who @ }list swap array_union
game @ team @ team_players_set
;
: team_player_del[ ref:who str:game str:team -- ]
game @ team @ team_players_get
{ who @ }list swap array_diff dup
array_count if
game @ team @ team_players_set
else
pop game @ team @ team_players_clear
then
;
: player_notify[ ref:who str:game str:text -- ]
who @ text @ notify
who @ descr_array
foreach var! dscr pop
dscr @ CHESS_PKGNAME mcp_supports 0.0 > if
dscr @ CHESS_PKGNAME "info" {
"game" game @
"info" text @
}dict mcp_send
else
dscr @ text @ descrnotify
then
repeat
;
: team_notify[ str:game str:team str:text -- ]
game @ team @ team_players_get
foreach swap pop
game @ text @ player_notify
repeat
;
( Player management -----------------------------)
: players_get[ str:game -- list:plist ]
game @ "players" gamedata_reflistget
dup not if pop { }list then
;
: players_set[ list:plist str:game -- ]
game @ "players" plist @ gamedata_reflistset
;
: players_init[ str:game -- ]
{ }list game @ players_set
;
: players_clear[ str:game -- ]
game @ "players" gamedata_reflistdel
;
: players_notify[ str:game str:mesg -- ]
game @ players_get
foreach var! who pop
who @ game @ mesg @ player_notify
repeat
;
: player_add[ ref:who str:game -- ]
game @ players_get
{ who @ }list array_union
game @ players_set
who @ game @ "team" "" playerdata_set
;
: player_team_get[ ref:who str:game -- str:team ]
who @ game @ "team" playerdata_get
dup not if pop "" then
;
: player_team_set[ ref:who str:game str:team -- ]
who @ game @ player_team_get dup if
who @ game @ rot team_player_del
else pop
then
who @ game @ "team" team @ playerdata_set
who @ game @ team @ team_player_add
;
: player_del[ ref:who str:game -- ]
game @ players_get
{ who @ }list swap array_diff
game @ players_set
who @ game @ player_team_get
who @ game @ rot team_player_del
who @ game @ playerdata_clear
;
( Board Square management -----------------------------)
: board_get[ str:game -- str:board ]
game @ "board" gamedata_get
dup not if
pop "........"
dup strcat
dup strcat
dup strcat
then
;
: board_set[ str:board str:game -- ]
game @ "board" board @ gamedata_set
;
: board_del[ str:game -- ]
game @ "board" gamedata_del
;
: board_setsquare[ str:board int:x int:y str:piece -- str:board' ]
board @
8 y @ - 8 * x @ -- +
strcut 1 strcut swap pop
piece @ dup not if pop "." then
swap strcat strcat
;
: board_getsquare[ str:board int:x int:y -- str:piece ]
board @
8 y @ - 8 * x @ + 1 midstr strip
dup "." strcmp not if pop "" then
;
: board_findpiece[ str:board str:piece -- int:x int:y ]
board @ piece @ instr
-- dup
8 % ++ 8 rot
8 / -
;
: square_set[ str:game int:x int:y str:piece -- ]
game @ board_get
x @ y @ piece @ board_setsquare
game @ board_set
;
: square_get[ str:game int:x int:y -- str:piece ]
game @ board_get
x @ y @ board_getsquare
;
( board position encoding. -----------------------)
: encode_pos[ int:x int:y -- str:pos ]
"ABCDEFGH" x @ 1 midstr
"12345678" y @ 1 midstr strcat
;
: decode_pos[ str:pos -- int:x int:y ]
pos @ strip toupper
dup strlen 2 = not if
"Invalid board position." abort
then
1 strcut
dup number? not if swap then
"12345678" swap instr dup not if
"Invalid board position." abort
then
swap
"ABCDEFGH" swap instr dup not if
"Invalid board position." abort
then
swap
;
: normalize_pos ( str:pos -- str:pos' )
decode_pos encode_pos
;
( Piece management -----------------------------)
: piece_is_white[ str:token -- bool:iswhite ]
token @ if "PNBRQK" token @ instr not not else 0 then
;
: piece_name[ str:token -- str:name ]
( token @ piece_is_white if "White " else "Black " then )
piecenames @ token @ toupper [] ( strcat )
;
: abs ( int:val -- int:val' )
dup 0 < if -1 * then
;
: sign ( int:val -- int:val' )
dup abs /
;
: piece_move_nocheck[ str:board str:from str:to -- str:board' ]
board @ from @ decode_pos
board_getsquare var! piece
board @
from @ decode_pos "" board_setsquare
to @ decode_pos piece @ board_setsquare
;
: piece_check_intervening[ str:board str:from str:to -- int:isok ]
from @ decode_pos var! y var! x
to @ decode_pos var! desty var! destx
begin
destx @ x @ - sign var! dx
desty @ y @ - sign var! dy
x @ dx @ + x !
y @ dy @ + y !
x @ destx @ =
y @ desty @ = and if
1 break
then
board @ x @ y @ board_getsquare if
0 break
then
repeat
;
: piece_capture_allowed[ str:board str:from str:to -- int:isok ]
board @ from @ decode_pos board_getsquare var! piece
board @ to @ decode_pos board_getsquare strip var! target
to @ decode_pos
from @ decode_pos rot swap - rot rot -
var! dx var! dy
dx @ not dy @ not and if 0 exit then
piece @ "B" stringcmp not if
dx @ abs dy @ abs = not if 0 exit then
board @ from @ to @ piece_check_intervening not if 0 exit then
then
piece @ "R" stringcmp not if
dx @ dy @ and if 0 exit then
board @ from @ to @ piece_check_intervening not if 0 exit then
then
piece @ "Q" stringcmp not if
dx @ dy @ and
dx @ abs dy @ abs = not and if 0 exit then
board @ from @ to @ piece_check_intervening not if 0 exit then
then
piece @ "K" stringcmp not if
dx @ abs 1 <= dy @ abs 1 <= and
not if 0 exit then
then
piece @ "N" stringcmp not if
dx @ abs 1 = dy @ abs 2 = and
dx @ abs 2 = dy @ abs 1 = and
or not if 0 exit then
then
piece @ "P" stringcmp not if
dy @ not if
piece @ piece_is_white if
from @ "?4" smatch not if 0 exit then
else
from @ "?5" smatch not if 0 exit then
then
else
piece @ piece_is_white if
dy @ 1 = not if 0 exit then
else
dy @ -1 = not if 0 exit then
then
then
dx @ abs 1 = not if 0 exit then
target @ not if 0 exit then
piece @ piece_is_white not
target @ piece_is_white not
= if 0 exit then
then
target @ if
piece @ piece_is_white not
target @ piece_is_white not = if
0 exit
then
then
( FIXME: Need test for if en-passante pawn moved before )
1
;
: piece_in_check[ str:board str:pos -- int:in_check ]
board @ pos @ decode_pos board_getsquare
dup if piece_is_white else pop -1 then
var! targwhite
1 8 1 for var! y
1 8 1 for var! x
board @ x @ y @ board_getsquare
dup if
piece_is_white targwhite @ = not
targwhite @ -1 = or if
board @ x @ y @ encode_pos pos @
piece_capture_allowed if
1 exit
then
then
else pop
then
repeat
repeat
0
;
: piece_move_leaves_in_check[ str:board str:from str:to str:kingpos -- int:incheck ]
board @ from @ to @ piece_move_nocheck
kingpos @ from @ stringcmp not if to @ kingpos ! then
kingpos @ piece_in_check
;
: piece_move_allowed[ str:game str:board str:from str:to -- int:isok ]
board @ from @ decode_pos board_getsquare var! piece
board @ to @ decode_pos board_getsquare strip var! target
to @ decode_pos
from @ decode_pos rot swap - rot rot -
var! dx var! dy
dx @ not dy @ not and if 0 exit then
piece @ "B" stringcmp not if
dx @ abs dy @ abs = not if 0 exit then
board @ from @ to @ piece_check_intervening not if 0 exit then
then
piece @ "R" stringcmp not if
dx @ dy @ and if 0 exit then
board @ from @ to @ piece_check_intervening not if 0 exit then
then
piece @ "Q" stringcmp not if
dx @ dy @ and
dx @ abs dy @ abs = not and if 0 exit then
board @ from @ to @ piece_check_intervening not if 0 exit then
then
piece @ "K" stringcmp not if
target @ "R" stringcmp not
from @ "E1" stringcmp not
to @ "A1" stringcmp not
to @ "H1" stringcmp not or and
from @ "E8" stringcmp not
to @ "A8" stringcmp not
to @ "H8" stringcmp not or and
or and if
to @ 1 strcut swap
"A" stringcmp not if
board @ from @ "D" 4 pick strcat from @
piece_move_leaves_in_check
board @ from @ "C" 5 pick strcat from @
piece_move_leaves_in_check or
board @ from @ "E" 5 rotate strcat from @
piece_move_leaves_in_check or
if 0 exit then
else
board @ from @ "F" 4 pick strcat from @
piece_move_leaves_in_check
board @ from @ "G" 5 pick strcat from @
piece_move_leaves_in_check or
board @ from @ "E" 5 rotate strcat from @
piece_move_leaves_in_check or
if 0 exit then
then
game @ from @ "moved" strcat
gamedata_get if 0 exit then
game @ to @ "moved" strcat
gamedata_get if 0 exit then
else
dx @ abs 1 <= dy @ abs 1 <= and
not if 0 exit then
then
then
piece @ "N" stringcmp not if
dx @ abs 1 = dy @ abs 2 = and
dx @ abs 2 = dy @ abs 1 = and
or not if 0 exit then
then
piece @ "P" stringcmp not if
dy @ not if
piece @ piece_is_white if
from @ "?4" smatch not if 0 exit then
else
from @ "?5" smatch not if 0 exit then
then
else
piece @ piece_is_white if
dy @ 2 = if
from @ "?2" smatch not if 0 exit then
else
dy @ 1 = not if 0 exit then
then
else
dy @ -2 = if
from @ "?7" smatch not if 0 exit then
else
dy @ -1 = not if 0 exit then
then
then
then
dx @ abs 1 > if 0 exit then
dx @ abs 1 = if
target @ not if 0 exit then
piece @ piece_is_white not
target @ piece_is_white not
= if 0 exit then
then
then
target @ if
piece @ piece_is_white not
target @ piece_is_white not = if
(castling)
piece @ "K" stringcmp not
target @ "R" stringcmp not and
from @ "E1" stringcmp not
to @ "A1" stringcmp not
to @ "H1" stringcmp not or and
from @ "E8" stringcmp not
to @ "A8" stringcmp not
to @ "H8" stringcmp not or and
or and not if 0 exit then
then
then
board @ from @ to @
board @ piece @ piece_is_white
if "K" else "k" then
board_findpiece encode_pos
piece_move_leaves_in_check
if 0 exit then
(
FIXME: Need test for if en-passante pawn moved before
)
1
;
: piece_in_checkmate[ str:game str:board str:pos -- int:mated ]
pos @ decode_pos var! row var! col
board @ col @ row @ board_getsquare var! targpiece
targpiece @ piece_is_white var! iswhite
( is king in check to start with? )
board @ pos @ piece_in_check not if 0 exit then
( can king move out of check? )
-1 1 1 for var! dy
-1 1 1 for var! dx
dx @ dy @ or not if continue then
col @ dx @ +
row @ dy @ +
over 1 < 3 pick 8 > or
over 1 < 3 pick 8 > or or not if
encode_pos var! tmpdest
game @ board @ pos @ tmpdest @ piece_move_allowed if
board @ pos @ tmpdest @ tmpdest @
piece_move_leaves_in_check not if 0 exit then
then
else
pop pop
then
repeat
repeat
( What threats are there? )
{ }list var! threats
{ }list var! mypieces
1 8 1 for var! y
1 8 1 for var! x
x @ y @ encode_pos var! tmppos
board @ x @ y @ board_getsquare
piece_is_white iswhite @ = if
tmppos @ mypieces @
array_appenditem mypieces !
continue
then
board @ tmppos @ pos @
piece_capture_allowed if
tmppos @ threats @
array_appenditem threats !
then
repeat
repeat
( Can we take the threat and leave us out of check? )
threats @ foreach var! threat pop
mypieces @ foreach var! piece pop
board @ piece @ threat @
piece_capture_allowed if
board @ piece @ threat @ pos @
piece_move_leaves_in_check not if 0 exit then
then
repeat
repeat
( Can we block the threat and leave us out of check? )
threats @ foreach threat ! pop
"RBQ" threat @ instr not if continue then
threat @ decode_pos var! threatrow var! threatcol
col @ threatcol @ - sign dx !
row @ threatrow @ - sign dy !
mypieces @ foreach piece ! pop
begin
threatcol @ dx @ + threatcol !
threatrow @ dy @ + threatrow !
threatcol @ col @ =
threatrow @ row @ = or not
while
threatcol @ threatrow @ encode_pos var! targ
game @ board @ piece @ targ @
piece_move_allowed if
board @ piece @ targ @ pos @
piece_move_leaves_in_check not if 0 exit then
then
repeat
repeat
repeat
( We're in checkmate. )
1
;
( Board management -----------------------------)
: board_init[ str:game -- ]
{
{ "R" "N" "B" "Q" "K" "B" "N" "R" }list
{ "P" "P" "P" "P" "P" "P" "P" "P" }list
{ "" "" "" "" "" "" "" "" }list
{ "" "" "" "" "" "" "" "" }list
{ "" "" "" "" "" "" "" "" }list
{ "" "" "" "" "" "" "" "" }list
{ "p" "p" "p" "p" "p" "p" "p" "p" }list
{ "r" "n" "b" "q" "k" "b" "n" "r" }list
}list
foreach swap ++ var! y
foreach swap ++ var! x var! piece
game @ x @ y @ piece @ square_set
repeat
repeat
;
: board_show_mcp[ int:dscr str:game -- ]
dscr @ CHESS_PKGNAME mcp_supports 0.0 > if
dscr @ CHESS_PKGNAME "setboard"
{
"object" game @
"board" game @ board_get
"sequence" game @ "sequence" gamedata_get
"color"
dscr @ descrdbref game @ player_team_get
"whiteplayer"
game @ "white" team_players_get
dup if 0 [] name else pop "" then
"blackplayer"
game @ "black" team_players_get
dup if 0 [] name else pop "" then
"turn"
game @ "turn" gamedata_get
dup not if pop "white" then
}dict mcp_send
then
;
: board_show_ansi[ int:dscr str:game -- ]
( Squares are red and black, alternating. )
( Pieces are white and green. )
( Just-moved piece is underlined? )
dscr @ {
"White: "
game @ "white" team_players_get
dup if 0 [] name else pop "" then
" Black: "
game @ "black" team_players_get
dup if 0 [] name else pop "" then
}join descrnotify
0 var! squarecolor
dscr @ " A B C D E F G H " descrnotify
1 8 1 for 9 swap - var! y
y @ 2 % squarecolor !
y @ intostr "" strcat
var! line
1 8 1 for var! x
game @ x @ y @ square_get var! piece
line @ piece @ toupper
dup not if pop " " then
" " swap over strcat strcat
{ "bold"
piece @ piece_is_white if "white" else "green" then
squarecolor @ if "bg_black" else "bg_red" then
}list "," array_join
textattr
strcat line !
squarecolor @ not squarecolor !
repeat
line @ y @ intostr strcat line !
dscr @ line @ descrnotify
repeat
dscr @ " A B C D E F G H " descrnotify
;
( Plain text mode looks like the following:
------------------------
8 | R [N] B [Q] K [B] N [R]|
7 |[P] P [P] P [P] P [P] P |
6 | [ ] [ ] [ ] [ ]|
5 |[ ] [ ] [ ] [ ] |
4 | [ ] [ ] [ ] [ ]|
3 |[ ] [ ] [ ] [ ] |
2 | p [p] p [p] p [p] p [p]|
1 |[r] n [b] q [k] b [n] r |
------------------------
A B C D E F G H
)
: board_show_ascii[ int:dscr str:game -- ]
0 var! squarecolor
dscr @ {
"White: "
game @ "white" team_players_get
dup if 0 [] name else pop "" then
" Black: "
game @ "black" team_players_get
dup if 0 [] name else pop "" then
}join descrnotify
dscr @ " A B C D E F G H " descrnotify
dscr @ " ------------------------ " descrnotify
1 8 1 for 9 swap - var! y
y @ 2 % squarecolor !
y @ intostr "|" strcat
var! line
1 8 1 for var! x
game @ x @ y @ square_get var! piece
line @
squarecolor @ if " " " " " " else "[" " " "]" then
piece @ not if swap piece ! else swap pop then
piece @ swap strcat strcat
strcat line !
squarecolor @ not squarecolor !
repeat
dscr @ line @ "|" strcat
y @ intostr strcat descrnotify
repeat
dscr @ " ------------------------ " descrnotify
dscr @ " A B C D E F G H " descrnotify
;
: board_show[ ref:who str:game int:do_mcp -- ]
who @ descr_array
foreach var! dscr pop
dscr @ CHESS_PKGNAME mcp_supports 0.0 > if
do_mcp @ if
dscr @ game @ board_show_mcp
then
else
who @ "C" flag? if
dscr @ game @ board_show_ansi
else
dscr @ game @ board_show_ascii
then
then
repeat
;
: board_show_toall[ str:game int:do_mcp -- ]
game @ players_get
foreach var! who pop
who @ game @ do_mcp @ board_show
repeat
;
: piece_move[ int:except ref:who str:game str:from str:to -- ]
game @ board_get var! board
game @ board @ from @ to @
piece_move_allowed not if
who @ game @ "That move would be illegal!" player_notify
who @ descr_array
foreach swap pop
game @ board_show_mcp
repeat
else
0 try
board @ from @ decode_pos board_getsquare var! piece
board @ to @ decode_pos board_getsquare var! takenpiece
catch
who @ swap notify
exit
endcatch
0 var! castling
"" var! rto
"" var! newto
from @ "E8" stringcmp not if
to @ "A8" stringcmp not if
1 castling ! "D8" rto ! "C8" newto !
else
to @ "H8" stringcmp not if
1 castling ! "F8" rto ! "G8" newto !
then
then
else
from @ "E1" stringcmp not if
to @ "A1" stringcmp not if
1 castling ! "D1" rto ! "C1" newto !
else
to @ "H1" stringcmp not if
1 castling ! "F1" rto ! "G1" newto !
then
then
then
then
game @ "sequence" over over gamedata_get ++ gamedata_set
game @ "turn" over over gamedata_get
dup not if pop "" then
"white" stringcmp if "white" else "black" then
gamedata_set
from @ toupper "[AEH][18]" smatch if
game @ from @ "moved" strcat 1 gamedata_set
then
{
who @ name " moves " from @ toupper
" to " to @ toupper "."
}join var! movemesg
game @ players_get
foreach var! player pop
player @ descr_array
foreach var! dscr pop
dscr @ except @ = not if
dscr @ CHESS_PKGNAME mcp_supports 0.0 > if
dscr @ CHESS_PKGNAME "move"
{
"game" game @
"sequence" game @ "sequence" gamedata_get
"from" from @
"to" to @
"whiteplayer"
game @ "white" team_players_get
dup if 0 [] name else pop "" then
"blackplayer"
game @ "black" team_players_get
dup if 0 [] name else pop "" then
"turn" game @ "turn" gamedata_get
}dict mcp_send
then
then
repeat
repeat
castling @ if
{
who @ name " castles on the "
to @ toupper "C*" smatch
if "queen" else "king" then
" side."
}join movemesg !
0 try
game @ to @ decode_pos square_get var! rook
game @ to @ decode_pos "" square_set
game @ rto @ decode_pos rook @ square_set
newto @ to !
catch
who @ swap notify
exit
endcatch
else
piece @ "p" strcmp not if
to @ "*1" smatch if
"q" piece !
then
else
piece @ "P" strcmp not if
to @ "*8" smatch if
"Q" piece !
then
then
then
then
0 try
game @ from @ decode_pos "" square_set
game @ to @ decode_pos piece @ square_set
catch
who @ swap notify
exit
endcatch
game @ board_get board !
game @ {
"Chess> match "
game @
": "
movemesg @
castling @ not
takenpiece @ striplead and if
" "
piece @ piece_name
" takes "
takenpiece @ piece_name
"."
then
piece @ piece_is_white
if "k" else "K" then
board @ swap board_findpiece encode_pos var! kingpos
board @ kingpos @
piece_in_check if " Check!" then
game @ board @ kingpos @
piece_in_checkmate if pop " Checkmate!" then
}join players_notify
game @ 0 board_show_toall
then
;
( Game management -----------------------------)
: games_get ( -- list:games )
prog "_games" array_get_proplist
{ }list array_union (make list items unique)
SORTTYPE_NOCASE_ASCEND array_sort (sort them)
;
: games_set ( list:games -- )
{ }list array_union (make list items unique)
SORTTYPE_NOCASE_ASCEND array_sort (sort them)
prog "_games#" remove_prop
prog "_games" rot array_put_proplist
;