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