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