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