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