0
|
1 ;;; gomoku.el --- Gomoku game between you and Emacs
|
|
2
|
|
3 ;; Copyright (C) 1988, 1994 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Philippe Schnoebelen <phs@lifia.imag.fr>
|
4
|
6 ;; Adapted-By: ESR, Daniel.Pfeiffer@Informatik.START.dbp.de
|
0
|
7 ;; Keywords: games
|
|
8
|
|
9 ;; This file is part of XEmacs.
|
|
10
|
|
11 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
12 ;; under the terms of the GNU General Public License as published by
|
|
13 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
14 ;; any later version.
|
|
15
|
|
16 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
|
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
4
|
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
24 ;; 02111-1307, USA.
|
0
|
25
|
4
|
26 ;;; Synched up with: FSF 19.34.
|
0
|
27
|
|
28 ;;; Commentary:
|
|
29
|
|
30 ;; RULES:
|
|
31 ;;
|
|
32 ;; Gomoku is a game played between two players on a rectangular board. Each
|
|
33 ;; player, in turn, marks a free square of its choice. The winner is the first
|
|
34 ;; one to mark five contiguous squares in any direction (horizontally,
|
|
35 ;; vertically or diagonally).
|
|
36 ;;
|
|
37 ;; I have been told that, in "The TRUE Gomoku", some restrictions are made
|
|
38 ;; about the squares where one may play, or else there is a known forced win
|
|
39 ;; for the first player. This program has no such restriction, but it does not
|
|
40 ;; know about the forced win, nor do I. Furthermore, you probably do not know
|
|
41 ;; it yourself :-).
|
|
42
|
|
43
|
|
44 ;; There are two main places where you may want to customize the program: key
|
|
45 ;; bindings and board display. These features are commented in the code. Go
|
|
46 ;; and see.
|
|
47
|
|
48
|
|
49 ;; HOW TO USE:
|
|
50 ;;
|
|
51 ;; The command "M-x gomoku" displays a
|
|
52 ;; board, the size of which depends on the size of the current window. The
|
|
53 ;; size of the board is easily modified by giving numeric arguments to the
|
|
54 ;; gomoku command and/or by customizing the displaying parameters.
|
|
55 ;;
|
|
56 ;; Emacs plays when it is its turn. When it is your turn, just put the cursor
|
|
57 ;; on the square where you want to play and hit RET, or X, or whatever key you
|
|
58 ;; bind to the command gomoku-human-plays. When it is your turn, Emacs is
|
|
59 ;; idle: you may switch buffers, read your mail, ... Just come back to the
|
|
60 ;; *Gomoku* buffer and resume play.
|
|
61
|
|
62
|
|
63 ;; ALGORITHM:
|
|
64 ;;
|
|
65 ;; The algorithm is briefly described in section "THE SCORE TABLE". Some
|
|
66 ;; parameters may be modified if you want to change the style exhibited by the
|
|
67 ;; program.
|
|
68
|
|
69 ;;; Code:
|
|
70
|
|
71 ;;;
|
|
72 ;;; GOMOKU MODE AND KEYMAP.
|
|
73 ;;;
|
4
|
74 (require 'facemenu)
|
|
75
|
0
|
76 (defvar gomoku-mode-hook nil
|
|
77 "If non-nil, its value is called on entry to Gomoku mode.")
|
|
78
|
|
79 (defvar gomoku-mode-map nil
|
|
80 "Local keymap to use in Gomoku mode.")
|
|
81
|
4
|
82 (if gomoku-mode-map nil
|
0
|
83 (setq gomoku-mode-map (make-sparse-keymap))
|
|
84 (set-keymap-name gomoku-mode-map 'gomoku-mode-map)
|
|
85
|
4
|
86 ;; Key bindings for cursor motion.
|
|
87 (define-key gomoku-mode-map "y" 'gomoku-move-nw) ; y
|
|
88 (define-key gomoku-mode-map "u" 'gomoku-move-ne) ; u
|
|
89 (define-key gomoku-mode-map "b" 'gomoku-move-sw) ; b
|
|
90 (define-key gomoku-mode-map "n" 'gomoku-move-se) ; n
|
|
91 (define-key gomoku-mode-map "h" 'backward-char) ; h
|
|
92 (define-key gomoku-mode-map "l" 'forward-char) ; l
|
|
93 (define-key gomoku-mode-map "j" 'gomoku-move-down) ; j
|
|
94 (define-key gomoku-mode-map "k" 'gomoku-move-up) ; k
|
|
95
|
|
96 (define-key gomoku-mode-map [kp-7] 'gomoku-move-nw)
|
|
97 (define-key gomoku-mode-map [kp-9] 'gomoku-move-ne)
|
|
98 (define-key gomoku-mode-map [kp-1] 'gomoku-move-sw)
|
|
99 (define-key gomoku-mode-map [kp-3] 'gomoku-move-se)
|
|
100 (define-key gomoku-mode-map [kp-4] 'backward-char)
|
|
101 (define-key gomoku-mode-map [kp-6] 'forward-char)
|
|
102 (define-key gomoku-mode-map [kp-2] 'gomoku-move-down)
|
|
103 (define-key gomoku-mode-map [kp-8] 'gomoku-move-up)
|
|
104
|
|
105 (define-key gomoku-mode-map "\C-n" 'gomoku-move-down) ; C-n
|
|
106 (define-key gomoku-mode-map "\C-p" 'gomoku-move-up) ; C-p
|
0
|
107
|
|
108 ;; Key bindings for entering Human moves.
|
|
109 (define-key gomoku-mode-map "X" 'gomoku-human-plays) ; X
|
|
110 (define-key gomoku-mode-map "x" 'gomoku-human-plays) ; x
|
4
|
111 (define-key gomoku-mode-map " " 'gomoku-human-plays) ; SPC
|
0
|
112 (define-key gomoku-mode-map "\C-m" 'gomoku-human-plays) ; RET
|
4
|
113 (define-key gomoku-mode-map "\C-c\C-p" 'gomoku-human-plays) ; C-c C-p
|
|
114 (define-key gomoku-mode-map "\C-c\C-b" 'gomoku-human-takes-back) ; C-c C-b
|
|
115 (define-key gomoku-mode-map "\C-c\C-r" 'gomoku-human-resigns) ; C-c C-r
|
|
116 (define-key gomoku-mode-map "\C-c\C-e" 'gomoku-emacs-plays) ; C-c C-e
|
|
117
|
|
118 (define-key gomoku-mode-map [kp-enter] 'gomoku-human-plays)
|
|
119 (define-key gomoku-mode-map [insert] 'gomoku-human-plays)
|
|
120 (define-key gomoku-mode-map [down-mouse-1] 'gomoku-click)
|
|
121 (define-key gomoku-mode-map [drag-mouse-1] 'gomoku-click)
|
|
122 (define-key gomoku-mode-map [mouse-1] 'gomoku-click)
|
|
123 (define-key gomoku-mode-map [down-mouse-2] 'gomoku-click)
|
|
124 (define-key gomoku-mode-map [mouse-2] 'gomoku-mouse-play)
|
|
125 (define-key gomoku-mode-map [drag-mouse-2] 'gomoku-mouse-play)
|
0
|
126
|
4
|
127 (substitute-key-definition 'previous-line 'gomoku-move-up
|
|
128 gomoku-mode-map (current-global-map))
|
|
129 (substitute-key-definition 'next-line 'gomoku-move-down
|
|
130 gomoku-mode-map (current-global-map))
|
|
131 (substitute-key-definition 'beginning-of-line 'gomoku-beginning-of-line
|
|
132 gomoku-mode-map (current-global-map))
|
|
133 (substitute-key-definition 'end-of-line 'gomoku-end-of-line
|
|
134 gomoku-mode-map (current-global-map))
|
|
135 (substitute-key-definition 'undo 'gomoku-human-takes-back
|
|
136 gomoku-mode-map (current-global-map))
|
|
137 (substitute-key-definition 'advertised-undo 'gomoku-human-takes-back
|
|
138 gomoku-mode-map (current-global-map)))
|
|
139
|
|
140 (defvar gomoku-emacs-won ()
|
|
141 "*For making font-lock use the winner's face for the line.")
|
0
|
142
|
4
|
143 (defvar gomoku-font-lock-O-face
|
|
144 (if window-system
|
|
145 (list (facemenu-get-face 'fg:red) 'bold))
|
|
146 "*Face to use for Emacs' O.")
|
|
147
|
|
148 (defvar gomoku-font-lock-X-face
|
|
149 (if window-system
|
|
150 (list (facemenu-get-face 'fg:green) 'bold))
|
|
151 "*Face to use for your X.")
|
|
152
|
|
153 (defvar gomoku-font-lock-keywords
|
|
154 '(("O" . gomoku-font-lock-O-face)
|
|
155 ("X" . gomoku-font-lock-X-face)
|
|
156 ("[-|/\\]" 0 (if gomoku-emacs-won
|
|
157 gomoku-font-lock-O-face
|
|
158 gomoku-font-lock-X-face)))
|
|
159 "*Font lock rules for Gomoku.")
|
|
160
|
|
161 (put 'gomoku-mode 'front-sticky
|
|
162 (put 'gomoku-mode 'rear-nonsticky '(intangible)))
|
|
163 (put 'gomoku-mode 'intangible 1)
|
0
|
164
|
|
165 (defun gomoku-mode ()
|
|
166 "Major mode for playing Gomoku against Emacs.
|
4
|
167 You and Emacs play in turn by marking a free square. You mark it with X
|
|
168 and Emacs marks it with O. The winner is the first to get five contiguous
|
0
|
169 marks horizontally, vertically or in diagonal.
|
4
|
170
|
0
|
171 You play by moving the cursor over the square you choose and hitting \\[gomoku-human-plays].
|
4
|
172
|
0
|
173 Other useful commands:
|
|
174 \\{gomoku-mode-map}
|
|
175 Entry to this mode calls the value of `gomoku-mode-hook' if that value
|
4
|
176 is non-nil. One interesting value is `turn-on-font-lock'."
|
0
|
177 (interactive)
|
|
178 (setq major-mode 'gomoku-mode
|
|
179 mode-name "Gomoku")
|
|
180 (gomoku-display-statistics)
|
|
181 (use-local-map gomoku-mode-map)
|
4
|
182 (make-local-variable 'font-lock-defaults)
|
|
183 (setq font-lock-defaults '(gomoku-font-lock-keywords t))
|
|
184 (toggle-read-only t)
|
0
|
185 (run-hooks 'gomoku-mode-hook))
|
|
186
|
|
187 ;;;
|
|
188 ;;; THE BOARD.
|
|
189 ;;;
|
|
190
|
|
191 ;; The board is a rectangular grid. We code empty squares with 0, X's with 1
|
|
192 ;; and O's with 6. The rectangle is recorded in a one dimensional vector
|
|
193 ;; containing padding squares (coded with -1). These squares allow us to
|
|
194 ;; detect when we are trying to move out of the board. We denote a square by
|
|
195 ;; its (X,Y) coords, or by the INDEX corresponding to them in the vector. The
|
|
196 ;; leftmost topmost square has coords (1,1) and index gomoku-board-width + 2.
|
|
197 ;; Similarly, vectors between squares may be given by two DX, DY coords or by
|
|
198 ;; one DEPL (the difference between indexes).
|
|
199
|
|
200 (defvar gomoku-board-width nil
|
|
201 "Number of columns on the Gomoku board.")
|
|
202
|
|
203 (defvar gomoku-board-height nil
|
|
204 "Number of lines on the Gomoku board.")
|
|
205
|
|
206 (defvar gomoku-board nil
|
|
207 "Vector recording the actual state of the Gomoku board.")
|
|
208
|
|
209 (defvar gomoku-vector-length nil
|
|
210 "Length of gomoku-board vector.")
|
|
211
|
|
212 (defvar gomoku-draw-limit nil
|
|
213 ;; This is usually set to 70% of the number of squares.
|
|
214 "After how many moves will Emacs offer a draw?")
|
|
215
|
|
216
|
|
217 (defun gomoku-xy-to-index (x y)
|
|
218 "Translate X, Y cartesian coords into the corresponding board index."
|
|
219 (+ (* y gomoku-board-width) x y))
|
|
220
|
|
221 (defun gomoku-index-to-x (index)
|
|
222 "Return corresponding x-coord of board INDEX."
|
|
223 (% index (1+ gomoku-board-width)))
|
|
224
|
|
225 (defun gomoku-index-to-y (index)
|
|
226 "Return corresponding y-coord of board INDEX."
|
|
227 (/ index (1+ gomoku-board-width)))
|
|
228
|
|
229 (defun gomoku-init-board ()
|
|
230 "Create the gomoku-board vector and fill it with initial values."
|
|
231 (setq gomoku-board (make-vector gomoku-vector-length 0))
|
|
232 ;; Every square is 0 (i.e. empty) except padding squares:
|
|
233 (let ((i 0) (ii (1- gomoku-vector-length)))
|
|
234 (while (<= i gomoku-board-width) ; The squares in [0..width] and in
|
|
235 (aset gomoku-board i -1) ; [length - width - 1..length - 1]
|
|
236 (aset gomoku-board ii -1) ; are padding squares.
|
|
237 (setq i (1+ i)
|
|
238 ii (1- ii))))
|
|
239 (let ((i 0))
|
|
240 (while (< i gomoku-vector-length)
|
|
241 (aset gomoku-board i -1) ; and also all k*(width+1)
|
|
242 (setq i (+ i gomoku-board-width 1)))))
|
|
243
|
|
244 ;;;
|
|
245 ;;; THE SCORE TABLE.
|
|
246 ;;;
|
|
247
|
|
248 ;; Every (free) square has a score associated to it, recorded in the
|
|
249 ;; GOMOKU-SCORE-TABLE vector. The program always plays in the square having
|
|
250 ;; the highest score.
|
|
251
|
|
252 (defvar gomoku-score-table nil
|
|
253 "Vector recording the actual score of the free squares.")
|
|
254
|
|
255
|
|
256 ;; The key point point about the algorithm is that, rather than considering
|
|
257 ;; the board as just a set of squares, we prefer to see it as a "space" of
|
|
258 ;; internested 5-tuples of contiguous squares (called qtuples).
|
|
259 ;;
|
|
260 ;; The aim of the program is to fill one qtuple with its O's while preventing
|
|
261 ;; you from filling another one with your X's. To that effect, it computes a
|
|
262 ;; score for every qtuple, with better qtuples having better scores. Of
|
|
263 ;; course, the score of a qtuple (taken in isolation) is just determined by
|
|
264 ;; its contents as a set, i.e. not considering the order of its elements. The
|
|
265 ;; highest score is given to the "OOOO" qtuples because playing in such a
|
|
266 ;; qtuple is winning the game. Just after this comes the "XXXX" qtuple because
|
|
267 ;; not playing in it is just loosing the game, and so on. Note that a
|
|
268 ;; "polluted" qtuple, i.e. one containing at least one X and at least one O,
|
|
269 ;; has score zero because there is no more any point in playing in it, from
|
|
270 ;; both an attacking and a defending point of view.
|
|
271 ;;
|
|
272 ;; Given the score of every qtuple, the score of a given free square on the
|
|
273 ;; board is just the sum of the scores of all the qtuples to which it belongs,
|
|
274 ;; because playing in that square is playing in all its containing qtuples at
|
|
275 ;; once. And it is that function which takes into account the internesting of
|
|
276 ;; the qtuples.
|
|
277 ;;
|
|
278 ;; This algorithm is rather simple but anyway it gives a not so dumb level of
|
|
279 ;; play. It easily extends to "n-dimensional Gomoku", where a win should not
|
|
280 ;; be obtained with as few as 5 contiguous marks: 6 or 7 (depending on n !)
|
|
281 ;; should be preferred.
|
|
282
|
|
283
|
|
284 ;; Here are the scores of the nine "non-polluted" configurations. Tuning
|
|
285 ;; these values will change (hopefully improve) the strength of the program
|
|
286 ;; and may change its style (rather aggressive here).
|
|
287
|
|
288 (defconst nil-score 7 "Score of an empty qtuple.")
|
|
289 (defconst Xscore 15 "Score of a qtuple containing one X.")
|
|
290 (defconst XXscore 400 "Score of a qtuple containing two X's.")
|
|
291 (defconst XXXscore 1800 "Score of a qtuple containing three X's.")
|
|
292 (defconst XXXXscore 100000 "Score of a qtuple containing four X's.")
|
|
293 (defconst Oscore 35 "Score of a qtuple containing one O.")
|
|
294 (defconst OOscore 800 "Score of a qtuple containing two O's.")
|
|
295 (defconst OOOscore 15000 "Score of a qtuple containing three O's.")
|
|
296 (defconst OOOOscore 800000 "Score of a qtuple containing four O's.")
|
|
297
|
|
298 ;; These values are not just random: if, given the following situation:
|
|
299 ;;
|
|
300 ;; . . . . . . . O .
|
|
301 ;; . X X a . . . X .
|
|
302 ;; . . . X . . . X .
|
|
303 ;; . . . X . . . X .
|
|
304 ;; . . . . . . . b .
|
|
305 ;;
|
|
306 ;; you want Emacs to play in "a" and not in "b", then the parameters must
|
|
307 ;; satisfy the inequality:
|
|
308 ;;
|
|
309 ;; 6 * XXscore > XXXscore + XXscore
|
|
310 ;;
|
|
311 ;; because "a" mainly belongs to six "XX" qtuples (the others are less
|
|
312 ;; important) while "b" belongs to one "XXX" and one "XX" qtuples. Other
|
|
313 ;; conditions are required to obtain sensible moves, but the previous example
|
|
314 ;; should illustrate the point. If you manage to improve on these values,
|
|
315 ;; please send me a note. Thanks.
|
|
316
|
|
317
|
4
|
318 ;; As we chose values 0, 1 and 6 to denote empty, X and O squares, the
|
|
319 ;; contents of a qtuple are uniquely determined by the sum of its elements and
|
0
|
320 ;; we just have to set up a translation table.
|
|
321
|
|
322 (defconst gomoku-score-trans-table
|
|
323 (vector nil-score Xscore XXscore XXXscore XXXXscore 0
|
|
324 Oscore 0 0 0 0 0
|
|
325 OOscore 0 0 0 0 0
|
|
326 OOOscore 0 0 0 0 0
|
|
327 OOOOscore 0 0 0 0 0
|
|
328 0)
|
|
329 "Vector associating qtuple contents to their score.")
|
|
330
|
|
331
|
|
332 ;; If you do not modify drastically the previous constants, the only way for a
|
|
333 ;; square to have a score higher than OOOOscore is to belong to a "OOOO"
|
|
334 ;; qtuple, thus to be a winning move. Similarly, the only way for a square to
|
|
335 ;; have a score between XXXXscore and OOOOscore is to belong to a "XXXX"
|
|
336 ;; qtuple. We may use these considerations to detect when a given move is
|
|
337 ;; winning or loosing.
|
|
338
|
|
339 (defconst gomoku-winning-threshold OOOOscore
|
|
340 "Threshold score beyond which an Emacs move is winning.")
|
|
341
|
|
342 (defconst gomoku-loosing-threshold XXXXscore
|
|
343 "Threshold score beyond which a human move is winning.")
|
|
344
|
|
345
|
|
346 (defun gomoku-strongest-square ()
|
|
347 "Compute index of free square with highest score, or nil if none."
|
|
348 ;; We just have to loop other all squares. However there are two problems:
|
|
349 ;; 1/ The SCORE-TABLE only gives correct scores to free squares. To speed
|
|
350 ;; up future searches, we set the score of padding or occupied squares
|
|
351 ;; to -1 whenever we meet them.
|
|
352 ;; 2/ We want to choose randomly between equally good moves.
|
|
353 (let ((score-max 0)
|
|
354 (count 0) ; Number of equally good moves
|
|
355 (square (gomoku-xy-to-index 1 1)) ; First square
|
|
356 (end (gomoku-xy-to-index gomoku-board-width gomoku-board-height))
|
|
357 best-square score)
|
|
358 (while (<= square end)
|
|
359 (cond
|
|
360 ;; If score is lower (i.e. most of the time), skip to next:
|
|
361 ((< (aref gomoku-score-table square) score-max))
|
|
362 ;; If score is better, beware of non free squares:
|
|
363 ((> (setq score (aref gomoku-score-table square)) score-max)
|
|
364 (if (zerop (aref gomoku-board square)) ; is it free ?
|
|
365 (setq count 1 ; yes: take it !
|
|
366 best-square square
|
|
367 score-max score)
|
|
368 (aset gomoku-score-table square -1))) ; no: kill it !
|
|
369 ;; If score is equally good, choose randomly. But first check freeness:
|
|
370 ((not (zerop (aref gomoku-board square)))
|
|
371 (aset gomoku-score-table square -1))
|
|
372 ((zerop (random (setq count (1+ count))))
|
|
373 (setq best-square square
|
|
374 score-max score)))
|
|
375 (setq square (1+ square))) ; try next square
|
|
376 best-square))
|
|
377
|
|
378 ;;;
|
|
379 ;;; INITIALIZING THE SCORE TABLE.
|
|
380 ;;;
|
|
381
|
|
382 ;; At initialization the board is empty so that every qtuple amounts for
|
|
383 ;; nil-score. Therefore, the score of any square is nil-score times the number
|
|
384 ;; of qtuples that pass through it. This number is 3 in a corner and 20 if you
|
|
385 ;; are sufficiently far from the sides. As computing the number is time
|
|
386 ;; consuming, we initialize every square with 20*nil-score and then only
|
|
387 ;; consider squares at less than 5 squares from one side. We speed this up by
|
|
388 ;; taking symmetry into account.
|
|
389 ;; Also, as it is likely that successive games will be played on a board with
|
|
390 ;; same size, it is a good idea to save the initial SCORE-TABLE configuration.
|
|
391
|
|
392 (defvar gomoku-saved-score-table nil
|
|
393 "Recorded initial value of previous score table.")
|
|
394
|
|
395 (defvar gomoku-saved-board-width nil
|
|
396 "Recorded value of previous board width.")
|
|
397
|
|
398 (defvar gomoku-saved-board-height nil
|
|
399 "Recorded value of previous board height.")
|
|
400
|
|
401
|
|
402 (defun gomoku-init-score-table ()
|
|
403 "Create the score table vector and fill it with initial values."
|
|
404 (if (and gomoku-saved-score-table ; Has it been stored last time ?
|
|
405 (= gomoku-board-width gomoku-saved-board-width)
|
|
406 (= gomoku-board-height gomoku-saved-board-height))
|
|
407 (setq gomoku-score-table (copy-sequence gomoku-saved-score-table))
|
|
408 ;; No, compute it:
|
|
409 (setq gomoku-score-table
|
|
410 (make-vector gomoku-vector-length (* 20 nil-score)))
|
|
411 (let (i j maxi maxj maxi2 maxj2)
|
|
412 (setq maxi (/ (1+ gomoku-board-width) 2)
|
|
413 maxj (/ (1+ gomoku-board-height) 2)
|
|
414 maxi2 (min 4 maxi)
|
|
415 maxj2 (min 4 maxj))
|
|
416 ;; We took symmetry into account and could use it more if the board
|
|
417 ;; would have been square and not rectangular !
|
|
418 ;; In our case we deal with all (i,j) in the set [1..maxi2]*[1..maxj] U
|
|
419 ;; [maxi2+1..maxi]*[1..maxj2]. Maxi2 and maxj2 are used because the
|
|
420 ;; board may well be less than 8 by 8 !
|
|
421 (setq i 1)
|
|
422 (while (<= i maxi2)
|
|
423 (setq j 1)
|
|
424 (while (<= j maxj)
|
|
425 (gomoku-init-square-score i j)
|
|
426 (setq j (1+ j)))
|
|
427 (setq i (1+ i)))
|
|
428 (while (<= i maxi)
|
|
429 (setq j 1)
|
|
430 (while (<= j maxj2)
|
|
431 (gomoku-init-square-score i j)
|
|
432 (setq j (1+ j)))
|
|
433 (setq i (1+ i))))
|
|
434 (setq gomoku-saved-score-table (copy-sequence gomoku-score-table)
|
|
435 gomoku-saved-board-width gomoku-board-width
|
|
436 gomoku-saved-board-height gomoku-board-height)))
|
|
437
|
|
438 (defun gomoku-nb-qtuples (i j)
|
|
439 "Return the number of qtuples containing square I,J."
|
|
440 ;; This function is complicated because we have to deal
|
|
441 ;; with ugly cases like 3 by 6 boards, but it works.
|
|
442 ;; If you have a simpler (and correct) solution, send it to me. Thanks !
|
|
443 (let ((left (min 4 (1- i)))
|
|
444 (right (min 4 (- gomoku-board-width i)))
|
|
445 (up (min 4 (1- j)))
|
|
446 (down (min 4 (- gomoku-board-height j))))
|
|
447 (+ -12
|
|
448 (min (max (+ left right) 3) 8)
|
|
449 (min (max (+ up down) 3) 8)
|
|
450 (min (max (+ (min left up) (min right down)) 3) 8)
|
|
451 (min (max (+ (min right up) (min left down)) 3) 8))))
|
|
452
|
|
453 (defun gomoku-init-square-score (i j)
|
|
454 "Give initial score to square I,J and to its mirror images."
|
|
455 (let ((ii (1+ (- gomoku-board-width i)))
|
|
456 (jj (1+ (- gomoku-board-height j)))
|
|
457 (sc (* (gomoku-nb-qtuples i j) (aref gomoku-score-trans-table 0))))
|
|
458 (aset gomoku-score-table (gomoku-xy-to-index i j) sc)
|
|
459 (aset gomoku-score-table (gomoku-xy-to-index ii j) sc)
|
|
460 (aset gomoku-score-table (gomoku-xy-to-index i jj) sc)
|
|
461 (aset gomoku-score-table (gomoku-xy-to-index ii jj) sc)))
|
|
462
|
|
463 ;;;
|
|
464 ;;; MAINTAINING THE SCORE TABLE.
|
|
465 ;;;
|
|
466
|
|
467 ;; We do not provide functions for computing the SCORE-TABLE given the
|
|
468 ;; contents of the BOARD. This would involve heavy nested loops, with time
|
|
469 ;; proportional to the size of the board. It is better to update the
|
|
470 ;; SCORE-TABLE after each move. Updating needs not modify more than 36
|
|
471 ;; squares: it is done in constant time.
|
|
472
|
|
473 (defun gomoku-update-score-table (square dval)
|
|
474 "Update score table after SQUARE received a DVAL increment."
|
|
475 ;; The board has already been updated when this function is called.
|
|
476 ;; Updating scores is done by looking for qtuples boundaries in all four
|
|
477 ;; directions and then calling update-score-in-direction.
|
|
478 ;; Finally all squares received the right increment, and then are up to
|
|
479 ;; date, except possibly for SQUARE itself if we are taking a move back for
|
|
480 ;; its score had been set to -1 at the time.
|
|
481 (let* ((x (gomoku-index-to-x square))
|
|
482 (y (gomoku-index-to-y square))
|
|
483 (imin (max -4 (- 1 x)))
|
|
484 (jmin (max -4 (- 1 y)))
|
|
485 (imax (min 0 (- gomoku-board-width x 4)))
|
|
486 (jmax (min 0 (- gomoku-board-height y 4))))
|
|
487 (gomoku-update-score-in-direction imin imax
|
|
488 square 1 0 dval)
|
|
489 (gomoku-update-score-in-direction jmin jmax
|
|
490 square 0 1 dval)
|
|
491 (gomoku-update-score-in-direction (max imin jmin) (min imax jmax)
|
|
492 square 1 1 dval)
|
|
493 (gomoku-update-score-in-direction (max (- 1 y) -4
|
|
494 (- x gomoku-board-width))
|
|
495 (min 0 (- x 5)
|
|
496 (- gomoku-board-height y 4))
|
|
497 square -1 1 dval)))
|
|
498
|
|
499 (defun gomoku-update-score-in-direction (left right square dx dy dval)
|
|
500 "Update scores for all squares in the qtuples starting between the LEFTth
|
|
501 square and the RIGHTth after SQUARE, along the DX, DY direction, considering
|
|
502 that DVAL has been added on SQUARE."
|
|
503 ;; We always have LEFT <= 0, RIGHT <= 0 and DEPL > 0 but we may very well
|
|
504 ;; have LEFT > RIGHT, indicating that no qtuple contains SQUARE along that
|
|
505 ;; DX,DY direction.
|
|
506 (cond
|
|
507 ((> left right)) ; Quit
|
|
508 (t ; Else ..
|
|
509 (let (depl square0 square1 square2 count delta)
|
|
510 (setq depl (gomoku-xy-to-index dx dy)
|
|
511 square0 (+ square (* left depl))
|
|
512 square1 (+ square (* right depl))
|
|
513 square2 (+ square0 (* 4 depl)))
|
|
514 ;; Compute the contents of the first qtuple:
|
|
515 (setq square square0
|
|
516 count 0)
|
|
517 (while (<= square square2)
|
|
518 (setq count (+ count (aref gomoku-board square))
|
|
519 square (+ square depl)))
|
|
520 (while (<= square0 square1)
|
|
521 ;; Update the squares of the qtuple beginning in SQUARE0 and ending
|
|
522 ;; in SQUARE2.
|
|
523 (setq delta (- (aref gomoku-score-trans-table count)
|
|
524 (aref gomoku-score-trans-table (- count dval))))
|
|
525 (cond ((not (zerop delta)) ; or else nothing to update
|
|
526 (setq square square0)
|
|
527 (while (<= square square2)
|
|
528 (if (zerop (aref gomoku-board square)) ; only for free squares
|
|
529 (aset gomoku-score-table square
|
|
530 (+ (aref gomoku-score-table square) delta)))
|
|
531 (setq square (+ square depl)))))
|
|
532 ;; Then shift the qtuple one square along DEPL, this only requires
|
|
533 ;; modifying SQUARE0 and SQUARE2.
|
|
534 (setq square2 (+ square2 depl)
|
|
535 count (+ count (- (aref gomoku-board square0))
|
|
536 (aref gomoku-board square2))
|
|
537 square0 (+ square0 depl)))))))
|
|
538
|
|
539 ;;;
|
|
540 ;;; GAME CONTROL.
|
|
541 ;;;
|
|
542
|
|
543 ;; Several variables are used to monitor a game, including a GAME-HISTORY (the
|
|
544 ;; list of all (SQUARE . PREVSCORE) played) that allows to take moves back
|
|
545 ;; (anti-updating the score table) and to compute the table from scratch in
|
|
546 ;; case of an interruption.
|
|
547
|
|
548 (defvar gomoku-game-in-progress nil
|
|
549 "Non-nil if a game is in progress.")
|
|
550
|
|
551 (defvar gomoku-game-history nil
|
|
552 "A record of all moves that have been played during current game.")
|
|
553
|
|
554 (defvar gomoku-number-of-moves nil
|
|
555 "Number of moves already played in current game.")
|
|
556
|
|
557 (defvar gomoku-number-of-human-moves nil
|
|
558 "Number of moves already played by human in current game.")
|
|
559
|
|
560 (defvar gomoku-emacs-played-first nil
|
|
561 "Non-nil if Emacs played first.")
|
|
562
|
|
563 (defvar gomoku-human-took-back nil
|
|
564 "Non-nil if Human took back a move during the game.")
|
|
565
|
|
566 (defvar gomoku-human-refused-draw nil
|
|
567 "Non-nil if Human refused Emacs offer of a draw.")
|
|
568
|
|
569 (defvar gomoku-emacs-is-computing nil
|
|
570 ;; This is used to detect interruptions. Hopefully, it should not be needed.
|
|
571 "Non-nil if Emacs is in the middle of a computation.")
|
|
572
|
|
573
|
|
574 (defun gomoku-start-game (n m)
|
|
575 "Initialize a new game on an N by M board."
|
|
576 (setq gomoku-emacs-is-computing t) ; Raise flag
|
|
577 (setq gomoku-game-in-progress t)
|
|
578 (setq gomoku-board-width n
|
|
579 gomoku-board-height m
|
|
580 gomoku-vector-length (1+ (* (+ m 2) (1+ n)))
|
|
581 gomoku-draw-limit (/ (* 7 n m) 10))
|
4
|
582 (setq gomuku emacs-won nil
|
|
583 gomoku-game-history nil
|
0
|
584 gomoku-number-of-moves 0
|
|
585 gomoku-number-of-human-moves 0
|
|
586 gomoku-emacs-played-first nil
|
|
587 gomoku-human-took-back nil
|
|
588 gomoku-human-refused-draw nil)
|
|
589 (gomoku-init-display n m) ; Display first: the rest takes time
|
|
590 (gomoku-init-score-table) ; INIT-BOARD requires that the score
|
|
591 (gomoku-init-board) ; table be already created.
|
|
592 (setq gomoku-emacs-is-computing nil))
|
|
593
|
|
594 (defun gomoku-play-move (square val &optional dont-update-score)
|
|
595 "Go to SQUARE, play VAL and update everything."
|
|
596 (setq gomoku-emacs-is-computing t) ; Raise flag
|
|
597 (cond ((= 1 val) ; a Human move
|
|
598 (setq gomoku-number-of-human-moves (1+ gomoku-number-of-human-moves)))
|
|
599 ((zerop gomoku-number-of-moves) ; an Emacs move. Is it first ?
|
|
600 (setq gomoku-emacs-played-first t)))
|
|
601 (setq gomoku-game-history
|
|
602 (cons (cons square (aref gomoku-score-table square))
|
|
603 gomoku-game-history)
|
|
604 gomoku-number-of-moves (1+ gomoku-number-of-moves))
|
|
605 (gomoku-plot-square square val)
|
|
606 (aset gomoku-board square val) ; *BEFORE* UPDATE-SCORE !
|
|
607 (if dont-update-score nil
|
|
608 (gomoku-update-score-table square val) ; previous val was 0: dval = val
|
|
609 (aset gomoku-score-table square -1))
|
|
610 (setq gomoku-emacs-is-computing nil))
|
|
611
|
|
612 (defun gomoku-take-back ()
|
|
613 "Take back last move and update everything."
|
|
614 (setq gomoku-emacs-is-computing t)
|
|
615 (let* ((last-move (car gomoku-game-history))
|
|
616 (square (car last-move))
|
|
617 (oldval (aref gomoku-board square)))
|
|
618 (if (= 1 oldval)
|
|
619 (setq gomoku-number-of-human-moves (1- gomoku-number-of-human-moves)))
|
|
620 (setq gomoku-game-history (cdr gomoku-game-history)
|
|
621 gomoku-number-of-moves (1- gomoku-number-of-moves))
|
|
622 (gomoku-plot-square square 0)
|
|
623 (aset gomoku-board square 0) ; *BEFORE* UPDATE-SCORE !
|
|
624 (gomoku-update-score-table square (- oldval))
|
|
625 (aset gomoku-score-table square (cdr last-move)))
|
|
626 (setq gomoku-emacs-is-computing nil))
|
|
627
|
|
628 ;;;
|
|
629 ;;; SESSION CONTROL.
|
|
630 ;;;
|
|
631
|
|
632 (defvar gomoku-number-of-emacs-wins 0
|
|
633 "Number of games Emacs won in this session.")
|
|
634
|
|
635 (defvar gomoku-number-of-human-wins 0
|
|
636 "Number of games you won in this session.")
|
|
637
|
|
638 (defvar gomoku-number-of-draws 0
|
|
639 "Number of games already drawn in this session.")
|
|
640
|
|
641
|
|
642 (defun gomoku-terminate-game (result)
|
|
643 "Terminate the current game with RESULT."
|
4
|
644 (message
|
|
645 (cond
|
|
646 ((eq result 'emacs-won)
|
|
647 (setq gomoku-number-of-emacs-wins (1+ gomoku-number-of-emacs-wins))
|
|
648 (cond ((< gomoku-number-of-moves 20)
|
|
649 "This was a REALLY QUICK win.")
|
|
650 (gomoku-human-refused-draw
|
|
651 "I won... Too bad you refused my offer of a draw !")
|
|
652 (gomoku-human-took-back
|
|
653 "I won... Taking moves back will not help you !")
|
|
654 ((not gomoku-emacs-played-first)
|
|
655 "I won... Playing first did not help you much !")
|
|
656 ((and (zerop gomoku-number-of-human-wins)
|
|
657 (zerop gomoku-number-of-draws)
|
|
658 (> gomoku-number-of-emacs-wins 1))
|
|
659 "I'm becoming tired of winning...")
|
|
660 ("I won.")))
|
|
661 ((eq result 'human-won)
|
|
662 (setq gomoku-number-of-human-wins (1+ gomoku-number-of-human-wins))
|
|
663 (concat "OK, you won this one."
|
|
664 (cond
|
|
665 (gomoku-human-took-back
|
|
666 " I, for one, never take my moves back...")
|
|
667 (gomoku-emacs-played-first
|
|
668 ".. so what ?")
|
|
669 (" Now, let me play first just once."))))
|
|
670 ((eq result 'human-resigned)
|
|
671 (setq gomoku-number-of-emacs-wins (1+ gomoku-number-of-emacs-wins))
|
|
672 "So you resign. That's just one more win for me.")
|
|
673 ((eq result 'nobody-won)
|
|
674 (setq gomoku-number-of-draws (1+ gomoku-number-of-draws))
|
|
675 (concat "This is a draw. "
|
|
676 (cond
|
|
677 (gomoku-human-took-back
|
|
678 "I, for one, never take my moves back...")
|
|
679 (gomoku-emacs-played-first
|
|
680 "Just chance, I guess.")
|
|
681 ("Now, let me play first just once."))))
|
|
682 ((eq result 'draw-agreed)
|
|
683 (setq gomoku-number-of-draws (1+ gomoku-number-of-draws))
|
|
684 (concat "Draw agreed. "
|
|
685 (cond
|
|
686 (gomoku-human-took-back
|
|
687 "I, for one, never take my moves back...")
|
|
688 (gomoku-emacs-played-first
|
|
689 "You were lucky.")
|
|
690 ("Now, let me play first just once."))))
|
|
691 ((eq result 'crash-game)
|
|
692 "Sorry, I have been interrupted and cannot resume that game...")))
|
|
693 (gomoku-display-statistics)
|
|
694 ;;(ding)
|
|
695 (setq gomoku-game-in-progress nil))
|
0
|
696
|
|
697 (defun gomoku-crash-game ()
|
|
698 "What to do when Emacs detects it has been interrupted."
|
|
699 (setq gomoku-emacs-is-computing nil)
|
|
700 (gomoku-terminate-game 'crash-game)
|
|
701 (sit-for 4) ; Let's see the message
|
|
702 (gomoku-prompt-for-other-game))
|
|
703
|
|
704 ;;;
|
|
705 ;;; INTERACTIVE COMMANDS.
|
|
706 ;;;
|
|
707
|
|
708 ;;;###autoload
|
|
709 (defun gomoku (&optional n m)
|
|
710 "Start a Gomoku game between you and Emacs.
|
|
711 If a game is in progress, this command allow you to resume it.
|
|
712 If optional arguments N and M are given, an N by M board is used.
|
4
|
713 If prefix arg is given for N, M is prompted for.
|
0
|
714
|
4
|
715 You and Emacs play in turn by marking a free square. You mark it with X
|
0
|
716 and Emacs marks it with O. The winner is the first to get five contiguous
|
|
717 marks horizontally, vertically or in diagonal.
|
4
|
718
|
0
|
719 You play by moving the cursor over the square you choose and hitting
|
|
720 \\<gomoku-mode-map>\\[gomoku-human-plays].
|
|
721 Use \\[describe-mode] for more info."
|
4
|
722 (interactive (if current-prefix-arg
|
|
723 (list (prefix-numeric-value current-prefix-arg)
|
|
724 (eval (read-minibuffer "Height: ")))))
|
0
|
725 (gomoku-switch-to-window)
|
|
726 (cond
|
|
727 (gomoku-emacs-is-computing
|
|
728 (gomoku-crash-game))
|
4
|
729 ((or (not gomoku-game-in-progress)
|
|
730 (<= gomoku-number-of-moves 2))
|
0
|
731 (let ((max-width (gomoku-max-width))
|
|
732 (max-height (gomoku-max-height)))
|
|
733 (or n (setq n max-width))
|
|
734 (or m (setq m max-height))
|
|
735 (cond ((< n 1)
|
|
736 (error "I need at least 1 column"))
|
|
737 ((< m 1)
|
|
738 (error "I need at least 1 row"))
|
|
739 ((> n max-width)
|
|
740 (error "I cannot display %d columns in that window" n)))
|
|
741 (if (and (> m max-height)
|
4
|
742 (not (eq m gomoku-saved-board-height))
|
|
743 ;; Use EQ because SAVED-BOARD-HEIGHT may be nil
|
0
|
744 (not (y-or-n-p (format "Do you really want %d rows " m))))
|
|
745 (setq m max-height)))
|
|
746 (message "One moment, please...")
|
|
747 (gomoku-start-game n m)
|
|
748 (if (y-or-n-p "Do you allow me to play first ")
|
|
749 (gomoku-emacs-plays)
|
|
750 (gomoku-prompt-for-move)))
|
|
751 ((y-or-n-p "Shall we continue our game ")
|
|
752 (gomoku-prompt-for-move))
|
|
753 (t
|
|
754 (gomoku-human-resigns))))
|
|
755
|
|
756 (defun gomoku-emacs-plays ()
|
|
757 "Compute Emacs next move and play it."
|
|
758 (interactive)
|
|
759 (gomoku-switch-to-window)
|
|
760 (cond
|
|
761 (gomoku-emacs-is-computing
|
|
762 (gomoku-crash-game))
|
|
763 ((not gomoku-game-in-progress)
|
|
764 (gomoku-prompt-for-other-game))
|
|
765 (t
|
|
766 (message "Let me think...")
|
|
767 (let (square score)
|
|
768 (setq square (gomoku-strongest-square))
|
|
769 (cond ((null square)
|
|
770 (gomoku-terminate-game 'nobody-won))
|
|
771 (t
|
|
772 (setq score (aref gomoku-score-table square))
|
|
773 (gomoku-play-move square 6)
|
|
774 (cond ((>= score gomoku-winning-threshold)
|
4
|
775 (setq gomoku-emacs-won t) ; for font-lock
|
0
|
776 (gomoku-find-filled-qtuple square 6)
|
|
777 (gomoku-terminate-game 'emacs-won))
|
|
778 ((zerop score)
|
|
779 (gomoku-terminate-game 'nobody-won))
|
|
780 ((and (> gomoku-number-of-moves gomoku-draw-limit)
|
|
781 (not gomoku-human-refused-draw)
|
|
782 (gomoku-offer-a-draw))
|
|
783 (gomoku-terminate-game 'draw-agreed))
|
|
784 (t
|
|
785 (gomoku-prompt-for-move)))))))))
|
|
786
|
4
|
787 ;; For small square dimensions this is approximate, since though measured in
|
|
788 ;; pixels, event's (X . Y) is a character's top-left corner.
|
0
|
789 (defun gomoku-click (click)
|
4
|
790 "Position at the square where you click."
|
|
791 (interactive "e")
|
|
792 (and (windowp (posn-window (setq click (event-end click))))
|
|
793 (numberp (posn-point click))
|
|
794 (select-window (posn-window click))
|
|
795 (setq click (posn-col-row click))
|
|
796 (gomoku-goto-xy
|
|
797 (min (max (/ (+ (- (car click)
|
|
798 gomoku-x-offset
|
|
799 1)
|
|
800 (window-hscroll)
|
|
801 gomoku-square-width
|
|
802 (% gomoku-square-width 2)
|
|
803 (/ gomoku-square-width 2))
|
|
804 gomoku-square-width)
|
|
805 1)
|
|
806 gomoku-board-width)
|
|
807 (min (max (/ (+ (- (cdr click)
|
|
808 gomoku-y-offset
|
|
809 1)
|
|
810 (let ((inhibit-point-motion-hooks t))
|
|
811 (count-lines 1 (window-start)))
|
|
812 gomoku-square-height
|
|
813 (% gomoku-square-height 2)
|
|
814 (/ gomoku-square-height 2))
|
|
815 gomoku-square-height)
|
|
816 1)
|
|
817 gomoku-board-height))))
|
|
818
|
|
819 (defun gomoku-mouse-play (click)
|
0
|
820 "Play at the square where you click."
|
|
821 (interactive "e")
|
4
|
822 (if (gomoku-click click)
|
|
823 (gomoku-human-plays)))
|
0
|
824
|
|
825 (defun gomoku-human-plays ()
|
|
826 "Signal to the Gomoku program that you have played.
|
|
827 You must have put the cursor on the square where you want to play.
|
|
828 If the game is finished, this command requests for another game."
|
|
829 (interactive)
|
|
830 (gomoku-switch-to-window)
|
|
831 (cond
|
|
832 (gomoku-emacs-is-computing
|
|
833 (gomoku-crash-game))
|
|
834 ((not gomoku-game-in-progress)
|
|
835 (gomoku-prompt-for-other-game))
|
|
836 (t
|
|
837 (let (square score)
|
|
838 (setq square (gomoku-point-square))
|
|
839 (cond ((null square)
|
|
840 (error "Your point is not on a square. Retry !"))
|
|
841 ((not (zerop (aref gomoku-board square)))
|
|
842 (error "Your point is not on a free square. Retry !"))
|
|
843 (t
|
|
844 (setq score (aref gomoku-score-table square))
|
|
845 (gomoku-play-move square 1)
|
|
846 (cond ((and (>= score gomoku-loosing-threshold)
|
|
847 ;; Just testing SCORE > THRESHOLD is not enough for
|
|
848 ;; detecting wins, it just gives an indication that
|
|
849 ;; we confirm with GOMOKU-FIND-FILLED-QTUPLE.
|
|
850 (gomoku-find-filled-qtuple square 1))
|
|
851 (gomoku-terminate-game 'human-won))
|
|
852 (t
|
|
853 (gomoku-emacs-plays)))))))))
|
|
854
|
|
855 (defun gomoku-human-takes-back ()
|
|
856 "Signal to the Gomoku program that you wish to take back your last move."
|
|
857 (interactive)
|
|
858 (gomoku-switch-to-window)
|
|
859 (cond
|
|
860 (gomoku-emacs-is-computing
|
|
861 (gomoku-crash-game))
|
|
862 ((not gomoku-game-in-progress)
|
|
863 (message "Too late for taking back...")
|
|
864 (sit-for 4)
|
|
865 (gomoku-prompt-for-other-game))
|
|
866 ((zerop gomoku-number-of-human-moves)
|
|
867 (message "You have not played yet... Your move ?"))
|
|
868 (t
|
|
869 (message "One moment, please...")
|
|
870 ;; It is possible for the user to let Emacs play several consecutive
|
|
871 ;; moves, so that the best way to know when to stop taking back moves is
|
|
872 ;; to count the number of human moves:
|
|
873 (setq gomoku-human-took-back t)
|
|
874 (let ((number gomoku-number-of-human-moves))
|
|
875 (while (= number gomoku-number-of-human-moves)
|
|
876 (gomoku-take-back)))
|
|
877 (gomoku-prompt-for-move))))
|
|
878
|
|
879 (defun gomoku-human-resigns ()
|
|
880 "Signal to the Gomoku program that you may want to resign."
|
|
881 (interactive)
|
|
882 (gomoku-switch-to-window)
|
|
883 (cond
|
|
884 (gomoku-emacs-is-computing
|
|
885 (gomoku-crash-game))
|
|
886 ((not gomoku-game-in-progress)
|
|
887 (message "There is no game in progress"))
|
|
888 ((y-or-n-p "You mean, you resign ")
|
|
889 (gomoku-terminate-game 'human-resigned))
|
|
890 ((y-or-n-p "You mean, we continue ")
|
|
891 (gomoku-prompt-for-move))
|
|
892 (t
|
|
893 (gomoku-terminate-game 'human-resigned)))) ; OK. Accept it
|
|
894
|
|
895 ;;;
|
|
896 ;;; PROMPTING THE HUMAN PLAYER.
|
|
897 ;;;
|
|
898
|
|
899 (defun gomoku-prompt-for-move ()
|
|
900 "Display a message asking for Human's move."
|
|
901 (message (if (zerop gomoku-number-of-human-moves)
|
|
902 "Your move ? (move to a free square and hit X, RET ...)"
|
|
903 "Your move ?"))
|
|
904 ;; This may seem silly, but if one omits the following line (or a similar
|
|
905 ;; one), the cursor may very well go to some place where POINT is not.
|
|
906 (save-excursion (set-buffer (other-buffer))))
|
|
907
|
|
908 (defun gomoku-prompt-for-other-game ()
|
|
909 "Ask for another game, and start it."
|
|
910 (if (y-or-n-p "Another game ")
|
|
911 (gomoku gomoku-board-width gomoku-board-height)
|
4
|
912 (message "Chicken !")))
|
0
|
913
|
|
914 (defun gomoku-offer-a-draw ()
|
|
915 "Offer a draw and return T if Human accepted it."
|
|
916 (or (y-or-n-p "I offer you a draw. Do you accept it ")
|
4
|
917 (not (setq gomoku-human-refused-draw t))))
|
0
|
918
|
|
919 ;;;
|
|
920 ;;; DISPLAYING THE BOARD.
|
|
921 ;;;
|
|
922
|
|
923 ;; You may change these values if you have a small screen or if the squares
|
|
924 ;; look rectangular, but spacings SHOULD be at least 2 (MUST BE at least 1).
|
|
925
|
|
926 (defconst gomoku-square-width 4
|
|
927 "*Horizontal spacing between squares on the Gomoku board.")
|
|
928
|
|
929 (defconst gomoku-square-height 2
|
|
930 "*Vertical spacing between squares on the Gomoku board.")
|
|
931
|
|
932 (defconst gomoku-x-offset 3
|
|
933 "*Number of columns between the Gomoku board and the side of the window.")
|
|
934
|
|
935 (defconst gomoku-y-offset 1
|
|
936 "*Number of lines between the Gomoku board and the top of the window.")
|
|
937
|
|
938
|
|
939 (defun gomoku-max-width ()
|
|
940 "Largest possible board width for the current window."
|
|
941 (1+ (/ (- (window-width (selected-window))
|
|
942 gomoku-x-offset gomoku-x-offset 1)
|
|
943 gomoku-square-width)))
|
|
944
|
|
945 (defun gomoku-max-height ()
|
|
946 "Largest possible board height for the current window."
|
|
947 (1+ (/ (- (window-height (selected-window))
|
|
948 gomoku-y-offset gomoku-y-offset 2)
|
|
949 ;; 2 instead of 1 because WINDOW-HEIGHT includes the mode line !
|
|
950 gomoku-square-height)))
|
|
951
|
|
952 (defun gomoku-point-y ()
|
4
|
953 "Return the board row where point is."
|
|
954 (let ((inhibit-point-motion-hooks t))
|
|
955 (1+ (/ (- (count-lines 1 (point)) gomoku-y-offset (if (bolp) 0 1))
|
|
956 gomoku-square-height))))
|
0
|
957
|
|
958 (defun gomoku-point-square ()
|
4
|
959 "Return the index of the square point is on."
|
|
960 (let ((inhibit-point-motion-hooks t))
|
|
961 (gomoku-xy-to-index (1+ (/ (- (current-column) gomoku-x-offset)
|
|
962 gomoku-square-width))
|
|
963 (gomoku-point-y))))
|
0
|
964
|
|
965 (defun gomoku-goto-square (index)
|
|
966 "Move point to square number INDEX."
|
|
967 (gomoku-goto-xy (gomoku-index-to-x index) (gomoku-index-to-y index)))
|
|
968
|
|
969 (defun gomoku-goto-xy (x y)
|
|
970 "Move point to square at X, Y coords."
|
4
|
971 (let ((inhibit-point-motion-hooks t))
|
|
972 (goto-line (+ 1 gomoku-y-offset (* gomoku-square-height (1- y)))))
|
0
|
973 (move-to-column (+ gomoku-x-offset (* gomoku-square-width (1- x)))))
|
|
974
|
|
975 (defun gomoku-plot-square (square value)
|
4
|
976 "Draw 'X', 'O' or '.' on SQUARE depending on VALUE, leave point there."
|
|
977 (or (= value 1)
|
|
978 (gomoku-goto-square square))
|
|
979 (let ((inhibit-read-only t)
|
|
980 (inhibit-point-motion-hooks t))
|
|
981 (insert-and-inherit (cond ((= value 1) ?X)
|
|
982 ((= value 6) ?O)
|
|
983 (?.)))
|
|
984 (and window-system
|
|
985 (zerop value)
|
|
986 (put-text-property (1- (point)) (point) 'mouse-face 'highlight))
|
0
|
987 (delete-char 1)
|
4
|
988 (backward-char 1))
|
|
989 (sit-for 0)) ; Display NOW
|
0
|
990
|
|
991 (defun gomoku-init-display (n m)
|
|
992 "Display an N by M Gomoku board."
|
|
993 (buffer-disable-undo (current-buffer))
|
4
|
994 (let ((inhibit-read-only t)
|
|
995 (point 1) opoint
|
|
996 (intangible t)
|
|
997 (i m) j x)
|
|
998 ;; Try to minimize number of chars (because of text properties)
|
|
999 (setq tab-width
|
|
1000 (if (zerop (% gomoku-x-offset gomoku-square-width))
|
|
1001 gomoku-square-width
|
|
1002 (max (/ (+ (% gomoku-x-offset gomoku-square-width)
|
|
1003 gomoku-square-width 1) 2) 2)))
|
0
|
1004 (erase-buffer)
|
4
|
1005 (newline gomoku-y-offset)
|
|
1006 (while (progn
|
|
1007 (setq j n
|
|
1008 x (- gomoku-x-offset gomoku-square-width))
|
|
1009 (while (>= (setq j (1- j)) 0)
|
|
1010 (insert-char ?\t (/ (- (setq x (+ x gomoku-square-width))
|
|
1011 (current-column))
|
|
1012 tab-width))
|
|
1013 (insert-char ? (- x (current-column)))
|
|
1014 (if (setq intangible (not intangible))
|
|
1015 (put-text-property point (point) 'intangible 2))
|
|
1016 (and (zerop j)
|
|
1017 (= i (- m 2))
|
|
1018 (progn
|
|
1019 (while (>= i 3)
|
|
1020 (append-to-buffer (current-buffer) opoint (point))
|
|
1021 (setq i (- i 2)))
|
|
1022 (goto-char (point-max))))
|
|
1023 (setq point (point))
|
|
1024 (insert ?.)
|
|
1025 (if window-system
|
|
1026 (put-text-property point (point)
|
|
1027 'mouse-face 'highlight)))
|
|
1028 (> (setq i (1- i)) 0))
|
|
1029 (if (= i (1- m))
|
|
1030 (setq opoint point))
|
|
1031 (insert-char ?\n gomoku-square-height))
|
|
1032 (or (eq (char-after 1) ?.)
|
|
1033 (put-text-property 1 2 'point-entered
|
|
1034 (lambda (x x) (if (bobp) (forward-char)))))
|
|
1035 (or intangible
|
|
1036 (put-text-property point (point) 'intangible 2))
|
|
1037 (put-text-property point (point) 'point-entered
|
|
1038 (lambda (x x) (if (eobp) (backward-char))))
|
|
1039 (put-text-property (point-min) (point) 'category 'gomoku-mode))
|
|
1040 (gomoku-goto-xy (/ (1+ n) 2) (/ (1+ m) 2)) ; center of the board
|
|
1041 (sit-for 0)) ; Display NOW
|
0
|
1042
|
|
1043 (defun gomoku-display-statistics ()
|
|
1044 "Obnoxiously display some statistics about previous games in mode line."
|
|
1045 ;; We store this string in the mode-line-process local variable.
|
|
1046 ;; This is certainly not the cleanest way out ...
|
|
1047 (setq mode-line-process
|
4
|
1048 (format ": Won %d, lost %d%s"
|
|
1049 gomoku-number-of-human-wins
|
|
1050 gomoku-number-of-emacs-wins
|
|
1051 (if (zerop gomoku-number-of-draws)
|
|
1052 ""
|
|
1053 (format ", drew %d" gomoku-number-of-draws))))
|
0
|
1054 (force-mode-line-update))
|
|
1055
|
|
1056 (defun gomoku-switch-to-window ()
|
|
1057 "Find or create the Gomoku buffer, and display it."
|
|
1058 (interactive)
|
|
1059 (let ((buff (get-buffer "*Gomoku*")))
|
|
1060 (if buff ; Buffer exists:
|
4
|
1061 (switch-to-buffer buff) ; no problem.
|
|
1062 (if gomoku-game-in-progress
|
|
1063 (gomoku-crash-game)) ; buffer has been killed or something
|
|
1064 (switch-to-buffer "*Gomoku*") ; Anyway, start anew.
|
|
1065 (gomoku-mode))))
|
0
|
1066
|
|
1067 ;;;
|
|
1068 ;;; CROSSING WINNING QTUPLES.
|
|
1069 ;;;
|
|
1070
|
|
1071 ;; When someone succeeds in filling a qtuple, we draw a line over the five
|
|
1072 ;; corresponding squares. One problem is that the program does not know which
|
|
1073 ;; squares ! It only knows the square where the last move has been played and
|
|
1074 ;; who won. The solution is to scan the board along all four directions.
|
|
1075
|
|
1076 (defun gomoku-find-filled-qtuple (square value)
|
|
1077 "Return T if SQUARE belongs to a qtuple filled with VALUEs."
|
|
1078 (or (gomoku-check-filled-qtuple square value 1 0)
|
|
1079 (gomoku-check-filled-qtuple square value 0 1)
|
|
1080 (gomoku-check-filled-qtuple square value 1 1)
|
|
1081 (gomoku-check-filled-qtuple square value -1 1)))
|
|
1082
|
|
1083 (defun gomoku-check-filled-qtuple (square value dx dy)
|
|
1084 "Return T if SQUARE belongs to a qtuple filled with VALUEs along DX, DY."
|
|
1085 (let ((a 0) (b 0)
|
|
1086 (left square) (right square)
|
4
|
1087 (depl (gomoku-xy-to-index dx dy)))
|
0
|
1088 (while (and (> a -4) ; stretch tuple left
|
|
1089 (= value (aref gomoku-board (setq left (- left depl)))))
|
|
1090 (setq a (1- a)))
|
4
|
1091 (while (and (< b (+ a 4)) ; stretch tuple right
|
0
|
1092 (= value (aref gomoku-board (setq right (+ right depl)))))
|
|
1093 (setq b (1+ b)))
|
4
|
1094 (cond ((= b (+ a 4)) ; tuple length = 5 ?
|
|
1095 (gomoku-cross-qtuple (+ square (* a depl)) (+ square (* b depl))
|
|
1096 dx dy)
|
0
|
1097 t))))
|
|
1098
|
|
1099 (defun gomoku-cross-qtuple (square1 square2 dx dy)
|
|
1100 "Cross every square between SQUARE1 and SQUARE2 in the DX, DY direction."
|
|
1101 (save-excursion ; Not moving point from last square
|
4
|
1102 (let ((depl (gomoku-xy-to-index dx dy))
|
|
1103 (inhibit-read-only t)
|
|
1104 (inhibit-point-motion-hooks t))
|
0
|
1105 ;; WARNING: this function assumes DEPL > 0 and SQUARE2 > SQUARE1
|
4
|
1106 (while (/= square1 square2)
|
0
|
1107 (gomoku-goto-square square1)
|
|
1108 (setq square1 (+ square1 depl))
|
|
1109 (cond
|
4
|
1110 ((= dy 0) ; Horizontal
|
|
1111 (forward-char 1)
|
|
1112 (insert-char ?- (1- gomoku-square-width) t)
|
|
1113 (delete-region (point) (progn
|
|
1114 (skip-chars-forward " \t")
|
|
1115 (point))))
|
|
1116 ((= dx 0) ; Vertical
|
|
1117 (let ((n 1)
|
|
1118 (column (current-column)))
|
0
|
1119 (while (< n gomoku-square-height)
|
|
1120 (setq n (1+ n))
|
4
|
1121 (forward-line 1)
|
|
1122 (indent-to column)
|
|
1123 (insert-and-inherit ?|))))
|
|
1124 ((= dx -1) ; 1st Diagonal
|
|
1125 (indent-to (prog1 (- (current-column) (/ gomoku-square-width 2))
|
|
1126 (forward-line (/ gomoku-square-height 2))))
|
|
1127 (insert-and-inherit ?/))
|
|
1128 (t ; 2nd Diagonal
|
|
1129 (indent-to (prog1 (+ (current-column) (/ gomoku-square-width 2))
|
|
1130 (forward-line (/ gomoku-square-height 2))))
|
|
1131 (insert-and-inherit ?\\))))))
|
0
|
1132 (sit-for 0)) ; Display NOW
|
|
1133
|
|
1134 ;;;
|
|
1135 ;;; CURSOR MOTION.
|
|
1136 ;;;
|
4
|
1137 ;; previous-line and next-line don't work right with intangible newlines
|
0
|
1138 (defun gomoku-move-down ()
|
|
1139 "Move point down one row on the Gomoku board."
|
|
1140 (interactive)
|
4
|
1141 (if (< (gomoku-point-y) gomoku-board-height)
|
|
1142 (next-line gomoku-square-height)))
|
0
|
1143
|
|
1144 (defun gomoku-move-up ()
|
|
1145 "Move point up one row on the Gomoku board."
|
|
1146 (interactive)
|
4
|
1147 (if (> (gomoku-point-y) 1)
|
|
1148 (previous-line gomoku-square-height)))
|
0
|
1149
|
|
1150 (defun gomoku-move-ne ()
|
|
1151 "Move point North East on the Gomoku board."
|
|
1152 (interactive)
|
|
1153 (gomoku-move-up)
|
4
|
1154 (forward-char))
|
0
|
1155
|
|
1156 (defun gomoku-move-se ()
|
|
1157 "Move point South East on the Gomoku board."
|
|
1158 (interactive)
|
|
1159 (gomoku-move-down)
|
4
|
1160 (forward-char))
|
0
|
1161
|
|
1162 (defun gomoku-move-nw ()
|
|
1163 "Move point North West on the Gomoku board."
|
|
1164 (interactive)
|
|
1165 (gomoku-move-up)
|
4
|
1166 (backward-char))
|
0
|
1167
|
|
1168 (defun gomoku-move-sw ()
|
|
1169 "Move point South West on the Gomoku board."
|
|
1170 (interactive)
|
|
1171 (gomoku-move-down)
|
4
|
1172 (backward-char))
|
|
1173
|
|
1174 (defun gomoku-beginning-of-line ()
|
|
1175 "Move point to first square on the Gomoku board row."
|
|
1176 (interactive)
|
|
1177 (move-to-column gomoku-x-offset))
|
|
1178
|
|
1179 (defun gomoku-end-of-line ()
|
|
1180 "Move point to last square on the Gomoku board row."
|
|
1181 (interactive)
|
|
1182 (move-to-column (+ gomoku-x-offset
|
|
1183 (* gomoku-square-width (1- gomoku-board-width)))))
|
0
|
1184
|
|
1185 (provide 'gomoku)
|
|
1186
|
|
1187 ;;; gomoku.el ends here
|