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