-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomoku.lisp
291 lines (255 loc) · 9.51 KB
/
gomoku.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
;;;-*- Mode: Lisp; Package: gomoku -*-
(in-package :gomoku)
; David Johnson-Davies - 10th May 2020
; Licensed under the MIT license: https://opensource.org/licenses/MIT
; To play the game in the listener evaluate:
; (in-package :gomoku)
; (gomoku #'human (alpha-beta-searcher 3 #'count-difference))
;
; Go-Moku
;
(defparameter empty 0 "An empty square")
(defparameter black 1 "A black move")
(defparameter white 2 "A white move")
(defparameter boardsize 15 "The size of the grid")
(defparameter arraysize (* boardsize boardsize) "The size of the array")
(defun initial-board ()
(make-array arraysize :initial-element empty))
(defun copy-board (board)
(copy-seq board))
(defun name-of (piece) (char ".OX" piece))
(defun opponent (player) (if (eql player 1) 2 1))
(defun print-board (board)
(format t "~% A B C D E F G H I J K L M N O (~a) (~a)~%" (count-difference black board) (count-difference white board))
(do ((y 0 (1+ y))) ((>= y boardsize))
(format t "~2,d " (1+ y))
(do ((x 0 (1+ x))) ((>= x boardsize))
(format t "~c " (name-of (bref board (+ x (* y boardsize))))))
(format t "~%")))
(defparameter *all-lines*
(let (lines)
;; Rows
(do ((y 0 (1+ y)) (line nil nil)) ((>= y boardsize))
(do ((x 0 (1+ x))) ((>= x boardsize))
(push (+ x (* y boardsize)) line))
(push line lines))
;; Columns
(do ((x 0 (1+ x)) (line nil nil)) ((>= x boardsize))
(do ((y 0 (1+ y))) ((>= y boardsize))
(push (+ x (* y boardsize)) line))
(push line lines))
;; Diagonals
(do ((z 0 (1+ z)) (line nil nil)) ((>= z boardsize))
(do ((y z (1+ y)) (x 0 (1+ x))) ((>= y boardsize)) (push (+ x (* y boardsize)) line))
(push line lines))
;;
(do ((z 1 (1+ z)) (line nil nil)) ((>= z boardsize))
(do ((y 0 (1+ y)) (x z (1+ x))) ((>= x boardsize)) (push (+ x (* y boardsize)) line))
(push line lines))
;;
(do ((z 0 (1+ z)) (line nil nil)) ((>= z boardsize))
(do ((y z (1+ y)) (x (- boardsize 1) (1- x))) ((>= y boardsize)) (push (+ x (* y boardsize)) line))
(push line lines))
;;
(do ((z 1 (1+ z)) (line nil nil)) ((>= z boardsize))
(do ((y 0 (1+ y)) (x (- boardsize 1 z) (1- x))) ((< x 0)) (push (+ x (* y boardsize)) line))
(push line lines))
(remove-if #'(lambda (line) (< (length line) 5)) lines)))
(defun bref (board square) (aref board square))
(defsetf bref (board square) (val)
`(setf (aref ,board ,square) ,val))
(defun valid-p (move)
(and (integerp move) (>= move 0) (< move arraysize)))
(defun legal-p (move player board)
(eq (bref board move) empty))
(defun make-move (move player board)
(setf (bref board move) player)
board)
(defun any-legal-move? (player board)
(dotimes (move arraysize nil)
(when (legal-p move player board) (return move))))
(defun gomoku (bl-strategy wh-strategy &optional (print t))
(let* ((board (initial-board))
(player black)
(result
(loop
(let ((strategy (if (eq player black) bl-strategy wh-strategy)))
(get-move strategy player board print)
;; Game ended?
(when (not (any-legal-move? player board)) (return nil))
(when (game-won? player board) (return player))
(setq player (if (eq player black) white black))))))
(when print
(format t "Game over. ~c won~%" (name-of result))
(print-board board))
result))
(defun get-move (strategy player board print)
(when print (print-board board))
(let ((move (funcall strategy player (copy-board board))))
(cond
((and (valid-p move) (legal-p move player board))
(make-move move player board))
(t (warn "Illegal move: ~d~%" move)
(get-move strategy player board print)))))
(defun human (player board)
(declare (ignore board))
(format t "~%~c to move: " (name-of player))
(let* ((move (read-line))
(col (- (char-code (char-upcase (char move 0))) (char-code #\A)))
(row (1- (parse-integer move :start 1))))
(+ (* row boardsize) col)))
(defun random-elt (seq)
"Pick a random element out of a sequence."
(when seq (elt seq (random (length seq)))))
(defun random-strategy (player board)
(random-elt (legal-moves player board)))
;
; Get a list of moves to consider
; Only try moves touching another piece
;
(defun legal-moves (player board)
(let (legal)
(dotimes (move arraysize)
(when
(and
(legal-p move player board)
(some #'(lambda (offset)
(and (<= 0 (+ move offset) 224)
(not (eq empty (bref board (+ move offset))))))
'(-1 1 -14 -15 -16 14 15 16)))
(push move legal)))
(or legal '(96 97 98 111 112 113 126 127 128))))
; Returns winning line
(defun game-won? (player board)
(some #'(lambda (line)
(let ((count 0))
(some
#'(lambda (move)
(if (eq (bref board move) player) (incf count) (setq count 0))
(>= count 5))
line)))
*all-lines*))
(defun winning-line (player board)
(let (win)
(some #'(lambda (line)
(let ((count 0))
(some
#'(lambda (move)
(cond
((eq (bref board move) player)
(incf count)
(push move win))
(t (setq count 0 win nil)))
(when (>= count 5) win))
line)))
*all-lines*)))
(defun score (player r1 p1 r2 p2 r3 p3)
(cond
((= p2 empty) 0)
((= p2 player) ; Player to move next - assume is O
(case r2
(5 5000000) ; line of 5
(4 ; opponent will win now
(cond
((and (>= r1 1) (= p1 empty)) 1000000) ; .OOOO
((and (>= r3 1) (= p3 empty)) 1000000) ; OOOO.
(t 0)))
(3
(cond
((and (>= r1 2) (= p1 empty) (>= r3 1) (= p3 empty)) 1000000) ; ..OOO.
((and (>= r1 1) (= p1 empty) (>= r3 2) (= p3 empty)) 1000000) ; .OOO..
(t 0)))
(2
(cond
((and (= p1 empty) (= p3 empty)) (+ (* r1 r3))) ; 1 or 2 with spaces around
(t 0)))
(1
(cond
((and (= p1 empty) (= p3 empty)) (+ (round (* r1 r3) 10))) ; 1 or 2 with spaces around
(t 0)))
(t 0)))
(t ; opponent
(case r2
(5 -5000000) ; line of 5
(4 ; will win next go
(cond
((and (>= r1 1) (= p1 empty) (>= r3 1) (= p3 empty)) -100000) ; .XXXX.
(t 0)))
(3
(cond
((and (>= r1 2) (= p1 empty) (>= r3 2) (= p3 empty)) -10000) ; ..XXX..
((and (>= r1 1) (= p1 empty) (>= r3 1) (= p3 empty)) -1000) ; .XXX.
((and (>= r1 1) (= p1 empty) (>= r3 2) (= p3 empty)) -1000) ; XXX..
(t 0)))
(2
(cond
((and (= p1 empty) (= p3 empty)) (- (* r1 r3))) ; 1 or 2 with spaces around
(t 0)))
(1
(cond
((and (= p1 empty) (= p3 empty)) (- (round (* r1 r3) 10))) ; 1 or 2 with spaces around
(t 0)))
(t 0)))))
(defun run (player board line)
(let ((score 0) (run 0) (r1 0) (r2 0) (r3 0) (p1 0) (p2 0) (p3 0) (last 0))
(map nil #'(lambda (move)
(let ((piece (bref board move)))
(cond
((eq piece last) (incf run))
(t (setq r3 r2 p3 p2)
(setq r2 r1 p2 p1)
(setq r1 run p1 last)
(setq run 1)
(incf score (score player r1 p1 r2 p2 r3 p3))))
(setq last piece)))
line)
(setq r3 r2 p3 p2)
(setq r2 r1 p2 p1)
(setq r1 run p1 last)
(incf score (score player r1 p1 r2 p2 r3 p3))
score))
(defun count-difference (player board)
(let ((score 0))
(map nil #'(lambda (line) (incf score (run player board line))) *all-lines*)
score))
(defun maximizer (eval-fn)
#'(lambda (player board)
(let* ((moves (legal-moves player board))
(scores (map 'list #'(lambda (move)
(funcall eval-fn player
(make-move move player (copy-board board))))
moves))
(best (apply #'max scores)))
(elt moves (position best scores)))))
(defun maximize-difference (player board)
(funcall (maximizer #'count-difference) player board))
(defconstant winning-value most-positive-fixnum)
(defconstant losing-value most-negative-fixnum)
(defun random-order (list)
(let ((rlist (map 'list #'(lambda (item) (cons item (random most-positive-fixnum))) list)))
(map 'list #'car (sort rlist #'< :key #'cdr))))
(defun alpha-beta (player board achievable cutoff ply eval-fn)
(cond
((zerop ply)
(funcall eval-fn player board))
(t
(let* ((moves (random-order (legal-moves player board)))
(best-move (first moves)))
(cond
((game-won? player board) winning-value)
((game-won? (opponent player) board) losing-value)
((null moves) 0)
(t
(dolist (move moves)
(let* ((board2 (make-move move player (copy-board board)))
(val (- (alpha-beta (opponent player) board2 (- cutoff) (- achievable) (- ply 1) eval-fn))))
(when (> val achievable)
(setf achievable val best-move move)))
(when (>= achievable cutoff) (return)))
(values achievable best-move)))))))
(defun alpha-beta-searcher (depth eval-fn)
#'(lambda (player board)
(multiple-value-bind (value move)
(alpha-beta player board losing-value winning-value depth eval-fn)
(declare (ignore value))
move)))