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