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