-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess-logic.rkt
More file actions
1604 lines (1411 loc) · 68.5 KB
/
Copy pathchess-logic.rkt
File metadata and controls
1604 lines (1411 loc) · 68.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
#lang typed/racket
(require "uchicago151/uchicago151.rkt")
(require "uchicago151/uc151image.rkt")
(require typed/test-engine/racket-tests)
(require "option.rkt")
(require "loc.rkt")
(define-type PieceType (U 'Pawn 'Bishop 'Knight 'Rook 'King 'Queen))
(define-type Player (U 'Black 'White))
(define-struct Piece
([type : PieceType]
[color : Player]))
(provide PieceType Player (struct-out Piece))
(define-type Square (Option Piece))
(provide Square)
(define-type Board (Listof (Listof Square)))
(provide Board)
(define-struct StdMove
([src : Loc]
[dst : Loc]
[moved : Piece]
[captured : (Option Piece)]))
(provide (struct-out StdMove))
(define-struct CastleMove
([king-src : Loc]
[king-dst : Loc]
[rook-src : Loc]
[rook-dst : Loc]
[moved : Player]))
(provide (struct-out CastleMove))
(define-type PromoteTo (U 'Queen 'Rook 'Bishop 'Knight))
(define-struct PromoMove
([src : Loc]
[dst : Loc]
[moved : Piece]
[captured : (Option Piece)]
[promote-to : PromoteTo]))
(provide PromoteTo (struct-out PromoMove))
(define-type Move (U StdMove CastleMove PromoMove))
(provide Move)
(define-struct Castles
([black-toward-0-0 : Boolean]
[black-toward-0-7 : Boolean]
[white-toward-7-0 : Boolean]
[white-toward-7-7 : Boolean]))
(provide (struct-out Castles))
(define-struct ChessGame
([board : Board]
[turn : Player]
[history : (Listof Move)]
[cas : Castles]))
(provide (struct-out ChessGame))
(define-struct (Pr A B)
([first : A]
[second : B]))
(provide (struct-out Pr))
;; We define values to represent rows in a starting chess board:
(define blackbaserow (list
(Some (Piece 'Rook 'Black))
(Some (Piece 'Knight 'Black))
(Some (Piece 'Bishop 'Black))
(Some (Piece 'Queen 'Black))
(Some (Piece 'King 'Black))
(Some (Piece 'Bishop 'Black))
(Some (Piece 'Knight 'Black))
(Some (Piece 'Rook 'Black))))
(define blackpawnrow (make-list 8 (Some (Piece 'Pawn 'Black))))
(define emptyrow (list 'None 'None 'None 'None
'None 'None 'None 'None))
(: colorswitch (Square -> Square))
;; Toggles the color of the piece if the square contains one.
(define (colorswitch sq)
(match sq
['None 'None]
[(Some (Piece t col)) (if (symbol=? 'Black col)
(Some (Piece t 'White))
(Some (Piece t 'Black)))]))
(check-expect (colorswitch (Some (Piece 'Pawn 'White)))
(Some (Piece 'Pawn 'Black)))
(check-expect (colorswitch (Some (Piece 'King 'Black)))
(Some (Piece 'King 'White)))
(: starting-board : Board)
;; This value represents the starting layout of a chess board,
;; with white at the bottom.
(define starting-board
(list blackbaserow blackpawnrow
emptyrow emptyrow
emptyrow emptyrow
(map colorswitch blackpawnrow)
(map colorswitch blackbaserow)))
(provide starting-board)
(: new-game : ChessGame)
;; Value to represent a new game of chess.
(define new-game
(ChessGame
starting-board
'White
'()
(Castles #t #t #t #t)))
(provide new-game)
(: board-ref (Board Loc -> Square))
;; Returns the contents of the specified square in the specified board.
(define (board-ref b l)
(list-ref (list-ref b (Loc-row l)) (Loc-col l)))
(check-expect (board-ref starting-board (Loc 7 5))
(Some (Piece 'Bishop 'White)))
(check-expect (board-ref starting-board (Loc 1 2))
(Some (Piece 'Pawn 'Black)))
(provide board-ref)
(: list-update (All(T) ((Listof T) Integer T -> (Listof T))))
;; Updates the nth item of the list to something new.
;; Raises an error if index out of bounds.
(define (list-update xs n x)
(cond
[(negative? n) (error "negative index")]
[(empty? xs) (error "index too high")]
[(zero? n) (cons x (rest xs))]
[else (cons (first xs) (list-update (rest xs) (sub1 n) x))]))
(check-expect (list-update (list 1 2 3 4 5) 3 51) (list 1 2 3 51 5))
(check-expect
(list-update (list 'a 'v 'd 'e) 0 "hello") (list "hello" 'v 'd 'e))
(: board-update (Board Loc Square -> Board))
;; Returns a board with the contents of the specified location updated.
(define (board-update b l s)
(list-update b (Loc-row l) (list-update
(list-ref b (Loc-row l)) (Loc-col l) s)))
(check-expect (board-update starting-board
(Loc 2 4)
(Some (Piece 'Queen 'White)))
(list blackbaserow blackpawnrow
(list-update emptyrow 4 (Some (Piece 'Queen 'White)))
emptyrow emptyrow emptyrow
(map colorswitch blackpawnrow)
(map colorswitch blackbaserow)))
(check-expect (board-update starting-board
(Loc 7 2)
(Some (Piece 'King 'Black)))
(list blackbaserow blackpawnrow
emptyrow emptyrow
emptyrow emptyrow
(map colorswitch blackpawnrow)
(list-update
(map colorswitch blackbaserow) 2
(Some (Piece 'King 'Black)))))
(: firstpass (All (A) ((A -> Boolean) (Listof A) -> (Option A))))
;; Returns the first item to pass the test, if there is such an item.
(define (firstpass test as)
(match as
['() 'None]
[(cons a rs) (if (test a) (Some a) (firstpass test rs))]))
(check-expect (firstpass even? (list 3 5 9 17 64 2 5 2)) (Some 64))
(check-expect (firstpass (lambda ([st : String]) (string-ci>? "marry" st))
(list "play" "zeal" "x-ray" "rub" "llama" "sad"))
(Some "llama"))
(check-expect (firstpass zero? (list 1 4 -3 5 9)) 'None)
(: rowpair (Loc Board -> (Listof (Pr Loc Square))))
;; Returns the row of the given location
;; with each entry including info on both the location and its contents.
(define (rowpair l b)
(build-list 8 (lambda ([n : Natural])
(Pr (Loc (Loc-row l) n)
(board-ref b (Loc (Loc-row l) n))))))
(: colpair (Loc Board -> (Listof (Pr Loc Square))))
;; Returns the column of the given location
;; with each entry including info on both the location and its contents.
(define (colpair l b)
(build-list 8 (lambda ([n : Natural])
(Pr (Loc n (Loc-col l)) (board-ref b (Loc n (Loc-col l)))))))
(: /pair (Loc Board -> (Listof (Pr Loc Square))))
;; Returns a list of elements (Pr Loc Square) corresponding to the diagonal
;; of the given location from southwest to northwest.
(define (/pair l b)
(local {(define sum (+ (Loc-row l) (Loc-col l)))}
(if (>= sum 7)
(build-list (- 15 sum)
(lambda ([n : Natural])
(Pr (Loc (- 7 n) (+ (- sum 7) n))
(board-ref b (Loc (- 7 n) (+ (- sum 7) n))))))
(build-list (add1 sum)
(lambda ([n : Natural])
(Pr (Loc (- sum n) n)
(board-ref b (Loc (- sum n) n))))))))
(: \pair (Loc Board -> (Listof (Pr Loc Square))))
;; Returns a list of elements (Pr Loc Square) corresponding to the diagonal
;; of the given location from northwest to southeast.
(define (\pair l b)
(local {(define dif (- (Loc-col l) (Loc-row l)))}
(if (>= dif 0)
(build-list (- 8 dif)
(lambda ([n : Natural])
(Pr (Loc n (+ dif n))
(board-ref b (Loc n (+ dif n))))))
(build-list (+ 8 dif)
(lambda ([n : Natural])
(Pr (Loc (+ (- dif) n) n)
(board-ref b (Loc (+ (- dif) n) n))))))))
(: stoppers ((U 'Bishop 'Rook) Loc Board -> (Listof (Option (Pr Loc Piece)))))
;; Returns in a list the occupied square (if there is one)
;; nearest to the given location in each direction.
;; If input 'Rook, directions are cardinal: (right, below, left, above)
;; If input 'Bishop, directions are diagonal: (NE, SE, SW, NW)
;; The occupied square is given as a (Pr Loc Piece)
(define (stoppers p l b)
(list
(match (firstpass
(lambda ([x : (Pr Loc Square)])
(match (Pr-second x)
['None #f]
[(Some (Piece t c))
(> (Loc-col (Pr-first x)) (Loc-col l))]))
(if (symbol=? 'Rook p) (rowpair l b) (/pair l b)))
['None 'None]
[(Some (Pr lc 'None)) 'None]
[(Some (Pr lc (Some (Piece tp cl)))) (Some (Pr lc (Piece tp cl)))])
(match (firstpass
(lambda ([x : (Pr Loc Square)])
(match (Pr-second x)
['None #f]
[(Some (Piece t c))
(> (Loc-row (Pr-first x)) (Loc-row l))]))
(if (symbol=? 'Rook p) (colpair l b) (\pair l b)))
['None 'None]
[(Some (Pr lc 'None)) 'None]
[(Some (Pr lc (Some (Piece tp cl)))) (Some (Pr lc (Piece tp cl)))])
(match (firstpass
(lambda ([x : (Pr Loc Square)])
(match (Pr-second x)
['None #f]
[(Some (Piece t c))
(< (Loc-col (Pr-first x)) (Loc-col l))]))
(if (symbol=? 'Rook p) (reverse (rowpair l b)) (reverse (/pair l b))))
['None 'None]
[(Some (Pr lc 'None)) 'None]
[(Some (Pr lc (Some (Piece tp cl)))) (Some (Pr lc (Piece tp cl)))])
(match (firstpass
(lambda ([x : (Pr Loc Square)])
(match (Pr-second x)
['None #f]
[(Some (Piece t c))
(< (Loc-row (Pr-first x)) (Loc-row l))]))
(if (symbol=? 'Rook p) (reverse (colpair l b)) (reverse (\pair l b))))
['None 'None]
[(Some (Pr lc 'None)) 'None]
[(Some (Pr lc (Some (Piece tp cl)))) (Some (Pr lc (Piece tp cl)))])))
(check-expect (stoppers 'Rook (Loc 1 3) starting-board)
(list (Some (Pr (Loc 1 4) (Piece 'Pawn 'Black)))
(Some (Pr (Loc 6 3) (Piece 'Pawn 'White)))
(Some (Pr (Loc 1 2) (Piece 'Pawn 'Black)))
(Some (Pr (Loc 0 3) (Piece 'Queen 'Black)))))
(check-expect (stoppers 'Rook (Loc 7 7) starting-board)
(list 'None 'None
(Some (Pr (Loc 7 6) (Piece 'Knight 'White)))
(Some (Pr (Loc 6 7) (Piece 'Pawn 'White)))))
(check-expect (stoppers 'Bishop (Loc 2 6) starting-board)
(list (Some (Pr (Loc 1 7) (Piece 'Pawn 'Black)))
'None
(Some (Pr (Loc 6 2) (Piece 'Pawn 'White)))
(Some (Pr (Loc 1 5) (Piece 'Pawn 'Black)))))
(: opt-map (All (T U) ((T -> U) (Option T) U -> U)))
;; apply f to value if there is one, otherwise return default value
;; ex: (opt-map add1 'None 0) => 0
;; ex: (opt-map add1 (Some 4) 0) => 5
(define (opt-map f opt def)
(match opt
['None def]
[(Some x) (f x)]))
(: move->str (Move -> String))
;; build a string version of the move, for purposes of comparison
;; note: there is a bijection between moves and strings (and must be)
(define (move->str m)
(match m
[(StdMove src dst moved captured)
(pipes (list "StdMove"
(loc->str src)
(loc->str dst)
(piece->str moved)
(opt-map piece->str captured "None")))]
[(CastleMove ks kd rs rd moved)
(pipes (list "CastleMove"
(loc->str ks)
(loc->str kd)
(loc->str rs)
(loc->str rd)
(symbol->string moved)))]
[(PromoMove src dst moved captured pro)
(pipes (list "PromoMove"
(loc->str src)
(loc->str dst)
(piece->str moved)
(opt-map piece->str captured "None")
(symbol->string pro)))]))
(: loc->str (Loc -> String))
;; return string representation of location
(define (loc->str loc)
(match loc
[(Loc r c)
(string-append "Loc:" (number->string r) "," (number->string c))]))
(: piece->str (Piece -> String))
;; return string representation of piece
(define (piece->str p)
(match p
[(Piece t pl)
(string-append "Piece:"
(symbol->string t)
","
(symbol->string pl))]))
(: pipes ((Listof String) -> String))
;; connect strings with | character in between
;; ex: (pipes (list "a" "bb" "ccc")) ==> "a|bb|ccc"
(define (pipes ss)
(match ss
['() ""]
[(list s) s]
[(cons s r) (string-append s "|" (pipes r))]))
(: move<? (Move Move -> Boolean))
;; move comparison for the purposes of sorting
(define (move<? m1 m2)
(string<? (move->str m1) (move->str m2)))
(: sort-moves : (Listof Move) -> (Listof Move))
;; sort a list of moves into a canonical order
;; allowing for comparison with check-expect
;; note: uses the built-in sort operation
(define (sort-moves moves)
(sort moves move<?))
(: possible-moves-rook (ChessGame Loc -> (Listof Move)))
;; Lists all the moves that a hypothetical rook in the given location
;; can make for the player whose turn it is
;; without considering whether the move puts the player in check.
(define (possible-moves-rook cg l)
(append
(match (first (stoppers 'Rook l (ChessGame-board cg)))
['None (build-list (- 7 (Loc-col l))
(lambda ([k : Natural])
(StdMove l
(Loc (Loc-row l) (+ (Loc-col l) (add1 k)))
(Piece 'Rook (ChessGame-turn cg))
'None)))]
[(Some (Pr lc (Piece tp col)))
(local {(define bl1
(build-list
(- (sub1 (Loc-col lc))
(Loc-col l))
(lambda ([k : Natural])
(StdMove l
(Loc (Loc-row l) (+ (Loc-col l) (add1 k)))
(Piece 'Rook (ChessGame-turn cg))
'None))))}
(if (symbol=? (ChessGame-turn cg) col) bl1
(append bl1
(list (StdMove l
(Loc (Loc-row l) (Loc-col lc))
(Piece 'Rook (ChessGame-turn cg))
(Some (Piece tp col)))))))])
(match (second (stoppers 'Rook l (ChessGame-board cg)))
['None (build-list (- 7 (Loc-row l))
(lambda ([k : Natural])
(StdMove l
(Loc (+ (Loc-row l) (add1 k)) (Loc-col l))
(Piece 'Rook (ChessGame-turn cg))
'None)))]
[(Some (Pr lc (Piece tp col)))
(local {(define bl1
(build-list
(- (sub1 (Loc-row lc))
(Loc-row l))
(lambda ([k : Natural])
(StdMove l
(Loc (+ (add1 k) (Loc-row l)) (Loc-col l))
(Piece 'Rook (ChessGame-turn cg))
'None))))}
(if (symbol=? (ChessGame-turn cg) col) bl1
(append bl1
(list (StdMove l
(Loc (Loc-row lc) (Loc-col l))
(Piece 'Rook (ChessGame-turn cg))
(Some (Piece tp col)))))))])
(match (third (stoppers 'Rook l (ChessGame-board cg)))
['None (build-list (Loc-col l)
(lambda ([k : Natural])
(StdMove l
(Loc (Loc-row l) (- (Loc-col l) (add1 k)))
(Piece 'Rook (ChessGame-turn cg))
'None)))]
[(Some (Pr lc (Piece tp col)))
(local {(define bl1
(build-list
(- (Loc-col l) (add1 (Loc-col lc)))
(lambda ([k : Natural])
(StdMove l
(Loc (Loc-row l) (- (Loc-col l) (add1 k)))
(Piece 'Rook (ChessGame-turn cg))
'None))))}
(if (symbol=? (ChessGame-turn cg) col) bl1
(append bl1
(list (StdMove l
(Loc (Loc-row l) (Loc-col lc))
(Piece 'Rook (ChessGame-turn cg))
(Some (Piece tp col)))))))])
(match (last (stoppers 'Rook l (ChessGame-board cg)))
['None (build-list (Loc-row l)
(lambda ([k : Natural])
(StdMove l
(Loc (- (Loc-row l) (add1 k)) (Loc-col l))
(Piece 'Rook (ChessGame-turn cg))
'None)))]
[(Some (Pr lc (Piece tp col)))
(local {(define bl1
(build-list
(- (Loc-row l) (add1 (Loc-row lc)))
(lambda ([k : Natural])
(StdMove l
(Loc (- (Loc-row l) (add1 k)) (Loc-col l))
(Piece 'Rook (ChessGame-turn cg))
'None))))}
(if (symbol=? (ChessGame-turn cg) col) bl1
(append bl1
(list (StdMove l
(Loc (Loc-row lc) (Loc-col l))
(Piece 'Rook (ChessGame-turn cg))
(Some (Piece tp col)))))))])))
(check-expect (possible-moves-rook (ChessGame starting-board
'White
'()
(Castles #t #t #t #t))
(Loc 2 3))
(list
(StdMove (Loc 2 3) (Loc 2 4) (Piece 'Rook 'White) 'None)
(StdMove (Loc 2 3) (Loc 2 5) (Piece 'Rook 'White) 'None)
(StdMove (Loc 2 3) (Loc 2 6) (Piece 'Rook 'White) 'None)
(StdMove (Loc 2 3) (Loc 2 7) (Piece 'Rook 'White) 'None)
(StdMove (Loc 2 3) (Loc 3 3) (Piece 'Rook 'White) 'None)
(StdMove (Loc 2 3) (Loc 4 3) (Piece 'Rook 'White) 'None)
(StdMove (Loc 2 3) (Loc 5 3) (Piece 'Rook 'White) 'None)
(StdMove (Loc 2 3) (Loc 2 2) (Piece 'Rook 'White) 'None)
(StdMove (Loc 2 3) (Loc 2 1) (Piece 'Rook 'White) 'None)
(StdMove (Loc 2 3) (Loc 2 0) (Piece 'Rook 'White) 'None)
(StdMove (Loc 2 3) (Loc 1 3) (Piece 'Rook 'White)
(Some (Piece 'Pawn 'Black)))))
(: possible-moves-bishop (ChessGame Loc -> (Listof Move)))
;; Lists all the moves that a hypothetical bishop in the given location
;; can make for the player whose turn it is
;; without considering whether the move puts the player in check.
(define (possible-moves-bishop cg l)
(local {(define col (Loc-col l))
(define row (Loc-row l))
(define brd (ChessGame-board cg))
(define trn (ChessGame-turn cg))
(define pair/ (/pair l brd))
(define pair\ (\pair l brd))}
(remove* (list (StdMove l l (Piece 'Bishop 'White) 'None))
(append
(match* ((first (stoppers 'Bishop l brd))
(third (stoppers 'Bishop l brd)))
[('None 'None) (map (lambda ([pr : (Pr Loc Square)])
(StdMove l (Pr-first pr)
(Piece 'Bishop trn) 'None))
pair/)]
[((Some (Pr lc (Piece tp clr))) 'None)
(map (lambda ([pr : (Pr Loc Square)])
(StdMove l (Pr-first pr) (Piece 'Bishop trn)
(if (and (= (Loc-row (Pr-first pr))
(Loc-row lc))
(= (Loc-col (Pr-first pr))
(Loc-col lc)))
(Some (Piece tp clr)) 'None)))
(filter (lambda ([pr : (Pr Loc Square)])
(if (symbol=? clr trn)
(< (Loc-col (Pr-first pr))
(Loc-col lc))
(<= (Loc-col (Pr-first pr))
(Loc-col lc)))) pair/))]
[('None (Some (Pr lc (Piece tp clr))))
(map (lambda ([pr : (Pr Loc Square)])
(StdMove l (Pr-first pr) (Piece 'Bishop trn)
(if (and (= (Loc-row (Pr-first pr))
(Loc-row lc))
(= (Loc-col (Pr-first pr))
(Loc-col lc)))
(Some (Piece tp clr)) 'None)))
(filter (lambda ([pr : (Pr Loc Square)])
(if (symbol=? clr trn) (> (Loc-col
(Pr-first pr))
(Loc-col lc))
(>= (Loc-col (Pr-first pr))
(Loc-col lc)))) pair/))]
[((Some (Pr lc1 (Piece tp1 clr1)))
(Some (Pr lc2
(Piece tp2 clr2))))
(map (lambda ([pr : (Pr Loc Square)])
(StdMove l (Pr-first pr) (Piece 'Bishop trn)
(cond
[(and (= (Loc-row (Pr-first pr))
(Loc-row lc1))
(= (Loc-col (Pr-first pr))
(Loc-col lc1)))
(Some (Piece tp1 clr1))]
[(and (= (Loc-row (Pr-first pr))
(Loc-row lc2))
(= (Loc-col (Pr-first pr))
(Loc-col lc2)))
(Some (Piece tp2 clr2))]
[else 'None])))
(filter (lambda ([pr : (Pr Loc Square)])
(cond
[(and (symbol=? clr1 trn)
(symbol=? clr2 trn))
(and (> (Loc-col (Pr-first pr))
(Loc-col lc2))
(< (Loc-col (Pr-first pr))
(Loc-col lc1)))]
[(and (symbol=? clr1 trn)
(not
(symbol=? clr2 trn)))
(and (>= (Loc-col (Pr-first pr))
(Loc-col lc2))
(< (Loc-col (Pr-first pr))
(Loc-col lc1)))]
[(and (not (symbol=? clr1 trn))
(symbol=? clr2 trn))
(and (> (Loc-col (Pr-first pr))
(Loc-col lc2))
(<= (Loc-col (Pr-first pr))
(Loc-col lc1)))]
[else (and (>= (Loc-col (Pr-first pr))
(Loc-col lc2))
(<= (Loc-col (Pr-first pr))
(Loc-col lc1)))]))
pair/))])
(match* ((second (stoppers 'Bishop l brd))
(last (stoppers 'Bishop l brd)))
[('None 'None) (map (lambda ([pr : (Pr Loc Square)])
(StdMove l
(Pr-first pr)
(Piece 'Bishop trn)
'None)) pair\ )]
[((Some (Pr lc (Piece tp clr))) 'None)
(map (lambda ([pr : (Pr Loc Square)])
(StdMove l (Pr-first pr) (Piece 'Bishop trn)
(if (and (=
(Loc-row (Pr-first pr))
(Loc-row lc))
(= (Loc-col (Pr-first pr))
(Loc-col lc)))
(Some (Piece tp clr)) 'None)))
(filter (lambda ([pr : (Pr Loc Square)])
(if (symbol=? clr trn) (< (Loc-col
(Pr-first pr))
(Loc-col lc))
(<= (Loc-col (Pr-first pr))
(Loc-col lc)))) pair\ ))]
[('None (Some (Pr lc (Piece tp clr))))
(map (lambda ([pr : (Pr Loc Square)])
(StdMove l (Pr-first pr) (Piece 'Bishop trn)
(if (and (= (Loc-row
(Pr-first pr))
(Loc-row lc))
(= (Loc-col (Pr-first pr))
(Loc-col lc)))
(Some (Piece tp clr)) 'None)))
(filter (lambda ([pr : (Pr Loc Square)])
(if (symbol=? clr trn) (> (Loc-col
(Pr-first pr))
(Loc-col lc))
(>= (Loc-col (Pr-first pr))
(Loc-col lc)))) pair\ ))]
[((Some (Pr lc1 (Piece tp1 clr1)))
(Some (Pr lc2
(Piece tp2 clr2))))
(map (lambda ([pr : (Pr Loc Square)])
(StdMove l (Pr-first pr) (Piece 'Bishop trn)
(cond
[(and (= (Loc-row (Pr-first pr))
(Loc-row lc1))
(= (Loc-col (Pr-first pr))
(Loc-col lc1)))
(Some (Piece tp1 clr1))]
[(and (= (Loc-row (Pr-first pr))
(Loc-row lc2))
(= (Loc-col (Pr-first pr))
(Loc-col lc2)))
(Some (Piece tp2 clr2))]
[else 'None])))
(filter (lambda ([pr : (Pr Loc Square)])
(cond
[(and (symbol=? clr1 trn)
(symbol=? clr2 trn))
(and (> (Loc-col (Pr-first pr))
(Loc-col lc2))
(< (Loc-col (Pr-first pr))
(Loc-col lc1)))]
[(and (symbol=? clr1 trn)
(not
(symbol=? clr2 trn)))
(and (>= (Loc-col (Pr-first pr))
(Loc-col lc2))
(< (Loc-col (Pr-first pr))
(Loc-col lc1)))]
[(and (not (symbol=? clr1 trn))
(symbol=? clr2 trn))
(and (> (Loc-col (Pr-first pr))
(Loc-col lc2))
(<= (Loc-col (Pr-first pr))
(Loc-col lc1)))]
[else (and (>= (Loc-col (Pr-first pr))
(Loc-col lc2))
(<= (Loc-col (Pr-first pr))
(Loc-col lc1)))]))
pair\ ))])))))
(check-expect (sort-moves (possible-moves-bishop (ChessGame starting-board
'White
'()
(Castles #t #t #t #t))
(Loc 2 3)))
(sort-moves
(list
(StdMove (Loc 2 3) (Loc 1 4) (Piece 'Bishop 'White)
(Some (Piece 'Pawn 'Black)))
(StdMove (Loc 2 3) (Loc 3 4) (Piece 'Bishop 'White) 'None)
(StdMove (Loc 2 3) (Loc 4 5) (Piece 'Bishop 'White) 'None)
(StdMove (Loc 2 3) (Loc 5 6) (Piece 'Bishop 'White) 'None)
(StdMove (Loc 2 3) (Loc 3 2) (Piece 'Bishop 'White) 'None)
(StdMove (Loc 2 3) (Loc 4 1) (Piece 'Bishop 'White) 'None)
(StdMove (Loc 2 3) (Loc 5 0) (Piece 'Bishop 'White) 'None)
(StdMove (Loc 2 3) (Loc 1 2) (Piece 'Bishop 'White)
(Some (Piece 'Pawn 'Black))))))
(: possible-moves-queen (ChessGame Loc -> (Listof Move)))
;; Lists all the moves that a hypothetical queen in the given location
;; can make for the player whose turn it is
;; without considering whether the move puts the player in check.
(define (possible-moves-queen cg l)
(map (lambda ([std : Move])
(match std
[(StdMove s d _ c) (StdMove s d (Piece 'Queen (ChessGame-turn cg)) c)]))
(append (possible-moves-rook cg l) (possible-moves-bishop cg l))))
(check-expect (sort-moves (possible-moves-queen (ChessGame starting-board
'White
'()
(Castles #t #t #t #t))
(Loc 2 3)))
(sort-moves
(list
(StdMove (Loc 2 3) (Loc 1 4) (Piece 'Queen 'White)
(Some (Piece 'Pawn 'Black)))
(StdMove (Loc 2 3) (Loc 3 4) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 4 5) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 5 6) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 3 2) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 4 1) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 5 0) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 1 2) (Piece 'Queen 'White)
(Some (Piece 'Pawn 'Black)))
(StdMove (Loc 2 3) (Loc 2 4) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 2 5) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 2 6) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 2 7) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 3 3) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 4 3) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 5 3) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 2 2) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 2 1) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 2 0) (Piece 'Queen 'White) 'None)
(StdMove (Loc 2 3) (Loc 1 3) (Piece 'Queen 'White)
(Some (Piece 'Pawn 'Black))))))
(: kinglocs (Loc -> (Listof Loc)))
;; Produces a list of all locations in the square perimeter around the given location.
;; Does not take into consideration the boundaries of the board.
(define (kinglocs l)
(local {(define r (Loc-row l))
(define c (Loc-col l))}
(list (Loc (add1 r) c)
(Loc (add1 r) (add1 c))
(Loc r (add1 c))
(Loc (add1 r) (sub1 c))
(Loc (sub1 r) (sub1 c))
(Loc (sub1 r) (add1 c))
(Loc (sub1 r) c)
(Loc r (sub1 c)))))
(check-expect
(knightlocs (Loc 3 4))
(list (Loc 5 5) (Loc 5 3) (Loc 4 6) (Loc 4 2)
(Loc 2 6) (Loc 2 2) (Loc 1 5) (Loc 1 3)))
(: knightlocs (Loc -> (Listof Loc)))
;; Produces a list of all locations with one entry differing by 2, the other by 1,
;; from entries of given location.
(define (knightlocs l)
(local {(define r (Loc-row l))
(define c (Loc-col l))}
(list (Loc (+ 2 r) (add1 c))
(Loc (+ 2 r) (sub1 c))
(Loc (add1 r) (+ 2 c))
(Loc (add1 r) (- c 2))
(Loc (sub1 r) (+ c 2))
(Loc (sub1 r) (- c 2))
(Loc (- r 2) (add1 c))
(Loc (- r 2) (sub1 c)))))
(: possible-moves-king-knight ((U 'Knight 'King) ChessGame Loc -> (Listof Move)))
;; Lists all the moves that a hypothetical piece (king or knight)
;; in the given location can make for the player whose turn it is
;; without considering whether the move puts the player in check.
(define (possible-moves-king-knight k cg l)
(map (lambda ([loc : Loc])
(match (board-ref (ChessGame-board cg) loc)
['None (StdMove l loc (Piece k (ChessGame-turn cg)) 'None)]
[(Some (Piece t col))
(StdMove l loc (Piece k (ChessGame-turn cg)) (Some (Piece t col)))]))
(filter (lambda ([lc : Loc])
(and (>= (Loc-col lc) 0)
(< (Loc-col lc) 8)
(>= (Loc-row lc) 0)
(< (Loc-row lc) 8)
(match (board-ref (ChessGame-board cg) lc)
['None #t]
[(Some (Piece _ p)) (not
(symbol=? p
(ChessGame-turn cg)))])))
(if (symbol=? k 'King) (kinglocs l) (knightlocs l)))))
(check-expect (sort-moves
(possible-moves-king-knight 'King
(ChessGame starting-board
'Black
'()
(Castles #t #t #t #t))
(Loc 6 6)))
(sort-moves
(list
(StdMove (Loc 6 6) (Loc 6 7) (Piece 'King 'Black)
(Some (Piece 'Pawn 'White)))
(StdMove (Loc 6 6) (Loc 7 7) (Piece 'King 'Black)
(Some (Piece 'Rook 'White)))
(StdMove (Loc 6 6) (Loc 7 6) (Piece 'King 'Black)
(Some (Piece 'Knight 'White)))
(StdMove (Loc 6 6) (Loc 7 5) (Piece 'King 'Black)
(Some (Piece 'Bishop 'White)))
(StdMove (Loc 6 6) (Loc 6 5) (Piece 'King 'Black)
(Some (Piece 'Pawn 'White)))
(StdMove (Loc 6 6) (Loc 5 5) (Piece 'King 'Black) 'None)
(StdMove (Loc 6 6) (Loc 5 6) (Piece 'King 'Black) 'None)
(StdMove (Loc 6 6) (Loc 5 7) (Piece 'King 'Black) 'None))))
(: pmpb2/3/5 (ChessGame Loc -> (Listof Move)))
;; Lists all the moves that a hypothetical black pawn in the given location
;; with row 2, 3, or 5 can make for the player whose turn it is
;; without considering whether the move puts the player in check.
(define (pmpb2/3/5 cg l)
(local {(define forward (Loc (add1 (Loc-row l)) (Loc-col l)))
(define diagleft (Loc (add1 (Loc-row l)) (sub1 (Loc-col l))))
(define diagright (Loc (add1 (Loc-row l)) (add1 (Loc-col l))))
(define colleft (Loc-col diagleft))
(define colright (Loc-col diagright))
(define b (ChessGame-board cg))}
(append (if (symbol? (board-ref b forward))
(list (StdMove l forward (Piece 'Pawn 'Black) 'None))
'())
(if (>= colleft 0)
(match (board-ref b diagleft)
['None '()]
[(Some (Piece tp col))
(if (symbol=? col 'White)
(list (StdMove l diagleft
(Piece 'Pawn 'Black)
(Some (Piece tp col)))) '())]) '())
(if (<= colright 7)
(match (board-ref b diagright)
['None '()]
[(Some (Piece tp col))
(if (symbol=? col 'White)
(list (StdMove l diagright
(Piece 'Pawn 'Black)
(Some (Piece tp col)))) '())]) '()))))
(: pmpb1 (ChessGame Loc -> (Listof Move)))
;; Lists all the moves that a hypothetical black pawn in the given location
;; with row 1 can make for the player whose turn it is
;; without considering whether the move puts the player in check.
(define (pmpb1 cg l)
(append (pmpb2/3/5 cg l)
(if (symbol? (board-ref (ChessGame-board cg)
(Loc (+ 2 (Loc-row l)) (Loc-col l))))
(list (StdMove l (Loc (+ 2 (Loc-row l)) (Loc-col l))
(Piece 'Pawn 'Black) 'None)) '())))
(: pmpb4 (ChessGame Loc -> (Listof Move)))
;; Lists all the moves that a hypothetical black pawn in the given location
;; with row 4 can make for the player whose turn it is
;; without considering whether the move puts the player in check.
(define (pmpb4 cg l)
(append (pmpb2/3/5 cg l)
(match (ChessGame-history cg)
['() '()]
[(cons a rs)
(match a
[(StdMove st dst moved capt)
(cond
[(and (symbol=? (Piece-type moved) 'Pawn)
(= (- (Loc-row st) (Loc-row dst)) 2)
(or (= (Loc-col st) (add1 (Loc-col l)))
(= (Loc-col st) (sub1 (Loc-col l)))))
(list (StdMove l (Loc (add1 (Loc-row dst)) (Loc-col dst))
(Piece 'Pawn 'Black)
(Some (Piece 'Pawn 'White))))]
[else '()])]
[(CastleMove _ _ _ _ _) '()]
[(PromoMove _ _ _ _ _) '()])])))
(: pmpb6 (ChessGame Loc -> (Listof Move)))
;; Lists all the moves that a hypothetical black pawn in the given location
;; with row 6 can make for the player whose turn it is
;; without considering whether the move puts the player in check.
(define (pmpb6 cg l)
(local {(define forward (Loc (add1 (Loc-row l)) (Loc-col l)))
(define diagleft (Loc (add1 (Loc-row l)) (sub1 (Loc-col l))))
(define diagright (Loc (add1 (Loc-row l)) (add1 (Loc-col l))))
(define colleft (Loc-col diagleft))
(define colright (Loc-col diagright))
(define b (ChessGame-board cg))}
(append (if (symbol? (board-ref b forward))
(list (PromoMove l forward (Piece 'Pawn 'Black) 'None 'Queen)
(PromoMove l forward (Piece 'Pawn 'Black) 'None 'Rook)
(PromoMove l forward (Piece 'Pawn 'Black) 'None 'Bishop)
(PromoMove l forward (Piece 'Pawn 'Black) 'None 'Knight))
'())
(if (>= colleft 0)
(match (board-ref b diagleft)
['None '()]
[(Some (Piece tp col))
(if (symbol=? col 'White)
(list
(PromoMove l diagleft
(Piece 'Pawn 'Black)
(Some (Piece tp col))
'Queen)
(PromoMove l diagleft
(Piece 'Pawn 'Black)
(Some (Piece tp col))
'Rook)
(PromoMove l diagleft
(Piece 'Pawn 'Black)
(Some (Piece tp col))
'Bishop)
(PromoMove l diagleft
(Piece 'Pawn 'Black)
(Some (Piece tp col))
'Knight)) '())]) '())
(if (<= colright 7)
(match (board-ref b diagright)
['None '()]
[(Some (Piece tp col))
(if (symbol=? col 'White)
(list (PromoMove l diagright
(Piece 'Pawn 'Black)
(Some (Piece tp col))
'Queen)
(PromoMove l diagright
(Piece 'Pawn 'Black)
(Some (Piece tp col))
'Rook)
(PromoMove l diagright
(Piece 'Pawn 'Black)
(Some (Piece tp col))
'Bishop)
(PromoMove l diagright
(Piece 'Pawn 'Black)
(Some (Piece tp col))
'Knight)) '())]) '()))))
(: pmpw5/4/2 (ChessGame Loc -> (Listof Move)))
;; Lists all the moves that a hypothetical white pawn in the given location
;; with row 2, 4, or 5 can make for the player whose turn it is
;; without considering whether the move puts the player in check.
(define (pmpw5/4/2 cg l)
(local {(define forward (Loc (sub1 (Loc-row l)) (Loc-col l)))
(define diagleft (Loc (sub1 (Loc-row l)) (sub1 (Loc-col l))))
(define diagright (Loc (sub1 (Loc-row l)) (add1 (Loc-col l))))
(define colleft (Loc-col diagleft))
(define colright (Loc-col diagright))
(define b (ChessGame-board cg))}
(append (if (symbol? (board-ref b forward))
(list (StdMove l forward (Piece 'Pawn 'White) 'None))
'())
(if (>= colleft 0)
(match (board-ref b diagleft)
['None '()]
[(Some (Piece tp col))
(if (symbol=? col 'Black)
(list (StdMove l diagleft
(Piece 'Pawn 'White)
(Some (Piece tp col)))) '())]) '())
(if (<= colright 7)
(match (board-ref b diagright)
['None '()]
[(Some (Piece tp col))
(if (symbol=? col 'Black)
(list (StdMove l diagright
(Piece 'Pawn 'White)
(Some (Piece tp col)))) '())]) '()))))
(: pmpw6 (ChessGame Loc -> (Listof Move)))
;; Lists all the moves that a hypothetical white pawn in the given location
;; with row 6 can make for the player whose turn it is
;; without considering whether the move puts the player in check.