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