forked from mishoo/queen.lisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.lisp
1137 lines (1013 loc) · 39.1 KB
/
board.lisp
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
(in-package #:queen)
(in-readtable queen::syntax)
(eval-when (:compile-toplevel :load-toplevel :execute)
(defconstant +QUEEN+ #x01)
(defconstant +ROOK+ #x02)
(defconstant +KNIGHT+ #x04)
(defconstant +BISHOP+ #x08)
(defconstant +PAWN+ #x10)
(defconstant +KING+ #x20)
(defconstant +WQUEEN+ (logior #x01 #x40))
(defconstant +WROOK+ (logior #x02 #x40))
(defconstant +WKNIGHT+ (logior #x04 #x40))
(defconstant +WBISHOP+ (logior #x08 #x40))
(defconstant +WPAWN+ (logior #x10 #x40))
(defconstant +WKING+ (logior #x20 #x40))
(defconstant +PROMOTABLE+ #x0f)
(defconstant +CAPTURABLE+ #x3f)
(defconstant +PIECE+ #x3f)
(defconstant +WHITE+ #x40)
(defconstant +BLACK+ #x00))
(defparameter +FEN-START+ "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
(defparameter *unicode* nil)
(declaim (inline piece
piece-side
index-valid?
white
board-index
field-index
index-field
index-row
index-col
is-pawn?
is-knight?
is-bishop?
is-rook?
is-queen?
is-king?
is-black?
is-white?
same-side?
opp-side?
board-get
board-get-rc
board-set
board-set-rc
make-move
move-from
move-to
move-piece
move-white?
move-black?
move-side
move-capture?
move-captured-piece
move-promote?
move-promoted-piece
move-set-promoted-piece
move-check?
move-set-check
move-enpa?
move-captured-index
move-oo?
move-ooo?
move-castle?))
(deftype piece ()
'(unsigned-byte 8))
(deftype board-index ()
'(integer 0 119))
(deftype board ()
'(simple-array piece (120)))
(defun piece (p)
(declare (type piece p))
(logand p +PIECE+))
(defun piece-side (p)
(declare (type piece p))
(logand p +WHITE+))
(defun index-valid? (index)
(declare (optimize speed)
(type fixnum index))
(and (typep index 'board-index)
(not (logtest index #x88))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun board-index (row col)
(declare (optimize speed)
(type (integer 0 7) row col))
(dpb row (byte 3 4) col))
(defun field-index (field)
(declare (type (simple-string 2) field))
(let ((col (aref field 0))
(row (aref field 1)))
(board-index (- (char-code row) 49)
(- (logior 32 (char-code col)) 97))))
(set-macro-character
#\$ (lambda (stream ch)
(declare (ignore ch))
(field-index (coerce (list (read-char stream)
(read-char stream)) 'string)))))
(defun index-row (index)
(declare (type board-index index))
(ldb (byte 3 4) index))
(defun index-col (index)
(declare (type board-index index))
(ldb (byte 3 0) index))
(defmacro with-row-col ((index row col) &body body)
(once-only (index)
`(let ((,row (index-row ,index))
(,col (index-col ,index)))
(declare (ignorable ,row ,col))
,@body)))
(defun index-field (index)
(declare (type board-index index))
(with-row-col (index row col)
(format nil "~C~C" (code-char (+ col 97)) (code-char (+ row 49)))))
(defun piece-char (p)
(declare (type piece p))
(if *unicode*
(piece-unicode p)
(ecase p
;; empty
(0 #\-)
;; black
(#.+PAWN+ #\p)
(#.+KNIGHT+ #\n)
(#.+KING+ #\k)
(#.+BISHOP+ #\b)
(#.+ROOK+ #\r)
(#.+QUEEN+ #\q)
;; white
(#.+WPAWN+ #\P)
(#.+WKNIGHT+ #\N)
(#.+WKING+ #\K)
(#.+WBISHOP+ #\B)
(#.+WROOK+ #\R)
(#.+WQUEEN+ #\Q))))
(defun piece-unicode (p)
(declare (type piece p))
(ecase p
;; empty
(0 #\□)
;; black
(#.+PAWN+ #\♟)
(#.+KNIGHT+ #\♞)
(#.+KING+ #\♚)
(#.+BISHOP+ #\♝)
(#.+ROOK+ #\♜)
(#.+QUEEN+ #\♛)
;; white
(#.+WPAWN+ #\♙)
(#.+WKNIGHT+ #\♘)
(#.+WKING+ #\♔)
(#.+WBISHOP+ #\♗)
(#.+WROOK+ #\♖)
(#.+WQUEEN+ #\♕)))
(defun char-piece (p)
(declare (type character p))
(case p
(#\- 0)
;; black
((#\p #\♟) +PAWN+)
((#\n #\♞) +KNIGHT+)
((#\k #\♚) +KING+)
((#\b #\♝) +BISHOP+)
((#\r #\♜) +ROOK+)
((#\q #\♛) +QUEEN+)
;; white
((#\P #\♙) #.+WPAWN+)
((#\N #\♘) #.+WKNIGHT+)
((#\K #\♔) #.+WKING+)
((#\B #\♗) #.+WBISHOP+)
((#\R #\♖) #.+WROOK+)
((#\Q #\♕) #.+WQUEEN+)))
(defun is-pawn? (p)
(declare (type piece p))
(logtest p +PAWN+))
(defun is-knight? (p)
(declare (type piece p))
(logtest p +KNIGHT+))
(defun is-bishop? (p)
(declare (type piece p))
(logtest p +BISHOP+))
(defun is-rook? (p)
(declare (type piece p))
(logtest p +ROOK+))
(defun is-queen? (p)
(declare (type piece p))
(logtest p +QUEEN+))
(defun is-king? (p)
(declare (type piece p))
(logtest p +KING+))
(defun is-white? (p)
(declare (type piece p))
(logtest p +WHITE+))
(defun is-black? (p)
(declare (type piece p))
(not (is-white? p)))
(defun same-side? (p1 p2)
(declare (type piece p1 p2))
(= (logand p1 +WHITE+) (logand p2 +WHITE+)))
(defun opp-side? (p1 p2)
(declare (type piece p1 p2))
(not (same-side? p1 p2)))
(defun board-get-rc (board row col)
(declare (optimize speed)
(type board board)
(type (integer 0 7) row col))
(aref board (board-index row col)))
(defun board-set-rc (board row col piece)
(declare (optimize speed)
(type board board)
(type (integer 0 7) row col)
(type piece piece))
(setf (aref board (board-index row col)) piece))
(defun board-get (board index)
(declare (optimize speed)
(type board board)
(type board-index index))
(aref board index))
(defun board-set (board index val)
(declare (optimize speed)
(type board board)
(type board-index index)
(type piece val))
(setf (aref board index) val))
(defsetf board-get board-set)
(defmacro with-piece ((board pos p &optional allow-empty) &body body)
(once-only (board pos)
`(let ((,p (board-get ,board ,pos)))
,(if allow-empty
`(progn ,@body)
`(unless (zerop ,p)
,@body)))))
(defun make-board ()
(make-array 120 :element-type 'piece :initial-element 0))
(defun board-foreach (board fn)
(declare (optimize speed)
(type board board)
(type (function (piece (integer 0 7) (integer 0 7) board-index) t) fn))
(loop for row from 0 to 7 do
(loop for col from 0 to 7
for index = (board-index row col)
for piece = (board-get board index)
when (not (zerop piece))
do (funcall fn piece row col index))))
(defun print-board (board &key (output t))
(loop for row from 7 downto 0
for line = (loop for col from 0 to 7
collect (piece-char (board-get-rc board row col)))
do (format output "~D │ ~{~A~^ ~}~%" (+ row 1) line))
(format output " └─────────────────~%")
(format output " a b c d e f g h~%"))
(defconstant +WHITE-OO+ 1)
(defconstant +WHITE-OOO+ 2)
(defconstant +WHITE-CASTLE+ 3)
(defconstant +BLACK-OO+ 4)
(defconstant +BLACK-OOO+ 8)
(defconstant +BLACK-CASTLE+ 12)
(defconstant +ALL-CASTLE+ 15)
(defstruct game
(board (make-board) :type board)
(state 0 :type (unsigned-byte 32))
(side +WHITE+ :type piece)
(enpa nil :type (or board-index null))
(fullmove 0 :type (unsigned-byte 32))
(halfmove 0 :type (unsigned-byte 32)))
(defmethod reset-from-fen ((game game) (in stream))
(let ((board (game-board game))
(state 0))
(with-parse-stream in
(labels ((read-row (row)
(loop for ch = (next)
for col upfrom 0
do (cond
((find ch "pnkbrqPNKBRQ")
(board-set-rc board row col (char-piece ch)))
((find ch "12345678")
(dotimes (i (- (char-code ch) 48))
(board-set-rc board row col 0)
(incf col))
(decf col))
(t (unget ch)
(return)))))
(read-position ()
(loop for row from 7 downto 0
do (read-row row)
(unless (zerop row)
(skip #\/))))
(read-side ()
(case (next)
(#\w (setf (game-side game) +WHITE+))
(#\b (setf (game-side game) +BLACK+))
(otherwise (error "Cannot read playing side"))))
(read-castling ()
(if (eql (peek) #\-)
(next)
(loop while (find (peek) "kqKQ")
do (case (next)
(#\k (setf state (logior state +BLACK-OO+)))
(#\q (setf state (logior state +BLACK-OOO+)))
(#\K (setf state (logior state +WHITE-OO+)))
(#\Q (setf state (logior state +WHITE-OOO+))))))
(setf (game-state game) state))
(read-en-passant ()
(if (eql (peek) #\-)
(progn
(next)
(setf (game-enpa game) nil))
(let ((col (next))
(row (next)))
(unless (and (find col "abcdefghABCDEFGH")
(find row "12345678"))
(error "Invalid en-passant field"))
(setf (game-enpa game)
(board-index (- (char-code row) 49)
(- (char-code (char-downcase col)) 97))))))
(read-halfmove ()
(setf (game-halfmove game) (read-number)))
(read-fullmove ()
(setf (game-fullmove game) (read-number))))
;; now do it
(read-position)
(skip #\SPACE)
(read-side)
(skip #\SPACE)
(read-castling)
(skip #\SPACE)
(read-en-passant)
(skip #\SPACE)
(read-halfmove)
(skip #\SPACE)
(read-fullmove)
game))))
(defmethod reset-from-fen ((game game) (fen string))
(with-input-from-string (in fen)
(reset-from-fen game in)))
(defmethod reset-game ((game game))
(reset-from-fen game +FEN-START+))
(defmethod game-fen ((game game))
(let ((board (game-board game))
(state (game-state game))
(enpa (game-enpa game))
(*unicode* nil))
(with-output-to-string (*standard-output*)
(loop for row from 7 downto 0
do (loop with empty = 0
for col from 0 to 7
for p = (board-get-rc board row col)
do (cond
((zerop p) (incf empty))
(t
(unless (zerop empty)
(format t "~D" empty))
(setf empty 0)
(write-char (piece-char p))))
finally (unless (zerop empty)
(format t "~D" empty)))
(unless (zerop row)
(write-char #\/)))
(write-char #\SPACE)
(write-char (if (= 0 (game-side game)) #\b #\w))
(write-char #\SPACE)
(when (logtest state +WHITE-OO+) (write-char #\K))
(when (logtest state +WHITE-OOO+) (write-char #\Q))
(when (logtest state +BLACK-OO+) (write-char #\k))
(when (logtest state +BLACK-OOO+) (write-char #\q))
(unless (logtest state +ALL-CASTLE+) (write-char #\-))
(write-char #\SPACE)
(if enpa
(write-string (index-field enpa))
(write-char #\-))
(write-char #\SPACE)
(format t "~D" (game-halfmove game))
(write-char #\SPACE)
(format t "~D" (game-fullmove game)))))
;;; moves
(deftype move ()
'(unsigned-byte 32))
(defun make-move (from to piece capture enpa)
(declare (optimize speed)
(type board-index from to)
(type piece piece capture)
(type (unsigned-byte 1) enpa))
(let ((move (dpb (index-col from) (byte 3 0) 0)))
(declare (type move move))
(setf move (dpb (index-row from) (byte 3 3) move))
(setf move (dpb (index-col to) (byte 3 6) move))
(setf move (dpb (index-row to) (byte 3 9) move))
(setf move (dpb piece (byte 7 12) move))
(setf move (dpb capture (byte 6 23) move))
(setf move (dpb enpa (byte 1 29) move))))
(defun move-from (move)
(declare (type move move))
(board-index (ldb (byte 3 3) move)
(ldb (byte 3 0) move)))
(defun move-to (move)
(declare (type move move))
(board-index (ldb (byte 3 9) move)
(ldb (byte 3 6) move)))
(defun move-piece (move)
(declare (type move move))
(ldb (byte 7 12) move))
(defun move-white? (move)
(declare (type move move))
(ldb-test (byte 1 18) move))
(defun move-black? (move)
(declare (type move move))
(not (move-white? move)))
(defun move-side (move)
(declare (type move move))
(ash (ldb (byte 1 18) move) 6))
(defun move-capture? (move)
(declare (type move move))
(ldb-test (byte 6 23) move))
(defun move-captured-piece (move)
(declare (type move move))
(let ((p (ldb (byte 6 23) move)))
(cond
((zerop p) nil)
((move-black? move) (logior p +WHITE+))
(t p))))
(defun move-promote? (move)
(declare (type move move))
(ldb-test (byte 4 19) move))
(defun move-promoted-piece (move)
(declare (type move move))
(let ((p (ldb (byte 4 19) move)))
(cond
((zerop p) nil)
((move-black? move) p)
(t (logior p +WHITE+)))))
(defun move-set-promoted-piece (move promo)
(declare (type move move)
(type piece promo))
(dpb promo (byte 4 19) move))
(defun move-set-check (move)
(declare (type move move))
(dpb 1 (byte 1 30) move))
(defun move-check? (move)
(declare (type move move))
(ldb-test (byte 1 30) move))
(defun move-enpa? (move)
(declare (type move move))
(ldb-test (byte 1 29) move))
(defun move-captured-index (move)
(declare (type move move))
(cond
((move-enpa? move)
(board-index (ldb (byte 3 3) move)
(ldb (byte 3 6) move)))
((move-capture? move)
(move-to move))
(t
(error "Not a capturing move"))))
(defun move-oo? (move)
(declare (type move move))
(= (logand move #b0111111000111000111)
#||# #b0100000000110000100))
(defun move-ooo? (move)
(declare (type move move))
(= (logand move #b0111111000111000111)
#||# #b0100000000010000100))
(defun move-castle? (move)
(declare (type move move))
(= (logand move #b0111111000011000111)
#||# #b0100000000010000100))
;;; move execution
(defun game-move (game move &optional quick)
(declare (optimize speed)
(type game game)
(type move move))
(let ((board (game-board game))
(from (move-from move))
(to (move-to move))
(piece (move-piece move))
(white (move-white? move))
(promo (move-promoted-piece move)))
;; update board
(board-set board to (or promo piece))
(board-set board from 0)
;; handle special moves (castle and en-passant)
(cond
((move-oo? move)
(cond
(white
(board-set board $H1 0)
(board-set board $F1 +WROOK+))
(t
(board-set board $H8 0)
(board-set board $F8 +ROOK+))))
((move-ooo? move)
(cond
(white
(board-set board $A1 0)
(board-set board $D1 +WROOK+))
(t
(board-set board $A8 0)
(board-set board $D8 +ROOK+))))
((move-enpa? move)
(board-set board (move-captured-index move) 0)))
;; update side to move and en-passant target
(setf (game-side game) (if white +BLACK+ +WHITE+)
(game-enpa game) (when (and (is-pawn? piece)
(= (abs (- from to)) 32))
(ash (+ from to) -1)))
;; update castling state
(symbol-macrolet ((state (game-state game)))
(when (and (logtest state +WHITE-OO+)
(or (= from $E1) (= from $H1) (= to $H1)))
(setf state (logxor state +WHITE-OO+)))
(when (and (logtest state +WHITE-OOO+)
(or (= from $E1) (= from $A1) (= to $A1)))
(setf state (logxor state +WHITE-OOO+)))
(when (and (logtest state +BLACK-OO+)
(or (= from $E8) (= from $H8) (= to $H8)))
(setf state (logxor state +BLACK-OO+)))
(when (and (logtest state +BLACK-OOO+)
(or (= from $E8) (= from $A8) (= to $A8)))
(setf state (logxor state +BLACK-OOO+))))
;; fullmove and halfmove counters
(unless quick
(unless white
(incf (game-fullmove game)))
(if (or (is-pawn? piece) (move-capture? move))
(setf (game-halfmove game) 0)
(incf (game-halfmove game))))))
(defun board-undo-move (board move)
(declare (optimize speed)
(type board board)
(type move move))
(let ((from (move-from move))
(to (move-to move))
(captured (move-captured-piece move))
(piece (move-piece move)))
;; restore board
(board-set board from piece)
(cond
(captured
(board-set board (move-captured-index move) captured)
(when (move-enpa? move)
(board-set board to 0)))
(t
(board-set board to 0)))
;; special moves
(cond
((move-oo? move)
(cond
((move-white? move)
(board-set board $H1 +WROOK+)
(board-set board $F1 0))
(t
(board-set board $H8 +ROOK+)
(board-set board $F8 0))))
((move-ooo? move)
(cond
((move-white? move)
(board-set board $A1 +WROOK+)
(board-set board $D1 0))
(t
(board-set board $A8 +ROOK+)
(board-set board $D8 0)))))))
(defmacro with-move ((game move &optional quick) &body body)
(once-only (game move)
(with-gensyms (state enpa halfmove fullmove side)
`(let ((,state (game-state ,game))
(,enpa (game-enpa ,game))
(,side (game-side ,game))
,@(unless quick
`((,halfmove (game-halfmove ,game))
(,fullmove (game-fullmove ,game)))))
(game-move ,game ,move ,quick)
(prog1
(progn ,@body)
(board-undo-move (game-board ,game) ,move)
(setf (game-side ,game) ,side
(game-state ,game) ,state
(game-enpa ,game) ,enpa
,@(unless quick
`((game-halfmove ,game) ,halfmove
(game-fullmove ,game) ,fullmove))))))))
;;; move generation
(defparameter +MOVES-KNIGHT+ '(31 33 14 18 -18 -14 -33 -31))
(defparameter +MOVES-BISHOP+ '(15 17 -15 -17))
(defparameter +MOVES-ROOK+ '(1 16 -16 -1))
(defparameter +MOVES-QING+ `(,@+MOVES-BISHOP+ ,@+MOVES-ROOK+))
(defun king-index (game &optional (side (game-side game)))
(declare (optimize speed)
(type game game)
(type piece side))
(loop with king = (logior +KING+ side)
with board = (game-board game)
for row from 0 to 7 do
(loop for col from 0 to 7
for index = (board-index row col)
when (= (board-get board index) king)
do (return-from king-index index))))
(defun attacked? (game &optional
(side (game-side game))
(index (king-index game side)))
(declare (optimize speed)
(type piece side)
(type game game)
(type board-index index))
(let* ((board (game-board game))
(opp (logxor side +WHITE+)))
(labels ((test (p piece)
(when (and (same-side? p opp)
(= p (logand p piece)))
(return-from attacked? t)))
(check (piece delta)
(declare (type piece piece)
(type fixnum delta))
(let ((pos (+ index delta)))
(when (index-valid? pos)
(with-piece (board pos p)
(test p piece)))))
(repeat (piece delta)
(declare (type fixnum delta))
(loop for pos fixnum = (+ index delta) then (+ pos delta)
while (index-valid? pos)
do (with-piece (board pos p)
(test p piece)
(return)))))
(declare (inline test check repeat))
(cond ((is-white? opp)
(check +WPAWN+ -15)
(check +WPAWN+ -17))
(t
(check +PAWN+ +15)
(check +PAWN+ +17)))
(loop with piece = (logior opp +KNIGHT+)
for delta in +MOVES-KNIGHT+
do (check piece delta))
(loop with piece = (logior opp +BISHOP+ +QUEEN+)
for delta in +MOVES-BISHOP+
do (repeat piece delta))
(loop with piece = (logior opp +ROOK+ +QUEEN+)
for delta in +MOVES-ROOK+
do (repeat piece delta))
(loop with piece = (logior opp +KING+)
for delta in +MOVES-QING+
do (check piece delta))
nil)))
(defun game-compute-moves (game)
(declare (optimize speed)
(type game game))
(let* ((side (game-side game))
(opp (logxor side +WHITE+))
(board (game-board game))
(moves '())
(enpa (game-enpa game))
(my-king (king-index game side))
(opp-king (king-index game opp))
flag in-check)
(loop for row fixnum from 0 to 7 do
(loop for col fixnum from 0 to 7
for from = (board-index row col)
for piece = (board-get board from)
when (same-side? piece side) do
(labels
((move-pawn (c1 c2 a1 a2 on-start on-end)
(labels ((try-enpa (delta)
(when enpa
(let ((to (+ from delta)))
(when (= to enpa)
(add (make-move from to piece (logior +PAWN+ opp) 1))))))
(try-capture (delta)
(let ((to (+ from delta)))
(when (index-valid? to)
(with-piece (board to p)
(when (opp-side? piece p)
(maybe-promote (make-move from to piece p 0)))))))
(try-advance (delta)
(let ((to (+ from delta)))
;; `to' index should be always valid
(with-piece (board to p t)
(when (zerop p)
(maybe-promote (make-move from to piece 0 0))
;; we want to return true if the field is empty, so that
;; i.e. if we're in check and D3 doesn't get us out (in which
;; case `add' will return nil), we still want to try D4.
t))))
(maybe-promote (move)
(cond
(on-end
(when (add (move-set-promoted-piece move +KNIGHT+))
(%add (move-set-promoted-piece move +BISHOP+))
(%add (move-set-promoted-piece move +ROOK+))
(%add (move-set-promoted-piece move +QUEEN+))))
(t
(add move)))))
(declare (inline try-enpa try-capture try-advance maybe-promote))
(unless (try-enpa c1) (try-capture c1))
(unless (try-enpa c2) (try-capture c2))
(when (and (try-advance a1) on-start)
(try-advance a2))))
(move-knight ()
(mapc #'move +MOVES-KNIGHT+))
(move-bishop ()
(mapc #'repeat +MOVES-BISHOP+))
(move-rook ()
(mapc #'repeat +MOVES-ROOK+))
(move-queen ()
(mapc #'repeat +MOVES-QING+))
(in-check? ()
(if flag
in-check
(setf flag t
in-check (attacked? game side my-king))))
(move-king (oo ooo oo1 oo2 ooo1 ooo2 ooo3)
(mapc #'move +MOVES-QING+)
;; note: `add' discards all moves that leave our king in check, so it's not
;; necessary to test here whether the target field is attacked; we only
;; have to check the middle field (i.e. F1 for white's O-O).
(when (and (logtest (game-state game) oo)
(zerop (board-get board oo1))
(zerop (board-get board oo2))
(not (in-check?))
(not (attacked? game side oo1)))
(add (make-move from oo2 piece 0 0)))
(when (and (logtest (game-state game) ooo)
(zerop (board-get board ooo1))
(zerop (board-get board ooo2))
(zerop (board-get board ooo3))
(not (in-check?))
(not (attacked? game side ooo1)))
(add (make-move from ooo2 piece 0 0))))
(move (delta)
(declare (type fixnum delta))
(let ((to (+ from delta)))
(when (index-valid? to)
(with-piece (board to p t)
(when (or (zerop p) (opp-side? p side))
(add (make-move from to piece p 0)))))))
(repeat (delta)
(declare (type fixnum delta))
(loop for to fixnum = (+ from delta) then (+ to delta)
while (index-valid? to) do
(let ((p (board-get board to)))
(cond
((zerop p)
(add (make-move from to piece 0 0)))
((opp-side? p side)
(add (make-move from to piece p 0))
(return))
(t
(return))))))
(add (m)
(with-move (game m t)
(let ((index (if (is-king? piece) (move-to m) my-king)))
(unless (attacked? game side index)
(car (push (if (attacked? game opp opp-king)
(move-set-check m)
m)
moves))))))
(%add (m)
(with-move (game m t)
(car (push (if (attacked? game opp opp-king)
(move-set-check m)
m)
moves)))))
(declare (inline move-pawn move-king in-check?))
(case piece
(#.+PAWN+ (move-pawn -15 -17 -16 -32 (= row 6) (= row 1)))
(#.+WPAWN+ (move-pawn +15 +17 +16 +32 (= row 1) (= row 6)))
((#.+KNIGHT+ #.+WKNIGHT+) (move-knight))
((#.+BISHOP+ #.+WBISHOP+) (move-bishop))
((#.+ROOK+ #.+WROOK+) (move-rook))
((#.+QUEEN+ #.+WQUEEN+) (move-queen))
(#.+KING+ (move-king +BLACK-OO+ +BLACK-OOO+ $F8 $G8 $D8 $C8 $B8))
(#.+WKING+ (move-king +WHITE-OO+ +WHITE-OOO+ $F1 $G1 $D1 $C1 $B1))))))
moves))
(defmethod game-parse-san ((game game) (in stream)
&optional (moves (game-compute-moves game)))
(let* ((side (game-side game))
(white (is-white? side))
(promo nil)
(capture nil)
piece from to from-file from-rank to-file to-rank)
(flet ((matches (m)
(let ((mfrom (move-from m))
(mto (move-to m)))
(when (and from to)
(return-from matches
(when (and (= from mfrom) (= to mto)
(eql promo (move-promoted-piece m)))
m)))
(when (and (eql piece (move-piece m))
(eql promo (move-promoted-piece m)))
(with-row-col (mfrom mfromrow mfromcol)
(with-row-col (mto mtorow mtocol)
(when (and from-file (/= from-file mfromcol))
(return-from matches nil))
(when (and from-rank (/= from-rank mfromrow))
(return-from matches nil))
(when (and to-file (/= to-file mtocol))
(return-from matches nil))
(when (and to-rank (/= to-rank mtorow))
(return-from matches nil))
(when (eql capture t)
(return-from matches (and (move-capture? m) m)))
(when capture
(return-from matches (and (eql (move-captured-piece m) capture) m)))
(return-from matches m)))))))
(with-parse-stream in
(labels ((read-piece ()
(let* ((ch (peek))
(p (and ch (char-piece ch))))
(when (and p (or (> (char-code ch) 255)
(is-white? p)))
(next)
(logior side (piece p)))))
(read-field ()
(let (file rank)
(setf file (peek))
(if (and file (char<= #\a file #\h))
(progn
(setf file (- (char-code file) 97))
(next))
(setf file nil))
(setf rank (peek))
(if (and rank (char<= #\1 rank #\8))
(progn
(setf rank (- (char-code rank) 49))
(next))
(setf rank nil))
(values file rank)))
(read-from ()
(multiple-value-bind (file rank) (read-field)
(setf from-file file
from-rank rank)
(when (and file rank)
(setf from (board-index rank file)))))
(maybe-skip (&rest chars)
(when (member (peek) chars :test #'eql)
(next)))
(skip-sep ()
(awhen (maybe-skip #\x #\: #\-)
(when (or (eql it #\x) (eql it #\:))
(setf capture t))))
(read-to ()
(multiple-value-bind (file rank) (read-field)
(setf to-file file
to-rank rank)
(when (and file rank)
(setf to (board-index rank file)))))
(read-promo ()
(when (eql (peek) #\=)
(next))
(read-piece))
(read-castle ()
(when (eql (peek) #\O)
(next)
(unless (eql (next) #\-)
(return-from game-parse-san nil))
(unless (eql (next) #\O)
(return-from game-parse-san nil))
(cond
((eql (peek) #\-)
(next)
(unless (eql (next) #\O)
(return-from game-parse-san nil))
(setf piece (logior +KING+ side)
from (if white $E1 $E8)
to (if white $C1 $C8)))
(t
(setf piece (logior +KING+ side)
from (if white $E1 $E8)
to (if white $G1 $G8))))
t)))
(unless (read-castle)
(setf piece (read-piece))
(read-from)
(skip-sep)
(when capture
(awhen (read-piece)
(setf capture (logxor it +WHITE+))))
(read-to)
(setf promo (read-promo))
(loop while (maybe-skip #\# #\+ #\! #\?))
(when (and (not piece) (or from-file from-rank to-file to-rank))
(setf piece (logior side +PAWN+)))
(when (and (or from-file from-rank)
(not to-file) (not to-rank)) ; only destination is specified
(setf to from