-
Notifications
You must be signed in to change notification settings - Fork 1
/
bce-ai.lisp
295 lines (243 loc) · 7.02 KB
/
bce-ai.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
;; This file is part of Boo's Chess Engine
;; Copyright 1999 By Christopher Bowron
;; Boo's Chess Engine AI routines
(provide 'bce-ai)
(proclaim fast)
(defconstant *ai-array-size* 10)
(defvar *ai-moves*
(make-array *ai-array-size* :initial-element *dummy-move*
:element-type 'move))
(deftype ai-move-type ()
(type-of *ai-moves*))
(proclaim '(type ai-move-type *ai-moves*))
(defun save-move (m depth)
(declare (type move m)
(type fixnum depth))
(when (= depth *search-depth*)
(format t "saving ~a @ depth ~a~%" (move-string m) depth))
(setf (aref *ai-moves* depth) m))
(defun get-saved-move (depth)
(declare (type fixnum depth))
(aref *ai-moves* depth))
(defun computer-move (color depth)
(setq *computer* t)
(let ((start-time (get-internal-real-time))
(advantage 0)
(m *dummy-move*))
(declare (type fixnum advantage)
(type move m))
(if (bookp)
(progn
(format t "Book Opening~%")
(setq advantage 0)
(setq m (book-opening)))
(progn
(setq advantage (bce-search *lose* *win* color depth))
(setq m (get-saved-move depth))))
(cond
((= m *dummy-move*)
(cond
((incheckp color) -1)
(t -2)))
(t
(let* ((start (move-start m))
(end (move-end m))
(p (get-piece (square-file start) (square-rank start))))
(push (do-move m) *game-undo-list*)
(fresh-line)
(format t "move ~a~A~%" (move-string m)
(cond
((and (= (chess-piece-piece p) pawn-value)
(or (= (square-rank end) rank8)
(= (square-rank end) rank1)))
"=Q")
(t "")))
(format t "BCE search : ~a~%" advantage)
(- (get-internal-real-time) start-time))))))
;; search routines
(proclaim '(ftype (function (fixnum fixnum fixnum fixnum) fixnum)
ab-search q-search))
(defvar minq *q-depth*)
(declaim (type fixnum minq))
;; quiesent search
(defun q-search (alpha beta color depth)
(declare (type fixnum alpha beta color depth))
(when (< depth minq)
(setq minq depth))
(cond
((<= depth 0) (evaluate color))
(t
(let ((moves (gen-moves color))
(searched nil)
(incheck (incheckp color)))
(declare (type boolean searched incheck))
(when (and (not (capture-p))
(not incheck))
(return-from q-search
(evaluate color)))
(loop
(when (endp moves) (return nil))
(when (>= alpha beta) (return t))
(let* ((m (pop moves))
(end (move-end m))
(f (square-file end))
(r (square-rank end)))
(declare (type move m)
(type square end)
(type fixnum f r))
;; continue search if in check or the move is a take
(when (or incheck
(not (emptyp f r)))
(let ((undo (do-move m))
(intocheck nil)
(value 0))
(declare (type undo-type undo)
(type boolean intocheck)
(type fixnum value))
(setq intocheck (incheckp color))
(cond
(intocheck (setq value *lose*))
(t
(setq searched t)
(setq value (- (bce-search (- beta)
(- alpha)
(opp color)
(1- depth))))))
(undo-move undo)
(when (> value alpha)
(setq alpha value))))))
(if searched
alpha
(evaluate color))))))
(defun bce-search (alpha beta color depth)
(declare (type fixnum alpha beta color depth))
(cond
((<= depth 0) (q-search alpha beta color *q-depth*))
(t
(let ((moves (gen-moves color))
(searched nil))
(declare (type boolean searched))
(loop
(when (endp moves) (return nil))
(when (>= alpha beta) (return t))
(let* ((m (pop moves))
(undo (do-move m))
(intocheck nil)
(value 0))
(declare (type move m)
(type undo-type undo)
(type boolean intocheck)
(type fixnum value))
(setq intocheck (incheckp color))
(cond
(intocheck (setq value *lose*))
(t
(setq searched t)
(setq value (- (bce-search (- beta)
(- alpha)
(opp color)
(1- depth))))))
(undo-move undo)
(when (> value alpha)
(setq alpha value)
(save-move m depth))))
(if searched
alpha
(evaluate color))))))
;; static evaluation functions
(proclaim '(ftype (function (fixnum) fixnum) evaluate material mobility
doubled-pawns))
(proclaim '(ftype (function (file rank chess-piece) fixnum) square-value))
(defun evaluate (color)
(declare (type fixnum color))
(let ((result 0)
(opp (opp color))
(us 0)
(them 0))
(declare (type fixnum result opp us them))
(setq us
(+ (material color)
(the fixnum (- (the fixnum
(* 8 (the fixnum (doubled-pawns color))))))
))
(setq them (+ (material opp)
(the fixnum (- (the fixnum
(* 8 (the fixnum (doubled-pawns opp))))))
))
(setq result (- us them))
result))
(defun material (color)
(declare (type fixnum color)
(inline))
(let ((index (color-index color)))
(declare (type fixnum index))
(aref *material* index)))
(defun mobility (color)
(declare (type fixnum color)
(inline))
(let ((index (color-index color)))
(declare (type fixnum index))
(aref *mobility* index)))
(defun square-value (f r p)
(declare (type fixnum f r)
(type chess-piece p))
(let* ((array
(aref *square-values*
(color-index (chess-piece-color p))))
(this-board (aref array (chess-piece-piece p))))
(declare (type (simple-vector 7) array))
(get-board this-board f r)))
(defun evaluate-2 (color)
(declare (type fixnum color))
(the fixnum
(+ (* 8 (the (integer -32000 32000) (- (material color)
(material (opp color)))))
(the fixnum (- (mobility color) (mobility (opp color)))))))
(defun doubled-pawns (color)
(declare (type fixnum color))
(let ((pawn-vec (aref *pawns* (color-index color)))
(doubled 0))
(declare (type fixnum doubled)
(type pawn-struct pawn-vec))
(dotimes (c 8)
(declare (type fixnum c))
(when (> (the fixnum (svref pawn-vec c)) 1)
(incf doubled)))
doubled))
(defun count-material ()
(setf (aref *material* 0) 0)
(setf (aref *material* 1) 0)
(fill (the pawn-struct (aref *pawns* 0)) 0)
(fill (the pawn-struct (aref *pawns* 1)) 0)
(loopboard
(let* ((p (get-piece f r)))
(declare (type chess-piece p))
(update-material (make-square f r) p t))))
;;(defun bce-search-2 (alpha beta color depth)
;; (declare (type fixnum alpha beta color depth))
;; (cond
;; ((= depth 0) (q-search alpha beta color *q-depth*))
;; (t
;; (let ((best *lose*) (value 0))
;; (declare (type fixnum best value))
;; (dolist (m (gen-moves color))
;; (declare (type move m))
;; (when (< best beta)
;; (when (> best alpha)
;; (setq alpha best)))
;; (let ((undo (do-move m))
;; (check nil))
;; (declare (type undo-type undo)
;; (type boolean check))
;; (setq check (incheckp color))
;; (unless check
;; (setq value
;; (- (bce-search-2 (- beta)
;; (- alpha)
;; (opp color)
;; (1- depth)))))
;; (undo-move undo)
;; (when (and (not check) (>= value best))
;; (setq best value)
;; (save-move m depth))))
;; best))))