-
Notifications
You must be signed in to change notification settings - Fork 1
/
chess.lisp
1792 lines (1493 loc) · 47.6 KB
/
chess.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; chess.lisp
;;;
;;; christopher bowron - [email protected]
;;;
;;; digital dreamland 1999
;;; ... we came, we saw, we got bored and left ...
;;; copyright 1999 christopher bowron
;; to do..
;; ???
;; done
;; fix domove, save-variables only when they change
;; declare all variable types - do, do*, dolist left
(proclaim '(optimize (speed 3)))
;; (compilation-speed 0)
;; (debug 0)
;; (safety 0)))
;;(setq ext:*gc-verbose* nil)
(deftype chess-board ()
'(simple-array symbol (8 8)))
(deftype chess-move ()
'cons)
(deftype chess-color ()
'(integer -1 1))
;; functions for dealing with moves
(defmacro make-move (sq1 sq2)
`(cons ,sq1 ,sq2))
(defmacro square1 (move)
`(car ,move))
(defmacro square2 (move)
`(cdr ,move))
(defmacro empty-move ()
`(make-move (make-square 8 8)
(make-square 8 8)))
(defvar checkmate 30000)
(defvar stalemate 15000)
(deftype small-fixnum ()
;; `(signed-byte 32))
`(integer -30000 30000))
;; functions for dealing with squares
(deftype chess-square ()
'cons)
(defmacro make-square (x y)
`(cons ,x ,y))
(defmacro squarex (square)
`(the small-fixnum (car ,square)))
(defmacro squarey (square)
`(the small-fixnum (cdr ,square)))
;;;(deftype chess-square ()
;;; '(simple-array small-fixnum))
;;;(defmacro make-square (x y)
;;; `(make-array 2 :element-type 'small-fixnum :initial-contents '(,x ,y)))
;;;(defmacro squarex (square)
;;; `(aref ,square 0))
;;;(defmacro squarey (square)
;;; `(aref ,square 1))
(defmacro nil-square ()
`(make-square 8 8))
(defvar draw-rep
(make-square 8 8))
;; global special variables
(defvar *black* -1)
(defvar *white* 1)
(defvar wcancastle (cons t t))
(defvar bcancastle (cons t t))
(defvar wenpassant nil)
(defvar benpassant nil)
(defvar turn 1)
(defvar computer-controlled (cons nil nil))
;; are these right/useful?
(proclaim '(type chess-color turn *white* *black*))
(proclaim '(type cons wcancastle bcancastle computer-controlled))
(proclaim '(type (or null small-fixnum) wenpassant benpassant))
;; not just the hamburger joint
(defvar white-castle (list (make-move (make-square 4 0) (make-square 6 0))
(make-move (make-square 4 0) (make-square 2 0))))
(defvar black-castle (list (make-move (make-square 4 7) (make-square 6 7))
(make-move (make-square 4 7) (make-square 2 7))))
(proclaim '(type list white-castle black-castle))
;; initial board setup
(defvar *board*
(make-array '(8 8) :element-type 'symbol
:initial-contents
'(; mirror image
(wr wn wb wq wk wb wn wr)
(wp wp wp wp wp wp wp wp)
(empty empty empty empty empty empty empty empty)
(empty empty empty empty empty empty empty empty)
(empty empty empty empty empty empty empty empty)
(empty empty empty empty empty empty empty empty)
(bp bp bp bp bp bp bp bp)
(br bn bb bq bk bb bn br)
)))
(proclaim '(type chess-board *board*))
;; +---------------------------------------------------------------------+
;; is location sq off of the board?
(defun offboardp (sq)
(declare (type chess-square sq))
(let ((a (squarex sq))
(b (squarey sq)))
(declare (type small-fixnum a b))
(cond
((> a 7) t)
((> b 7) t)
((< a 0) t)
((< b 0) t)
(t nil))))
;; does a clear path exist directly from sq1 to sq2 ?
(defun clearpathp (sq1 sq2 board)
(declare (type chess-square sq1 sq1)
(type chess-board board))
(let* ((sx (squarex sq1))
(ex (squarex sq2))
(sy (squarey sq1))
(ey (squarey sq2))
(dx (- ex sx))
(dy (- ey sy))
(res t)
(done nil)
(x 0)
(y 0))
(declare (type small-fixnum sx ex sy ey dx dy x y)
(type symbol res done))
(setq dx
(cond
((= 0 dx) 0)
((> 0 dx) -1)
(t 1)))
(setq dy
(cond
((= 0 dy) 0)
((> 0 dy) -1)
(t 1)))
(setq x sx)
(setq y sy)
(loop
(setq x (+ x dx))
(setq y (+ y dy))
(when (and (= x ex)
(= y ey))
(return))
(when done
(return))
(when (not (equalp (getpiecexy x y board) 'empty))
(setq res nil)
(setq done t)))
res))
;; functions for accessing board
;; +---------------------------------------------------------------------+
;; get piece at '(x . y)
(defun getpiece (locale board)
(declare (inline) (type chess-square locale) (type chess-board board))
(aref board (squarey locale) (squarex locale)))
(defun getpiecexy (x y board)
(declare (inline) (type small-fixnum x y) (type chess-board board))
(aref board y x))
(defun setpiecexy (x y board piece)
(declare (inline) (type small-fixnum x y) (type chess-board board)
(type symbol piece))
(setf (aref board y x) piece))
(defun setpiece (locale board piece)
(declare (inline) (type chess-square locale) (type chess-board board)
(type symbol piece))
(setf (aref board (squarey locale) (squarex locale)) piece))
(proclaim '(type (function (chess-square chess-board) symbol) getpiece))
(proclaim '(type (function (small-fixnum small-fixnum chess-board) symbol) getpiecexy))
(proclaim '(type (function (small-fixnum small-fixnum chess-board symbol) nil) setpiecexy))
(proclaim '(type (function (chess-square chess-board symbol) nil) setpiece))
;; +---------------------------------------------------------------------+
;; valid move checker for pawns
(defun pcheck (move board)
(declare (type chess-board board) (type chess-move move))
(let* ((sq1 (square1 move))
(sq2 (square2 move))
(hor (- (squarex sq2) (squarex sq1)))
(ver (- (squarey sq2) (squarey sq1)))
(color (getcolor (getpiece sq1 board)))
(direction color)
(start (the small-fixnum (if (= color *white*) 1 6)))
(sq2piece (getpiece sq2 board))
(sq2color (getcolor sq2piece))
(sq2y (squarey sq2))
(sq1y (squarey sq1))
(newxycolor (getcolor (getpiecexy
(squarex sq2) (+ direction (squarey sq1)) board)))
)
(declare (type small-fixnum hor ver direction)
(type small-fixnum start sq2y sq1y)
(type chess-color color sq2color newxycolor)
(type symbol sq2piece)
(type chess-square sq1 sq2))
(cond
;; pawn can move forward one
((and (= sq2color 0)
(= 0 hor)
(= direction ver))
t)
;; pawn can move forward 2 on first move
((and (= 0 hor)
(= (* 2 direction) ver)
(= sq2color 0)
(= newxycolor 0)
(= sq1y start))
t)
;; pawns can attack 1 square diagonally
((and (= direction ver)
(or (= -1 hor) (= 1 hor))
(= sq2color (- color)))
t)
;; white pawns can do en passant
((and (= direction ver)
(or (= -1 hor) (= 1 hor))
(equalp (squarey sq2) 5)
(= color *white*)
(equalp (squarex sq2) wenpassant))
t)
;; so can black pawns
((and (= direction ver)
(or (= -1 hor) (= 1 hor))
(equalp sq2y 2)
(= color *black*)
(equalp (squarex sq2) benpassant))
t)
;; and thats all pawns can do
(t nil))))
(defun ncheck (move board)
(declare (ignore board) (type chess-move move))
(let* ((sq1 (square1 move))
(sq2 (square2 move))
(hor (- (squarex sq2) (squarex sq1)))
(ver (- (squarey sq2) (squarey sq1))))
(declare (type chess-square sq1 sq2)
(type small-fixnum hor ver))
(cond
;; knights move (2, 1)
((and (= (abs hor) 2)
(= (abs ver) 1)) t)
;; knights move (1, 2)
((and (= (abs ver) 2)
(= (abs hor) 1)) t)
;; and thats all knights do
(t nil))
))
(defun bcheck (move board)
(declare (type chess-board board) (type chess-move move))
(let* ((sq1 (square1 move))
(sq2 (square2 move))
(hor (- (squarex sq2) (squarex sq1)))
(ver (- (squarey sq2) (squarey sq1))))
(declare (type chess-square sq1 sq2)
(type small-fixnum hor ver))
(cond
;; dont divide by zero
((= 0 ver) nil)
;; bishops move diagaonally
((and (or (= hor ver) (= hor (- ver)))
(clearpathp sq1 sq2 board))
t)
;; and thats all bishops do
(t nil))))
(defun rcheck (move board)
(declare (type chess-board board) (type chess-move move))
(let* ((sq1 (square1 move))
(sq2 (square2 move))
(hor (- (squarex sq2) (squarex sq1)))
(ver (- (squarey sq2) (squarey sq1))))
(declare (type chess-square sq1 sq2)
(type small-fixnum hor ver))
(cond
;; rooks move side to side
((and (= hor 0)
(clearpathp sq1 sq2 board)) t)
;; rooks move up and down
((and (= ver 0)
(clearpathp sq1 sq2 board)) t)
;; and thats all rooks do
(t nil))))
(defun qcheck (move board)
(declare (type chess-board board) (type chess-move move))
;; queens can do any move rook or bishop can do
(or
(bcheck move board)
(rcheck move board)))
;; what can the king do?
(defun kcheck (move board)
(declare (type chess-board board) (type chess-move move))
(let* ((sq1 (square1 move))
(sq2 (square2 move))
(hor (- (squarex sq2) (squarex sq1)))
(ver (- (squarey sq2) (squarey sq1))))
(declare (type chess-square sq1 sq2)
(type small-fixnum hor ver))
(cond
;; king can move 1 in any direction
((and (<= (abs hor) 1)
(<= (abs ver) 1)) t)
;; white castling
;; king side
((and (equalp move (nth 0 white-castle))
(clearpathp sq1 sq2 board)
(car wcancastle)
(not (incheckp *white* board))
(not (check-checker '((4 . 0) . (5 . 0)) *white* board)))
t)
;; queen side
((and (equalp move (nth 1 white-castle))
(clearpathp sq1 '(0 . 0) board)
(cdr wcancastle)
(not (incheckp *white* board))
(not (check-checker '((4 . 0) . (3 . 0)) *white* board)))
t)
;; black castling - king side
((and (equalp move (nth 0 black-castle))
(clearpathp sq1 sq2 board)
(car bcancastle)
(not (incheckp *black* board))
(not (check-checker '((4 . 7) . (5 . 7)) *black* board)))
t)
;; queen side
((and (equalp move (nth 1 black-castle))
(clearpathp sq1 '(0 . 7) board)
(cdr bcancastle)
(not (incheckp *black* board))
(not (check-checker '((4 . 7) . (3 . 7)) *black* board)))
t)
(t nil))))
;; empty squares just arent what they used to be
(defun echeck (move board)
(declare (ignore move board))
nil)
;; +---------------------------------------------------------------------+
;; piece declarations
(defvar wp `("P" ,#'pcheck ,1 pawn-moves ))
(defvar wn `("N" ,#'ncheck ,3 knight-moves))
(defvar wb `("B" ,#'bcheck ,3 bishop-moves))
(defvar wr `("R" ,#'rcheck ,5 rook-moves ))
(defvar wq `("Q" ,#'qcheck ,10 queen-moves ))
(defvar wk `("K" ,#'kcheck ,200 king-moves ))
(defvar bp `("p" ,#'pcheck ,-1 pawn-moves ))
(defvar bn `("n" ,#'ncheck ,-3 knight-moves))
(defvar bb `("b" ,#'bcheck ,-3 bishop-moves))
(defvar br `("r" ,#'rcheck ,-5 rook-moves ))
(defvar bq `("q" ,#'qcheck ,-10 queen-moves ))
(defvar bk `("k" ,#'kcheck ,-200 king-moves ))
(defvar empty `(" " ,#'echeck ,0))
(proclaim '(type list wp wn wb wr wq wk))
(proclaim '(type list bp bn bb br bq bk))
(proclaim '(type list empty))
;; +---------------------------------------------------------------------+
;; return 1 for white, -1 for black, 0 for empty
(defun getcolor (piece)
(declare (inline) (type symbol piece))
(let ((temp (third (symbol-value piece))))
(declare (type small-fixnum temp))
(cond
((= 0 temp) 0)
((> 0 temp) -1)
(t 1))))
(proclaim '(type (function (symbol) chess-color) getcolor))
;;; print out the current board
(defun printboard (board)
(declare (type chess-board board))
(format t " a b c d e f g h ~%")
(format t " +---+---+---+---+---+---+---+---+~%")
(do ((x 7 (- x 1)))
((< x 0) nil)
(declare (type small-fixnum x))
(format t "~a |" (1+ x))
(do ((y 0 (+ y 1)))
((> y 7) nil)
(declare (type small-fixnum y))
;;(if (= (mod (+ x y) 2) 1)
(format t " ~a |"
(car (symbol-value (getpiecexy y x board))))
;; (format t "*~a*|"
;; (car (symbol-value (getpiecexy y x board)))))
)
(format t " ~a~%" (1+ x))
(format t " +---+---+---+---+---+---+---+---+~%")
)
(format t " a b c d e f g h ~%"))
;; get move from player
(defun getmove ()
(let ((s1 "")
(s2 "")
(sq1 (nil-square))
(sq2 (nil-square))
(r (string-downcase (read-line t nil nil))))
(declare (type string s1 s2)
(type chess-square sq1 sq2)
(string r))
(format t "~a~%" r)
(cond
((equalp r "draw")
(format t "you offer draw~%")
draw-rep)
((not (equalp (position #\- r) 2)) nil)
(t
(setq s1 (subseq r 0 (position #\- r)))
(setq s2 (subseq r (1+ (position #\- r))))
(setq sq1
(make-square (- (char-int (char s1 0)) (char-int #\a))
(- (char-int (char s1 1)) (char-int #\1))))
(setq sq2
(make-square (- (char-int (char s2 0)) (char-int #\a))
(- (char-int (char s2 1)) (char-int #\1))))
(format t "move : from ~a to ~a~%" sq1 sq2)
(make-move sq1 sq2)))))
;; initialize board
(defun initboard ()
(setq wcancastle (cons t t))
(setq bcancastle (cons t t))
(setq wenpassant nil)
(setq benpassant nil)
(setq turn 1)
(setq *board*
(make-array '(8 8) :element-type 'symbol
:initial-contents
'(; mirror image
(wr wn wb wq wk wb wn wr)
(wp wp wp wp wp wp wp wp)
(empty empty empty empty empty empty empty empty)
(empty empty empty empty empty empty empty empty)
(empty empty empty empty empty empty empty empty)
(empty empty empty empty empty empty empty empty)
(bp bp bp bp bp bp bp bp)
(br bn bb bq bk bb bn br)
)))
"Board Setup")
;; play a game
(defun newgame()
(let ((white 1)
(black 1)
(wm (empty-move))
(bm (empty-move))
(undolist nil))
(declare (type small-fixnum white black)
(type list bm wm)
(type list undolist))
(do ((done nil))
(done undolist)
(declare (type symbol done))
(loop
(printboard *board*)
(when (game-over-p *board*)
(setq done t)
(return))
;;(when (equalp computer-controlled '(t . t))
;; (format t "press return to continue~%")
;; (when (equalp (read-line) "exit")
;; (setq done t)
;; (return)))
(format t "W ~a : " white)
(force-output)
(setq turn *white*)
(setq wm (if (car computer-controlled)
(compmove *white* *board*)
(getmove)))
(when (null wm)
(return (setq done t)))
(if (and (cdr computer-controlled) (equalp wm draw-rep))
(format t "draw is denied... play on~%")
(when (validp wm *board*)
(format t "white moves: ~a~%" (algebraic wm))
(push (domove wm *board*) undolist)
(return)))
(format t "invalid move~%"))
(setq white (1+ white))
(when done (return undolist))
(loop
(printboard *board*)
(setq turn *black*)
(when (game-over-p *board*)
(setq done t)
(return))
(format t "B ~a : " black)
(force-output)
(setq bm (if (cdr computer-controlled)
(compmove *black* *board*)
(getmove)))
(when (null bm)
(return (setq done t)))
(if (and (car computer-controlled) (equalp bm draw-rep))
(format t "draw is denied... play on~%")
(when (validp bm *board*)
(format t "black moves: ~a~%" (algebraic bm))
(push (domove bm *board*) undolist)
(return)))
(format t "invalid move~%"))
(setq black (1+ black))
)))
;; is the game over and why
(defun game-over-p (board)
(declare (type chess-board board))
(let ((done nil))
(declare (type symbol done))
(when (= turn *white*)
(cond
((checkmatep *white* board)
(format t "White checkmated~%")
(setq done t))
((stalematep *white* board)
(format t "White stalemated~%")
(setq done t))
((incheckp *white* board)
(format t "White in check~%"))))
(when (= turn *black*)
(cond
((checkmatep *black* board)
(format t "Black checkmated~%")
(setq done t))
((stalematep *black* board)
(format t "Black stalemated~%")
(setq done t))
((incheckp *black* board)
(format t "Black in check~%"))))
done))
;; is the move valid and not into check?
(defun validp (move board)
(declare (inline) (type chess-move move) (type chess-board board))
(isvalid move board t))
;; is move valid regardless of check
(defun validp-nocheck (move board)
(declare (inline) (type chess-move move) (type chess-board board))
(isvalid move board nil))
;; is the move valid?
(defun isvalid (move board check)
(declare (type chess-move move)
(type chess-board board)
(type symbol check))
(let* ((piece (symbol-value (getpiece (square1 move) board)))
(color (getcolor (getpiece (square1 move) board)))
(piece3 (third piece))
(piece2 (second piece))
)
(declare (type list piece)
(type function piece2)
(type small-fixnum color piece3))
(cond
;; is it the right color for this turn?
;;((< (/ piece3 turn) 0) nil)
((and (> piece3 0) (< turn 0)) nil)
((and (< piece3 0) (> turn 0)) nil)
;; is there in fact a 2nd square?
((null (square2 move)) nil)
;; is it off the board?
((offboardp (square2 move)) nil)
;; are we taking our own piece?
((and (= (getcolor (getpiece (square1 move) board))
(getcolor (getpiece (square2 move) board)))) nil)
;; does the piece checker say its ok?
((not (funcall piece2 move board)) nil)
;; are we moving into check?
((and check (check-checker move color board)) nil)
(t t))))
;; will move put color into check?
(defun check-checker (move color board)
(declare (type chess-move move)
(type chess-board board)
(type small-fixnum color))
(let* ((res nil)
(undo nil))
(declare (type symbol res)
(type list undo))
;; new board
(setq undo (domove move board))
(when (incheckp color board)
(setq res t))
;; restore original board
(undo-move undo board)
res))
;; update board for move
;; return list to undo move
(defun domove (move board)
(declare (type chess-move move) (type chess-board board))
(let* ((sq1 (square1 move))
(sq2 (square2 move))
(old-movelist nil)
(moremoves nil) ;save variables only when they change
(old-variables nil) ;(save-variables))
(piece (getpiece sq1 board))
(hor (- (squarex sq2) (squarex sq1)))
(ver (- (squarey sq2) (squarey sq1)))
(color (getcolor (getpiece sq1 board))))
(declare (type chess-square sq1 sq2)
(type list old-movelist old-variables moremoves)
(type symbol piece)
(type small-fixnum hor ver color))
;;(setq old-variables (save-variables))
;; normal move
;; save original setup
(push (cons sq2 (getpiece sq2 board)) old-movelist)
(push (cons sq1 (getpiece sq1 board)) old-movelist)
(setpiecexy (squarex sq2) (squarey sq2) board
piece)
(setpiecexy (squarex sq1) (squarey sq1) board
'empty)
(cond
;; pawn promotion?
((and (equalp piece 'wp)
(= (squarey sq2) 7))
(setpiecexy (squarex sq2) (squarey sq2) board (choosepiece *white*)))
((and (equalp piece 'bp)
(= (squarey sq2) 0))
(setpiecexy (squarex sq2) (squarey sq2) board (choosepiece *black*)))
;; castle?
((and (equalp piece 'wk)
(equalp move (nth 0 white-castle)))
(setq moremoves (domove '( (7 . 0) . (5 . 0) ) board))
(setq old-variables (save-variables))
(setq wcancastle (cons nil nil)))
((and (equalp piece 'wk)
(equalp move (nth 1 white-castle)))
(setq moremoves (domove '( (0 . 0) . (3 . 0) ) board))
(setq old-variables (save-variables))
(setq wcancastle (cons nil nil)))
((and (equalp piece 'bk)
(equalp move (nth 0 black-castle)))
(setq moremoves (domove '( (7 . 7) . (5 . 7) ) board))
(setq old-variables (save-variables))
(setq bcancastle (cons nil nil)))
((and (equalp piece 'bk)
(equalp move (nth 1 black-castle)))
(setq moremoves (domove '( (0 . 7) . (3 . 7) ) board))
(setq old-variables (save-variables))
(setq bcancastle (cons nil nil)))
;; rules of castling
((equalp sq1 '(4 . 0))
(setq old-variables (save-variables))
(setq wcancastle (cons nil nil)))
((equalp sq1 '(7 . 0))
(setq old-variables (save-variables))
(setf (car wcancastle) nil))
((equalp sq1 '(0 . 0))
(setq old-variables (save-variables))
(setf (cdr wcancastle) nil))
((equalp sq1 '(4 . 7))
(setq old-variables (save-variables))
(setq bcancastle (cons nil nil)))
((equalp sq1 '(0 . 7))
(setq old-variables (save-variables))
(setf (car bcancastle) nil))
((equalp sq1 '(7 . 7))
(setq old-variables (save-variables))
(setf (cdr bcancastle) nil))
;; can opponent enpassante on next move?
(benpassant
(when (and (= -1 ver)
(or (= -1 hor) (= 1 hor))
(equalp (squarey sq2) 2)
(= color *black*)
(equalp (squarex sq2) benpassant))
(push (cons (make-move (squarex sq2) (1- (squarey sq2))) 'wp) old-movelist)
(setpiecexy (squarex sq2) (1+ (squarey sq2)) board 'empty ))
(setq old-variables (save-variables))
(setq benpassant nil));; only valid on next move
(wenpassant
(when (and (= 1 ver)
(or (= -1 hor) (= 1 hor))
(equalp (squarey sq2) 5)
(= color *white*)
(equalp (squarex sq2) wenpassant))
(push (cons (make-move (squarex sq2) (1+ (squarey sq2))) 'bp) old-movelist)
(setpiecexy (squarex sq2) (1- (squarey sq2)) board 'empty))
(setq old-variables (save-variables))
(setq wenpassant nil))
((and (equalp piece 'wp)
(= (- (squarey sq2) (squarey sq1)) 2))
(setq old-variables (save-variables))
(setq benpassant (squarex sq1)))
((and (equalp piece 'bp)
(= (- (squarey sq2) (squarey sq1)) -2))
(setq old-variables (save-variables))
(setq wenpassant (squarex sq1))))
;;(when (cdr moremoves)
;; (restore-variables (cdr moremoves)))
(dolist (item (car moremoves))
(declare (type chess-move item))
(push item old-movelist))
(cons old-movelist old-variables)))
;; take back a move
(defun undo-move (undolist board)
(declare (type list undolist)
(type chess-board board))
(when (cdr undolist)
(restore-variables (cdr undolist)))
(dolist (item (car undolist))
(declare (type cons item))
(setpiece (car item) board (cdr item))))
;; choose piece for promotion
;; needs to be more robust
(defun choosepiece (color)
(declare (type small-fixnum color))
(cond
((and (car computer-controlled) (= color *white*)) 'WQ)
((and (cdr computer-controlled) (= color *black*)) 'BQ)
(t
(let (piece)
(declare (type symbol piece))
(format t "Q, R, B, N ? : ")
(setq piece (read))
(cond
((= color *white*)
(cond
((equalp piece 'Q) 'WQ)
((equalp piece 'R) 'WR)
((equalp piece 'B) 'WB)
((equalp piece 'N) 'WN)
))
((= color *black*)
(cond
((equalp piece 'Q) 'BQ)
((equalp piece 'R) 'BR)
((equalp piece 'B) 'BB)
((equalp piece 'N) 'BN)
))
(t (format t "uhhhh....")))))))
;; is color in check?
(defun incheckp (color board)
(declare (type small-fixnum color)
(type chess-board board))
(let ((king (nil-square))
(res nil)
(ksym (if (= color *white*) 'wk 'bk))
(saved-turn turn))
(declare (type chess-square king)
(type symbol ksym res)
(type small-fixnum saved-turn))
(setq turn (- color))
(setq king
(catch 'catch-king
(loopboard #'(lambda (x y)
(when (equalp (getpiecexy x y board) ksym)
(throw 'catch-king (make-square x y)))))))
(catch 'got-em
(dolist (location (locations (- color) board))
(declare (type chess-square location))
(when (validp-nocheck (make-move location king) board)
(throw 'got-em (setq res t)))))
(setq turn saved-turn)
res))
;; is color in checkmate?
(defun checkmatep (color board)
(declare (inline) (type small-fixnum color) (type chess-board board))
(and (incheckp color board)
(equalp (validmoves color board) nil)))
;; is color stalemated?
(defun stalematep (color board)
(declare (inline) (type small-fixnum color) (type chess-board board))
(and (not (incheckp color board))
(equalp (validmoves color board) nil)))
;; apply function to all x and y combinations
(defun loopboard (thefunc)
(declare (inline)
(type (function (small-fixnum small-fixnum)) thefunc))
(do ((x 0 (+ x 1)))
((> x 7) nil)
(declare (type small-fixnum x))
(do ((y 0 (+ y 1)))
((> y 7) nil)
(declare (type small-fixnum y))
(funcall thefunc x y))))
;; other way of doing it
(defmacro boardloop (function)
`(do ((x 0 (+ x 1)))
((> x 7) nil)
(declare (type small-fixnum x))
(do ((y 0 (+ y 1)))
((> y 7) nil)
(declare (type small-fixnum y))
,function)))
;; how is color's material worth
(defun material (color board)
(declare (inline) (type small-fixnum color) (type chess-board board))
(reduce #'+ (pieces color board)
:key #'(lambda (x) (third (symbol-value x)))))
;; how many moves does color have available
(defun mobility (color board)
(declare (inline) (type small-fixnum color) (type chess-board board))
(let ((thelist (validmoves color board)))
(declare (type list thelist))
(if (= color *white*)
(length thelist)
(- (length thelist)))))
;; evalute color's advantage
(defun evalboard (color board)
(declare (type small-fixnum color) (type chess-board board))
(let* ((mat 0)
(mob 0)
(adv 0)
(mw (material *white* board))
(mb (material *black* board))
(nw (mobility *white* board))
(nb (mobility *black* board))
(infow (reduce #'+ (pawn-info *white* board)))
(infob (reduce #'+ (pawn-info *black* board)))
(w5 (* 5 infow))
(b5 (* 5 infob))
)
(declare (type small-fixnum mat mob adv mb mw nw nb
infow infob w5 b5))
(setq mat (+ mw mb))
(setq mob (+ nw nb))
(setq adv (+ (* 15 mat) mob))
(decf adv w5)
(incf adv b5)
(when (= color *black*) (setq adv (- adv)))
(cond
;; putting other person in check is good
;;((incheckp (- color) board) (setq adv (+ adv 10)))
;; checkmateing other person is really good
((checkmatep (- color) board) (setq adv checkmate))
;; stalemate is good if we are behind
((and (stalematep color board) (< adv 0)) (setq adv stalemate)))
adv))
;; computer's move
(defun compmove (color board)
(declare (type small-fixnum color) (type chess-board board))
(let ((res (nil-square)))
(declare (type chess-move res))
(format t "thinking...~%")
(if (= color *white*)
(setq res (player1 color board))
(setq res (player2 color board)))
(format t "done~%")
res))
;; how will board evaluate after move?
(defun evaluate-move (move color board)
(declare (type small-fixnum color) (type chess-board board)
(type chess-move move))
(let* ((res 0)
(undo nil)