r10498: fix number type
[reversi.git] / edge-table.lisp
1 ;;;;***************************************************************************
2 ;;;;
3 ;;;; FILE IDENTIFICATION
4 ;;;; 
5 ;;;;  Name:           edge-table.lisp
6 ;;;;  Purpose:        Edge table routines for reversi
7 ;;;;  Programer:      Kevin M. Rosenberg based on code by Peter Norvig
8 ;;;;  Date Started:   1 Nov 2001
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file is Copyright (c) 2001-2003 by Kevin M. Rosenberg 
13 ;;;; and Copyright (c) 1998-2002 Peter Norvig
14 ;;;;
15 ;;;; Reversi users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
17 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
18 ;;;;***************************************************************************
19
20
21 (in-package #:reversi)
22
23
24 (eval-when (:compile-toplevel :load-toplevel :execute)
25   (defparameter *edge-and-x-lists*
26     '((22 11 12 13 14 15 16 17 18 27)
27       (72 81 82 83 84 85 86 87 88 77)
28       (22 11 21 31 41 51 61 71 81 72)
29       (27 18 28 38 48 58 68 78 88 77))
30     "The four edges (with their X-squares)."))
31
32 (defparameter *top-edge* (first *edge-and-x-lists*))
33
34 (defvar *edge-table* nil
35   "Array of values to player-to-move for edge positions.")
36
37 ;;(declaim (type (simple-array fixnum #.(expt 3 10)) *edge-table*))
38
39 (defun make-edge-table ()
40   (setq *edge-table* (make-array (expt 3 10) :element-type 'fixnum
41                                  :adjustable nil :fill-pointer nil))
42   (init-edge-table)
43   *edge-table*)
44
45 (deftype edge-table () '(simple-array fixnum (*)))
46
47
48 (defun map-edge-n-pieces (fn player board n squares index)
49   "Call fn on all edges with n pieces."
50   ;; Index counts 1 for player; 2 for opponent
51   (declare (fixnum n index)
52            (type player player)
53            (type square index)
54            (type (simple-array fixnum (100)) board)
55            (list squares)
56            (optimize (speed 3) (space 0) (safety 0)))
57   (cond
58     ((< (length squares) n) nil)
59     ((null squares) (funcall fn board index))
60     (t (let ((index3 (* 3 index))
61              (sq (first squares)))
62          (declare (fixnum index3 sq))
63          (map-edge-n-pieces fn player board n (rest squares) index3)
64          (when (and (plusp n) (= (bref board sq) empty))
65            (setf (bref board sq) player)
66            (map-edge-n-pieces fn player board (- n 1) (rest squares)
67                               (+ 1 index3))
68            (setf (bref board sq) (opponent player))
69            (map-edge-n-pieces fn player board (- n 1) (rest squares)
70                               (+ 2 index3))
71            (setf (bref board sq) empty))))))
72
73
74
75 (defun possible-edge-moves-value (player board index)
76   "Consider all possible edge moves. 
77   Combine their values into a single number."
78   (declare (type board board)
79            (type player player)
80            (type square index))
81   (combine-edge-moves
82    (cons
83       (list 1.0 (aref *edge-table* index)) ;; no move
84       (loop for sq in *top-edge*             ;; possible moves
85             when (= (bref board sq) empty)
86             collect (possible-edge-move player board sq)))
87     player))
88
89
90 (defun edge-index (player board squares)
91   "The index counts 1 for player; 2 for opponent,
92   on each square---summed as a base 3 number."
93   (declare (type board board)
94            (type player player)
95            (type cons squares)
96            (optimize (speed 3) (safety 0) (space 0)))
97   (let ((index 0))
98     (declare (fixnum index))
99     (dolist (sq squares)
100       (declare (type square sq))
101       (setq index 
102         (the fixnum 
103           (+ 
104            (the fixnum (* index 3))
105            (the fixnum (cond ((= (bref board sq) empty) 0)
106                              ((= (bref board sq) player) 1)
107                              (t 2)))))))
108     index))
109
110 (defun possible-edge-move (player board sq)
111   "Return a (prob val) pair for a possible edge move."
112   (declare (type board board)
113            (type player player)
114            (type square sq))
115   (let ((new-board (replace-board (svref *ply-boards* player) board)))
116     (make-move sq player new-board)
117     (list (edge-move-probability player board sq)
118           (- (aref *edge-table*
119                     (edge-index (opponent player)
120                                new-board *top-edge*))))))
121
122 (defun combine-edge-moves (possibilities player)
123   "Combine the best moves."
124   (declare (type player player)
125            (optimize (speed 3) (safety 0) (space 0)))
126   (let ((prob 1.0)
127         (val 0.0)
128         (fn (if (= player black) #'> #'<)))
129     (declare (short-float prob val))
130     (loop for pair in (sort possibilities fn :key #'second)
131           while (>= prob 0.0)
132         do (incf val (* prob (first pair) (second pair)))
133            (decf prob (* prob (first pair))))
134     (round val)))
135
136
137 (eval-when (:compile-toplevel :load-toplevel :execute)
138   (let ((corner/xsqs '((11 . 22) (18 . 27) (81. 72) (88 . 77))))
139     (defun corner-p (sq) (assoc sq corner/xsqs))
140     (defun x-square-p (sq) (rassoc sq corner/xsqs))
141     (defun x-square-for (corner) (cdr (assoc corner corner/xsqs)))
142     (defun corner-for (xsq) (car (rassoc xsq corner/xsqs)))))
143
144 (defun edge-move-probability (player board square)
145   "What's the probability that player can move to this square?"
146   (declare (type board board)
147            (type player player)
148            (type square square)
149            (optimize (speed 3) (safety 0) (space 0)))
150   (cond
151     ((x-square-p square) .5) ;; X-squares
152     ((legal-p square player board) 1.0) ;; immediate capture
153     ((corner-p square) ;; move to corner depends on X-square
154      (let ((x-sq (x-square-for square)))
155        (declare (type square x-sq))
156        (cond
157          ((= (bref board x-sq) empty) .1)
158          ((= (bref board x-sq) player) 0.001)
159          (t .9))))
160     (t (/ (aref
161             '#2A((.1  .4 .7)
162                  (.05 .3  *)
163                  (.01  *  *))
164             (count-edge-neighbors player board square)
165             (count-edge-neighbors (opponent player) board square))
166           (if (legal-p square (opponent player) board) 2 1)))))
167
168 (defun count-edge-neighbors (player board square)
169   "Count the neighbors of this square occupied by player."
170   (declare (type board board)
171            (type player player)
172            (type square square))
173   (count-if #'(lambda (inc)
174                 (declare (type square inc))
175                 (= (bref board (+ square inc)) player))
176             '(+1 -1)))
177
178 (defparameter *static-edge-table*
179   '#2A(;stab  semi    un 
180        (   *    0 -2000) ; X
181        ( 700    *     *) ; corner
182        (1200  200   -25) ; C
183        (1000  200    75) ; A
184        (1000  200    50) ; B
185        (1000  200    50) ; B
186        (1000  200    75) ; A
187        (1200  200   -25) ; C
188        ( 700    *     *) ; corner
189        (   *    0 -2000) ; X
190        ))
191 (declaim (type (simple-array t (* *)) *static-edge-table*))
192
193 (defun static-edge-stability (player board)
194   "Compute this edge's static stability"
195   (declare (type board board)
196            (type player player)
197            (optimize (speed 3) (safety 0) (space 0)))
198   (loop for sq in *top-edge*
199       for i from 0
200       sum (the fixnum 
201             (cond
202              ((= (bref board sq) empty) 0)
203              ((= (bref board sq) player)
204               (aref *static-edge-table* i
205                     (piece-stability board sq)))
206              (t (- (aref *static-edge-table* i
207                          (piece-stability board sq))))))))
208
209 (eval-when (:compile-toplevel :load-toplevel :execute)
210   (let ((stable 0) (semi-stable 1) (unstable 2))
211     (declare (type fixnum stable semi-stable unstable))
212     
213     (defun piece-stability (board sq)
214       (declare (type board board)
215                (fixnum sq)
216                (optimize (speed 3) (safety 0) (space 0)))
217       (cond
218         ((corner-p sq) stable)
219         ((x-square-p sq)
220          (if (eql (bref board (corner-for sq)) empty)
221              unstable semi-stable))
222         (t (let* ((player (bref board sq))
223                   (opp (opponent player))
224                   (p1 (find player board :test-not #'eql
225                             :start sq :end 19))
226                   (p2 (find player board :test-not #'eql
227                             :start 11 :end sq
228                             :from-end t)))
229              (declare (fixnum player opp))
230              (cond
231                ;; unstable pieces can be captured immediately
232                ;; by playing in the empty square
233                ((or (and (eql p1 empty) (eql p2 opp))
234                     (and (eql p2 empty) (eql p1 opp)))
235                 unstable)
236                ;; Semi-stable pieces might be captured
237                ((and (eql p1 opp) (eql p2 opp)
238                      (find empty board :start 11 :end 19))
239                 semi-stable)
240                ((and (eql p1 empty) (eql p2 empty))
241                 semi-stable)
242                ;; Stable pieces can never be captured
243                (t stable))))))))
244
245
246 (defun init-edge-table ()
247   "Initialize *edge-table*, starting from the empty board."
248   ;; Initialize the static values
249   (declare (optimize (speed 3) (safety 0) (space 0)))
250   (loop for n-pieces from 0 to 10 do 
251         (map-edge-n-pieces
252          #'(lambda (board index)
253              (declare (type board board)
254                       (fixnum index))
255               (setf (aref *edge-table* index)
256                     (the fixnum (static-edge-stability black board))))
257          black (initial-board) n-pieces *top-edge* 0))
258   ;; Now iterate five times trying to improve:
259   (dotimes (i 5) 
260     (declare (fixnum i))
261     ;; Do the indexes with most pieces first
262     (loop for n-pieces fixnum from 9 downto 1 do 
263           (map-edge-n-pieces
264             #'(lambda (board index)
265                 (declare (type board board)
266                          (fixnum index))
267                 (setf (aref *edge-table* index)
268                       (the fixnum (possible-edge-moves-value black board index))))
269             black (initial-board) n-pieces *top-edge* 0))))
270