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