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