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