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