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