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