annotate lisp/play/gomoku.el @ 111262:029e4783cbae

Merge changes from emacs-23 branch
author Chong Yidong <cyd@stupidchicken.com>
date Sun, 31 Oct 2010 10:40:01 -0400
parents 1a6d2311d8d5 516f3d80dac6
children be0bea620615
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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,
516f3d80dac6 Replace some inappropriate uses of toggle-read-only. (Bug#7292)
Glenn Morris <rgm@gnu.org>
parents: 106815
diff changeset
4 ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
846
20674ae6bf52 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 807
diff changeset
5
19963
2f60572de4d1 Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 14948
diff changeset
6 ;; Author: Philippe Schnoebelen <phs@lsv.ens-cachan.fr>
40788
98e8ee29a39c (gomoku): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 39886
diff changeset
7 ;; Maintainer: FSF
23869
8d2bb5d1416a Update comment.
Karl Heuer <kwzh@gnu.org>
parents: 21363
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
11 ;; This file is part of GNU Emacs.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
17
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
18 ;; GNU Emacs is distributed in the hope that it will be useful,
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
21 ;; GNU General Public License for more details.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
22
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
28 ;; RULES:
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
29 ;;
74145
8eceefbbcde7 Space/tab mixup.
Juanma Barranquero <lekktu@gmail.com>
parents: 68634
diff changeset
30 ;; Gomoku is a game played between two players on a rectangular board. Each
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
31 ;; player, in turn, marks a free square of its choice. The winner is the first
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
32 ;; one to mark five contiguous squares in any direction (horizontally,
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
33 ;; vertically or diagonally).
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
34 ;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
35 ;; I have been told that, in "The TRUE Gomoku", some restrictions are made
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
36 ;; about the squares where one may play, or else there is a known forced win
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
37 ;; for the first player. This program has no such restriction, but it does not
40788
98e8ee29a39c (gomoku): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 39886
diff changeset
38 ;; know about the forced win, nor do I.
98e8ee29a39c (gomoku): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 39886
diff changeset
39 ;; See http://renju.nu/r1rulhis.htm for more information.
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
40
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
41
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
42 ;; There are two main places where you may want to customize the program: key
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
43 ;; bindings and board display. These features are commented in the code. Go
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
44 ;; and see.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
45
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
46
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
47 ;; HOW TO USE:
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
50 ;; board, the size of which depends on the size of the current window. The
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
51 ;; size of the board is easily modified by giving numeric arguments to the
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
52 ;; gomoku command and/or by customizing the displaying parameters.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
53 ;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
54 ;; Emacs plays when it is its turn. When it is your turn, just put the cursor
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
55 ;; on the square where you want to play and hit RET, or X, or whatever key you
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
56 ;; bind to the command gomoku-human-plays. When it is your turn, Emacs is
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
57 ;; idle: you may switch buffers, read your mail, ... Just come back to the
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
58 ;; *Gomoku* buffer and resume play.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
59
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
60
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
61 ;; ALGORITHM:
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
62 ;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
63 ;; The algorithm is briefly described in section "THE SCORE TABLE". Some
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
64 ;; parameters may be modified if you want to change the style exhibited by the
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
68
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
69 (defgroup gomoku nil
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
70 "Gomoku game between you and Emacs."
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
71 :prefix "gomoku-"
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
72 :group 'games)
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
73 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
74 ;;; GOMOKU MODE AND KEYMAP.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
75 ;;;
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
76 (defcustom gomoku-mode-hook nil
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
77 "If non-nil, its value is called on entry to Gomoku mode.
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
78 One useful value to include is `turn-on-font-lock' to highlight the pieces."
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
79 :type 'hook
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
80 :group 'gomoku)
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
157 "Local keymap to use in Gomoku mode.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
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
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
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
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
187 "Major mode for playing Gomoku against Emacs.
207
cc1060d41d94 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 52
diff changeset
188 You and Emacs play in turn by marking a free square. You mark it with X
cc1060d41d94 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 52
diff changeset
189 and Emacs marks it with O. The winner is the first to get five contiguous
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
cc1060d41d94 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 52
diff changeset
192 You play by moving the cursor over the square you choose and hitting \\[gomoku-human-plays].
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
029e4783cbae Merge changes from emacs-23 branch
Chong Yidong <cyd@stupidchicken.com>
parents: 107859 111257
diff changeset
199 buffer-read-only t))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
200
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
201 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
202 ;;; THE BOARD.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
203 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
204
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
205 ;; The board is a rectangular grid. We code empty squares with 0, X's with 1
74145
8eceefbbcde7 Space/tab mixup.
Juanma Barranquero <lekktu@gmail.com>
parents: 68634
diff changeset
206 ;; and O's with 6. The rectangle is recorded in a one dimensional vector
8eceefbbcde7 Space/tab mixup.
Juanma Barranquero <lekktu@gmail.com>
parents: 68634
diff changeset
207 ;; containing padding squares (coded with -1). These squares allow us to
8eceefbbcde7 Space/tab mixup.
Juanma Barranquero <lekktu@gmail.com>
parents: 68634
diff changeset
208 ;; detect when we are trying to move out of the board. We denote a square by
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
209 ;; its (X,Y) coords, or by the INDEX corresponding to them in the vector. The
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
210 ;; leftmost topmost square has coords (1,1) and index gomoku-board-width + 2.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
211 ;; Similarly, vectors between squares may be given by two DX, DY coords or by
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
212 ;; one DEPL (the difference between indexes).
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
213
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
214 (defvar gomoku-board-width nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
215 "Number of columns on the Gomoku board.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
216
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
217 (defvar gomoku-board-height nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
218 "Number of lines on the Gomoku board.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
219
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
220 (defvar gomoku-board nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
221 "Vector recording the actual state of the Gomoku board.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
222
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
225
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
226 (defvar gomoku-draw-limit nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
227 ;; This is usually set to 70% of the number of squares.
207
cc1060d41d94 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 52
diff changeset
228 "After how many moves will Emacs offer a draw?")
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
229
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
230
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
231 (defun gomoku-xy-to-index (x y)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
232 "Translate X, Y cartesian coords into the corresponding board index."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
233 (+ (* y gomoku-board-width) x y))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
234
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
235 (defun gomoku-index-to-x (index)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
236 "Return corresponding x-coord of board INDEX."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
237 (% index (1+ gomoku-board-width)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
238
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
239 (defun gomoku-index-to-y (index)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
240 "Return corresponding y-coord of board INDEX."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
241 (/ index (1+ gomoku-board-width)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
242
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
245 (setq gomoku-board (make-vector gomoku-vector-length 0))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
246 ;; Every square is 0 (i.e. empty) except padding squares:
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
247 (let ((i 0) (ii (1- gomoku-vector-length)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
248 (while (<= i gomoku-board-width) ; The squares in [0..width] and in
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
249 (aset gomoku-board i -1) ; [length - width - 1..length - 1]
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
250 (aset gomoku-board ii -1) ; are padding squares.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
251 (setq i (1+ i)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
252 ii (1- ii))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
253 (let ((i 0))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
254 (while (< i gomoku-vector-length)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
255 (aset gomoku-board i -1) ; and also all k*(width+1)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
256 (setq i (+ i gomoku-board-width 1)))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
257
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
258 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
259 ;;; THE SCORE TABLE.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
260 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
261
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
262 ;; Every (free) square has a score associated to it, recorded in the
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
263 ;; GOMOKU-SCORE-TABLE vector. The program always plays in the square having
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
264 ;; the highest score.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
265
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
266 (defvar gomoku-score-table nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
267 "Vector recording the actual score of the free squares.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
268
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
269
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
270 ;; The key point point about the algorithm is that, rather than considering
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
271 ;; the board as just a set of squares, we prefer to see it as a "space" of
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
272 ;; internested 5-tuples of contiguous squares (called qtuples).
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
273 ;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
274 ;; The aim of the program is to fill one qtuple with its O's while preventing
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
275 ;; you from filling another one with your X's. To that effect, it computes a
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
276 ;; score for every qtuple, with better qtuples having better scores. Of
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
277 ;; course, the score of a qtuple (taken in isolation) is just determined by
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
278 ;; its contents as a set, i.e. not considering the order of its elements. The
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
279 ;; highest score is given to the "OOOO" qtuples because playing in such a
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
280 ;; qtuple is winning the game. Just after this comes the "XXXX" qtuple because
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
281 ;; not playing in it is just loosing the game, and so on. Note that a
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
282 ;; "polluted" qtuple, i.e. one containing at least one X and at least one O,
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
283 ;; has score zero because there is no more any point in playing in it, from
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
284 ;; both an attacking and a defending point of view.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
285 ;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
286 ;; Given the score of every qtuple, the score of a given free square on the
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
287 ;; board is just the sum of the scores of all the qtuples to which it belongs,
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
288 ;; because playing in that square is playing in all its containing qtuples at
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
289 ;; once. And it is that function which takes into account the internesting of
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
290 ;; the qtuples.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
291 ;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
292 ;; This algorithm is rather simple but anyway it gives a not so dumb level of
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
293 ;; play. It easily extends to "n-dimensional Gomoku", where a win should not
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
294 ;; be obtained with as few as 5 contiguous marks: 6 or 7 (depending on n !)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
295 ;; should be preferred.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
296
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
297
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
298 ;; Here are the scores of the nine "non-polluted" configurations. Tuning
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
299 ;; these values will change (hopefully improve) the strength of the program
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
300 ;; and may change its style (rather aggressive here).
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
301
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
302 (defconst nil-score 7 "Score of an empty qtuple.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
303 (defconst Xscore 15 "Score of a qtuple containing one X.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
304 (defconst XXscore 400 "Score of a qtuple containing two X's.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
305 (defconst XXXscore 1800 "Score of a qtuple containing three X's.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
306 (defconst XXXXscore 100000 "Score of a qtuple containing four X's.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
307 (defconst Oscore 35 "Score of a qtuple containing one O.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
308 (defconst OOscore 800 "Score of a qtuple containing two O's.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
309 (defconst OOOscore 15000 "Score of a qtuple containing three O's.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
310 (defconst OOOOscore 800000 "Score of a qtuple containing four O's.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
311
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
312 ;; These values are not just random: if, given the following situation:
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
313 ;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
314 ;; . . . . . . . O .
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
315 ;; . X X a . . . X .
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
316 ;; . . . X . . . X .
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
317 ;; . . . X . . . X .
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
318 ;; . . . . . . . b .
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
319 ;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
320 ;; you want Emacs to play in "a" and not in "b", then the parameters must
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
321 ;; satisfy the inequality:
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
322 ;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
323 ;; 6 * XXscore > XXXscore + XXscore
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
324 ;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
325 ;; because "a" mainly belongs to six "XX" qtuples (the others are less
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
326 ;; important) while "b" belongs to one "XXX" and one "XX" qtuples. Other
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
327 ;; conditions are required to obtain sensible moves, but the previous example
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
328 ;; should illustrate the point. If you manage to improve on these values,
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
329 ;; please send me a note. Thanks.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
330
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
331
14040
187735b53d52 Comment fixes.
Karl Heuer <kwzh@gnu.org>
parents: 11685
diff changeset
332 ;; As we chose values 0, 1 and 6 to denote empty, X and O squares, the
187735b53d52 Comment fixes.
Karl Heuer <kwzh@gnu.org>
parents: 11685
diff changeset
333 ;; contents of a qtuple are uniquely determined by the sum of its elements and
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
334 ;; we just have to set up a translation table.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
335
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
336 (defconst gomoku-score-trans-table
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
337 (vector nil-score Xscore XXscore XXXscore XXXXscore 0
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
338 Oscore 0 0 0 0 0
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
339 OOscore 0 0 0 0 0
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
340 OOOscore 0 0 0 0 0
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
341 OOOOscore 0 0 0 0 0
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
342 0)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
343 "Vector associating qtuple contents to their score.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
344
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
345
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
346 ;; If you do not modify drastically the previous constants, the only way for a
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
347 ;; square to have a score higher than OOOOscore is to belong to a "OOOO"
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
348 ;; qtuple, thus to be a winning move. Similarly, the only way for a square to
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
349 ;; have a score between XXXXscore and OOOOscore is to belong to a "XXXX"
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
350 ;; qtuple. We may use these considerations to detect when a given move is
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
351 ;; winning or loosing.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
352
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
353 (defconst gomoku-winning-threshold OOOOscore
207
cc1060d41d94 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 52
diff changeset
354 "Threshold score beyond which an Emacs move is winning.")
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
355
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
356 (defconst gomoku-loosing-threshold XXXXscore
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
357 "Threshold score beyond which a human move is winning.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
358
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
359
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
360 (defun gomoku-strongest-square ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
361 "Compute index of free square with highest score, or nil if none."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
362 ;; We just have to loop other all squares. However there are two problems:
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
363 ;; 1/ The SCORE-TABLE only gives correct scores to free squares. To speed
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
364 ;; up future searches, we set the score of padding or occupied squares
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
365 ;; to -1 whenever we meet them.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
366 ;; 2/ We want to choose randomly between equally good moves.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
367 (let ((score-max 0)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
368 (count 0) ; Number of equally good moves
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
369 (square (gomoku-xy-to-index 1 1)) ; First square
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
370 (end (gomoku-xy-to-index gomoku-board-width gomoku-board-height))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
371 best-square score)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
372 (while (<= square end)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
373 (cond
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
374 ;; If score is lower (i.e. most of the time), skip to next:
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
375 ((< (aref gomoku-score-table square) score-max))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
376 ;; If score is better, beware of non free squares:
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
377 ((> (setq score (aref gomoku-score-table square)) score-max)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
378 (if (zerop (aref gomoku-board square)) ; is it free ?
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
379 (setq count 1 ; yes: take it !
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
380 best-square square
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
381 score-max score)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
382 (aset gomoku-score-table square -1))) ; no: kill it !
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
383 ;; If score is equally good, choose randomly. But first check freeness:
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
384 ((not (zerop (aref gomoku-board square)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
385 (aset gomoku-score-table square -1))
4402
3cd121df96c8 (random-number): Remove.
Paul Eggert <eggert@twinsun.com>
parents: 1221
diff changeset
386 ((zerop (random (setq count (1+ count))))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
387 (setq best-square square
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
388 score-max score)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
389 (setq square (1+ square))) ; try next square
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
390 best-square))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
391
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
392 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
393 ;;; INITIALIZING THE SCORE TABLE.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
394 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
395
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
396 ;; At initialization the board is empty so that every qtuple amounts for
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
397 ;; nil-score. Therefore, the score of any square is nil-score times the number
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
398 ;; of qtuples that pass through it. This number is 3 in a corner and 20 if you
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
399 ;; are sufficiently far from the sides. As computing the number is time
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
400 ;; consuming, we initialize every square with 20*nil-score and then only
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
401 ;; consider squares at less than 5 squares from one side. We speed this up by
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
402 ;; taking symmetry into account.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
403 ;; Also, as it is likely that successive games will be played on a board with
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
404 ;; same size, it is a good idea to save the initial SCORE-TABLE configuration.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
405
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
406 (defvar gomoku-saved-score-table nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
407 "Recorded initial value of previous score table.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
408
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
409 (defvar gomoku-saved-board-width nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
410 "Recorded value of previous board width.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
411
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
412 (defvar gomoku-saved-board-height nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
413 "Recorded value of previous board height.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
414
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
415
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
416 (defun gomoku-init-score-table ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
417 "Create the score table vector and fill it with initial values."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
418 (if (and gomoku-saved-score-table ; Has it been stored last time ?
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
419 (= gomoku-board-width gomoku-saved-board-width)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
420 (= gomoku-board-height gomoku-saved-board-height))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
421 (setq gomoku-score-table (copy-sequence gomoku-saved-score-table))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
422 ;; No, compute it:
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
423 (setq gomoku-score-table
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
424 (make-vector gomoku-vector-length (* 20 nil-score)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
425 (let (i j maxi maxj maxi2 maxj2)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
426 (setq maxi (/ (1+ gomoku-board-width) 2)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
427 maxj (/ (1+ gomoku-board-height) 2)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
428 maxi2 (min 4 maxi)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
429 maxj2 (min 4 maxj))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
430 ;; We took symmetry into account and could use it more if the board
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
431 ;; would have been square and not rectangular !
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
432 ;; In our case we deal with all (i,j) in the set [1..maxi2]*[1..maxj] U
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
433 ;; [maxi2+1..maxi]*[1..maxj2]. Maxi2 and maxj2 are used because the
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
434 ;; board may well be less than 8 by 8 !
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
435 (setq i 1)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
436 (while (<= i maxi2)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
437 (setq j 1)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
438 (while (<= j maxj)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
439 (gomoku-init-square-score i j)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
440 (setq j (1+ j)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
441 (setq i (1+ i)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
442 (while (<= i maxi)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
443 (setq j 1)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
444 (while (<= j maxj2)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
445 (gomoku-init-square-score i j)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
446 (setq j (1+ j)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
447 (setq i (1+ i))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
448 (setq gomoku-saved-score-table (copy-sequence gomoku-score-table)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
449 gomoku-saved-board-width gomoku-board-width
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
450 gomoku-saved-board-height gomoku-board-height)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
451
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
452 (defun gomoku-nb-qtuples (i j)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
453 "Return the number of qtuples containing square I,J."
207
cc1060d41d94 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 52
diff changeset
454 ;; This function is complicated because we have to deal
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
455 ;; with ugly cases like 3 by 6 boards, but it works.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
456 ;; If you have a simpler (and correct) solution, send it to me. Thanks !
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
457 (let ((left (min 4 (1- i)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
458 (right (min 4 (- gomoku-board-width i)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
459 (up (min 4 (1- j)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
460 (down (min 4 (- gomoku-board-height j))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
461 (+ -12
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
462 (min (max (+ left right) 3) 8)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
463 (min (max (+ up down) 3) 8)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
464 (min (max (+ (min left up) (min right down)) 3) 8)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
465 (min (max (+ (min right up) (min left down)) 3) 8))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
466
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
467 (defun gomoku-init-square-score (i j)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
468 "Give initial score to square I,J and to its mirror images."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
469 (let ((ii (1+ (- gomoku-board-width i)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
470 (jj (1+ (- gomoku-board-height j)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
471 (sc (* (gomoku-nb-qtuples i j) (aref gomoku-score-trans-table 0))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
472 (aset gomoku-score-table (gomoku-xy-to-index i j) sc)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
473 (aset gomoku-score-table (gomoku-xy-to-index ii j) sc)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
474 (aset gomoku-score-table (gomoku-xy-to-index i jj) sc)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
475 (aset gomoku-score-table (gomoku-xy-to-index ii jj) sc)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
476
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
477 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
478 ;;; MAINTAINING THE SCORE TABLE.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
479 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
480
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
481 ;; We do not provide functions for computing the SCORE-TABLE given the
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
482 ;; contents of the BOARD. This would involve heavy nested loops, with time
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
483 ;; proportional to the size of the board. It is better to update the
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
484 ;; SCORE-TABLE after each move. Updating needs not modify more than 36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
485 ;; squares: it is done in constant time.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
486
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
487 (defun gomoku-update-score-table (square dval)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
488 "Update score table after SQUARE received a DVAL increment."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
489 ;; The board has already been updated when this function is called.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
490 ;; Updating scores is done by looking for qtuples boundaries in all four
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
491 ;; directions and then calling update-score-in-direction.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
492 ;; Finally all squares received the right increment, and then are up to
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
493 ;; date, except possibly for SQUARE itself if we are taking a move back for
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
494 ;; its score had been set to -1 at the time.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
495 (let* ((x (gomoku-index-to-x square))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
496 (y (gomoku-index-to-y square))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
497 (imin (max -4 (- 1 x)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
498 (jmin (max -4 (- 1 y)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
499 (imax (min 0 (- gomoku-board-width x 4)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
500 (jmax (min 0 (- gomoku-board-height y 4))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
501 (gomoku-update-score-in-direction imin imax
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
502 square 1 0 dval)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
503 (gomoku-update-score-in-direction jmin jmax
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
504 square 0 1 dval)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
505 (gomoku-update-score-in-direction (max imin jmin) (min imax jmax)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
506 square 1 1 dval)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
507 (gomoku-update-score-in-direction (max (- 1 y) -4
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
508 (- x gomoku-board-width))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
509 (min 0 (- x 5)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
510 (- gomoku-board-height y 4))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
511 square -1 1 dval)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
512
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
513 (defun gomoku-update-score-in-direction (left right square dx dy dval)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
514 "Update scores for all squares in the qtuples starting between the LEFTth
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
515 square and the RIGHTth after SQUARE, along the DX, DY direction, considering
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
516 that DVAL has been added on SQUARE."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
517 ;; We always have LEFT <= 0, RIGHT <= 0 and DEPL > 0 but we may very well
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
518 ;; have LEFT > RIGHT, indicating that no qtuple contains SQUARE along that
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
519 ;; DX,DY direction.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
520 (cond
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
521 ((> left right)) ; Quit
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
522 (t ; Else ..
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
523 (let (depl square0 square1 square2 count delta)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
524 (setq depl (gomoku-xy-to-index dx dy)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
525 square0 (+ square (* left depl))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
526 square1 (+ square (* right depl))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
527 square2 (+ square0 (* 4 depl)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
528 ;; Compute the contents of the first qtuple:
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
529 (setq square square0
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
530 count 0)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
531 (while (<= square square2)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
532 (setq count (+ count (aref gomoku-board square))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
533 square (+ square depl)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
534 (while (<= square0 square1)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
535 ;; Update the squares of the qtuple beginning in SQUARE0 and ending
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
536 ;; in SQUARE2.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
537 (setq delta (- (aref gomoku-score-trans-table count)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
538 (aref gomoku-score-trans-table (- count dval))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
539 (cond ((not (zerop delta)) ; or else nothing to update
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
540 (setq square square0)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
541 (while (<= square square2)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
542 (if (zerop (aref gomoku-board square)) ; only for free squares
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
543 (aset gomoku-score-table square
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
544 (+ (aref gomoku-score-table square) delta)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
545 (setq square (+ square depl)))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
546 ;; Then shift the qtuple one square along DEPL, this only requires
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
547 ;; modifying SQUARE0 and SQUARE2.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
548 (setq square2 (+ square2 depl)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
549 count (+ count (- (aref gomoku-board square0))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
550 (aref gomoku-board square2))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
551 square0 (+ square0 depl)))))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
552
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
553 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
554 ;;; GAME CONTROL.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
555 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
556
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
557 ;; Several variables are used to monitor a game, including a GAME-HISTORY (the
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
558 ;; list of all (SQUARE . PREVSCORE) played) that allows to take moves back
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
559 ;; (anti-updating the score table) and to compute the table from scratch in
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
560 ;; case of an interruption.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
561
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
562 (defvar gomoku-game-in-progress nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
563 "Non-nil if a game is in progress.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
564
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
565 (defvar gomoku-game-history nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
566 "A record of all moves that have been played during current game.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
567
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
568 (defvar gomoku-number-of-moves nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
569 "Number of moves already played in current game.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
570
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
571 (defvar gomoku-number-of-human-moves nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
572 "Number of moves already played by human in current game.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
573
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
574 (defvar gomoku-emacs-played-first nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
575 "Non-nil if Emacs played first.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
576
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
577 (defvar gomoku-human-took-back nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
578 "Non-nil if Human took back a move during the game.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
579
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
580 (defvar gomoku-human-refused-draw nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
581 "Non-nil if Human refused Emacs offer of a draw.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
582
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
583 (defvar gomoku-emacs-is-computing nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
584 ;; This is used to detect interruptions. Hopefully, it should not be needed.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
585 "Non-nil if Emacs is in the middle of a computation.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
586
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
587
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
588 (defun gomoku-start-game (n m)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
589 "Initialize a new game on an N by M board."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
590 (setq gomoku-emacs-is-computing t) ; Raise flag
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
591 (setq gomoku-game-in-progress t)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
592 (setq gomoku-board-width n
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
593 gomoku-board-height m
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
594 gomoku-vector-length (1+ (* (+ m 2) (1+ n)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
598 gomoku-number-of-moves 0
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
599 gomoku-number-of-human-moves 0
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
600 gomoku-emacs-played-first nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
601 gomoku-human-took-back nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
602 gomoku-human-refused-draw nil)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
603 (gomoku-init-display n m) ; Display first: the rest takes time
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
604 (gomoku-init-score-table) ; INIT-BOARD requires that the score
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
605 (gomoku-init-board) ; table be already created.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
606 (setq gomoku-emacs-is-computing nil))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
607
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
608 (defun gomoku-play-move (square val &optional dont-update-score)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
609 "Go to SQUARE, play VAL and update everything."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
610 (setq gomoku-emacs-is-computing t) ; Raise flag
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
611 (cond ((= 1 val) ; a Human move
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
612 (setq gomoku-number-of-human-moves (1+ gomoku-number-of-human-moves)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
613 ((zerop gomoku-number-of-moves) ; an Emacs move. Is it first ?
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
614 (setq gomoku-emacs-played-first t)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
615 (setq gomoku-game-history
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
616 (cons (cons square (aref gomoku-score-table square))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
617 gomoku-game-history)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
618 gomoku-number-of-moves (1+ gomoku-number-of-moves))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
619 (gomoku-plot-square square val)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
620 (aset gomoku-board square val) ; *BEFORE* UPDATE-SCORE !
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
621 (if dont-update-score nil
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
622 (gomoku-update-score-table square val) ; previous val was 0: dval = val
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
623 (aset gomoku-score-table square -1))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
624 (setq gomoku-emacs-is-computing nil))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
625
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
626 (defun gomoku-take-back ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
627 "Take back last move and update everything."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
628 (setq gomoku-emacs-is-computing t)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
629 (let* ((last-move (car gomoku-game-history))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
630 (square (car last-move))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
631 (oldval (aref gomoku-board square)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
632 (if (= 1 oldval)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
633 (setq gomoku-number-of-human-moves (1- gomoku-number-of-human-moves)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
634 (setq gomoku-game-history (cdr gomoku-game-history)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
635 gomoku-number-of-moves (1- gomoku-number-of-moves))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
636 (gomoku-plot-square square 0)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
637 (aset gomoku-board square 0) ; *BEFORE* UPDATE-SCORE !
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
638 (gomoku-update-score-table square (- oldval))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
639 (aset gomoku-score-table square (cdr last-move)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
640 (setq gomoku-emacs-is-computing nil))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
641
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
642 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
643 ;;; SESSION CONTROL.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
644 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
651
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
652 (defvar gomoku-number-of-draws 0
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
653 "Number of games already drawn in this session.")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
654
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
655
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
656 (defun gomoku-terminate-game (result)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
710
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
711 (defun gomoku-crash-game ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
712 "What to do when Emacs detects it has been interrupted."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
713 (setq gomoku-emacs-is-computing nil)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
714 (gomoku-terminate-game 'crash-game)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
715 (sit-for 4) ; Let's see the message
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
716 (gomoku-prompt-for-other-game))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
717
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
718 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
719 ;;; INTERACTIVE COMMANDS.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
720 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
721
1221
d5f91623a2cb (gomoku): Make it autoload.
Richard M. Stallman <rms@gnu.org>
parents: 923
diff changeset
722 ;;;###autoload
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
723 (defun gomoku (&optional n m)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
724 "Start a Gomoku game between you and Emacs.
40788
98e8ee29a39c (gomoku): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 39886
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
729
207
cc1060d41d94 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 52
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
732 marks horizontally, vertically or in diagonal.
207
cc1060d41d94 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 52
diff changeset
733
cc1060d41d94 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 52
diff changeset
734 You play by moving the cursor over the square you choose and hitting
cc1060d41d94 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 52
diff changeset
735 \\<gomoku-mode-map>\\[gomoku-human-plays].
40788
98e8ee29a39c (gomoku): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 39886
diff changeset
736
98e8ee29a39c (gomoku): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 39886
diff changeset
737 This program actually plays a simplified or archaic version of the
98e8ee29a39c (gomoku): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 39886
diff changeset
738 Gomoku game, and ought to be upgraded to use the full modern rules.
98e8ee29a39c (gomoku): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 39886
diff changeset
739
207
cc1060d41d94 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 52
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
755 (cond
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
756 (gomoku-emacs-is-computing
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
760 (let ((max-width (gomoku-max-width))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
761 (max-height (gomoku-max-height)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
762 (or n (setq n max-width))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
763 (or m (setq m max-height))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
764 (cond ((< n 1)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
765 (error "I need at least 1 column"))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
766 ((< m 1)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
767 (error "I need at least 1 row"))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
768 ((> n max-width)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
769 (error "I cannot display %d columns in that window" n)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
774 (setq m max-height)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
775 (message "One moment, please...")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
778 (gomoku-emacs-plays)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
781 (gomoku-prompt-for-move))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
782 (t
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
783 (gomoku-human-resigns))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
784
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
785 (defun gomoku-emacs-plays ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
786 "Compute Emacs next move and play it."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
787 (interactive)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
788 (gomoku-switch-to-window)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
789 (cond
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
790 (gomoku-emacs-is-computing
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
791 (gomoku-crash-game))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
792 ((not gomoku-game-in-progress)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
793 (gomoku-prompt-for-other-game))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
794 (t
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
795 (message "Let me think...")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
796 (let (square score)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
797 (setq square (gomoku-strongest-square))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
798 (cond ((null square)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
799 (gomoku-terminate-game 'nobody-won))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
800 (t
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
801 (setq score (aref gomoku-score-table square))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
802 (gomoku-play-move square 6)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
805 (gomoku-find-filled-qtuple square 6)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
806 (gomoku-terminate-game 'emacs-won))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
807 ((zerop score)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
808 (gomoku-terminate-game 'nobody-won))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
809 ((and (> gomoku-number-of-moves gomoku-draw-limit)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
810 (not gomoku-human-refused-draw)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
811 (gomoku-offer-a-draw))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
812 (gomoku-terminate-game 'draw-agreed))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
813 (t
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
814 (gomoku-prompt-for-move)))))))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
854 (defun gomoku-human-plays ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
855 "Signal to the Gomoku program that you have played.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
856 You must have put the cursor on the square where you want to play.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
857 If the game is finished, this command requests for another game."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
858 (interactive)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
859 (gomoku-switch-to-window)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
860 (cond
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
861 (gomoku-emacs-is-computing
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
862 (gomoku-crash-game))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
863 ((not gomoku-game-in-progress)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
864 (gomoku-prompt-for-other-game))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
865 (t
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
866 (let (square score)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
867 (setq square (gomoku-point-square))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
872 (t
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
873 (setq score (aref gomoku-score-table square))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
874 (gomoku-play-move square 1)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
875 (cond ((and (>= score gomoku-loosing-threshold)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
876 ;; Just testing SCORE > THRESHOLD is not enough for
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
877 ;; detecting wins, it just gives an indication that
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
878 ;; we confirm with GOMOKU-FIND-FILLED-QTUPLE.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
879 (gomoku-find-filled-qtuple square 1))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
880 (gomoku-terminate-game 'human-won))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
881 (t
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
882 (gomoku-emacs-plays)))))))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
883
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
884 (defun gomoku-human-takes-back ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
885 "Signal to the Gomoku program that you wish to take back your last move."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
886 (interactive)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
887 (gomoku-switch-to-window)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
888 (cond
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
889 (gomoku-emacs-is-computing
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
890 (gomoku-crash-game))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
891 ((not gomoku-game-in-progress)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
892 (message "Too late for taking back...")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
893 (sit-for 4)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
894 (gomoku-prompt-for-other-game))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
897 (t
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
898 (message "One moment, please...")
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
899 ;; It is possible for the user to let Emacs play several consecutive
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
900 ;; moves, so that the best way to know when to stop taking back moves is
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
901 ;; to count the number of human moves:
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
902 (setq gomoku-human-took-back t)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
903 (let ((number gomoku-number-of-human-moves))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
904 (while (= number gomoku-number-of-human-moves)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
905 (gomoku-take-back)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
906 (gomoku-prompt-for-move))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
907
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
908 (defun gomoku-human-resigns ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
909 "Signal to the Gomoku program that you may want to resign."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
910 (interactive)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
911 (gomoku-switch-to-window)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
912 (cond
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
913 (gomoku-emacs-is-computing
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
914 (gomoku-crash-game))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
915 ((not gomoku-game-in-progress)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
920 (gomoku-prompt-for-move))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
921 (t
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
922 (gomoku-terminate-game 'human-resigned)))) ; OK. Accept it
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
923
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
924 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
925 ;;; PROMPTING THE HUMAN PLAYER.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
926 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
927
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
928 (defun gomoku-prompt-for-move ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
929 "Display a message asking for Human's move."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
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 ...)"
65689
96187e7edd1b 2005-09-25 Romain Francoise <romain@orebokech.com>
Romain Francoise <romain@orebokech.com>
parents: 64993
diff changeset
932 "Your move?"))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
933 ;; This may seem silly, but if one omits the following line (or a similar
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
934 ;; one), the cursor may very well go to some place where POINT is not.
105829
328150f0cf76 * url-util.el (url-insert-entities-in-string):
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 104390
diff changeset
935 ;; FIXME: this can't be right!! --Stef
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
936 (save-excursion (set-buffer (other-buffer))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
937
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
938 (defun gomoku-prompt-for-other-game ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
939 "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
940 (if (y-or-n-p "Another game? ")
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
941 (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
942 (error "Chicken!")))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
943
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
944 (defun gomoku-offer-a-draw ()
42205
7308bbc423d5 Doc fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 41347
diff changeset
945 "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
946 (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
947 (not (setq gomoku-human-refused-draw t))))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
948
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
949 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
950 ;;; DISPLAYING THE BOARD.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
951 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
952
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
953 (defun gomoku-max-width ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
954 "Largest possible board width for the current window."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
955 (1+ (/ (- (window-width (selected-window))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
956 gomoku-x-offset gomoku-x-offset 1)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
957 gomoku-square-width)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
958
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
959 (defun gomoku-max-height ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
960 "Largest possible board height for the current window."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
961 (1+ (/ (- (window-height (selected-window))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
962 gomoku-y-offset gomoku-y-offset 2)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
963 ;; 2 instead of 1 because WINDOW-HEIGHT includes the mode line !
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
964 gomoku-square-height)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
965
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
966 (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
967 "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
968 (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
969 (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
970 gomoku-square-height))))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
971
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
972 (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
973 "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
974 (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
975 (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
976 gomoku-square-width))
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
977 (gomoku-point-y))))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
978
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
979 (defun gomoku-goto-square (index)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
980 "Move point to square number INDEX."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
981 (gomoku-goto-xy (gomoku-index-to-x index) (gomoku-index-to-y index)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
982
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
983 (defun gomoku-goto-xy (x y)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
984 "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
985 (let ((inhibit-point-motion-hooks t))
104390
c6ae8d43800c Use forward-line, not goto-line.
Glenn Morris <rgm@gnu.org>
parents: 100908
diff changeset
986 (goto-char (point-min))
c6ae8d43800c Use forward-line, not goto-line.
Glenn Morris <rgm@gnu.org>
parents: 100908
diff changeset
987 (forward-line (+ gomoku-y-offset (* gomoku-square-height (1- y)))))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
988 (move-to-column (+ gomoku-x-offset (* gomoku-square-width (1- x)))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
989
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
990 (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
991 "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
992 (or (= value 1)
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
993 (gomoku-goto-square square))
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
994 (let ((inhibit-read-only t)
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
995 (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
996 (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
997 ((= value 6) ?O)
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
998 (?.)))
30543
cae243d66bbe (gomoku-font-lock-O-face, gomoku-font-lock-X-face)
Eli Zaretskii <eliz@gnu.org>
parents: 23869
diff changeset
999 (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
1000 (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
1001 (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
1002 '(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
1003 (delete-char 1)
14948
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1004 (backward-char 1))
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1005 (sit-for 0)) ; Display NOW
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1006
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1007 (defun gomoku-init-display (n m)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1008 "Display an N by M Gomoku board."
52
2e64db0383ef *** empty log message ***
Joseph Arceneaux <jla@gnu.org>
parents: 36
diff changeset
1009 (buffer-disable-undo (current-buffer))
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1010 (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
1011 (point 1) opoint
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1012 (intangible t)
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1013 (i m) j x)
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1014 ;; 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
1015 (setq tab-width
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1016 (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
1017 gomoku-square-width
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1018 (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
1019 gomoku-square-width 1) 2) 2)))
5822
db219f81f5de (gomoku-init-display, gomoku-put-char):
Richard M. Stallman <rms@gnu.org>
parents: 4402
diff changeset
1020 (erase-buffer)
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1021 (newline gomoku-y-offset)
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1022 (while (progn
14948
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1023 (setq j n
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1024 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
1025 (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
1026 (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
1027 (current-column))
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1028 tab-width))
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1029 (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
1030 (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
1031 (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
1032 (and (zerop j)
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1033 (= i (- m 2))
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1034 (progn
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1035 (while (>= i 3)
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1036 (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
1037 (setq i (- i 2)))
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1038 (goto-char (point-max))))
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1039 (setq point (point))
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1040 (insert ?.)
38075
437623e4fe1c (gomoku-plot-square, gomoku-init-display): Add help-echo to mouse-highlighted
Eli Zaretskii <eliz@gnu.org>
parents: 30543
diff changeset
1041 (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
1042 point (point)
437623e4fe1c (gomoku-plot-square, gomoku-init-display): Add help-echo to mouse-highlighted
Eli Zaretskii <eliz@gnu.org>
parents: 30543
diff changeset
1043 '(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
1044 help-echo "mouse-2: play at this square")))
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1045 (> (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
1046 (if (= i (1- m))
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1047 (setq opoint point))
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1048 (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
1049 (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
1050 (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
1051 (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
1052 (or intangible
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1053 (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
1054 (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
1055 (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
1056 (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
1057 (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
1058 (sit-for 0)) ; Display NOW
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1059
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1060 (defun gomoku-display-statistics ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1061 "Obnoxiously display some statistics about previous games in mode line."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1062 ;; We store this string in the mode-line-process local variable.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1063 ;; This is certainly not the cleanest way out ...
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1064 (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
1065 (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
1066 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
1067 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
1068 (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
1069 ""
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1070 (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
1071 (force-mode-line-update))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1072
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1073 (defun gomoku-switch-to-window ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1074 "Find or create the Gomoku buffer, and display it."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1075 (interactive)
50563
23dfa4b79c21 (gomoku-buffer-name): New constant.
Glenn Morris <rgm@gnu.org>
parents: 49598
diff changeset
1076 (if (get-buffer gomoku-buffer-name) ; Buffer exists:
23dfa4b79c21 (gomoku-buffer-name): New constant.
Glenn Morris <rgm@gnu.org>
parents: 49598
diff changeset
1077 (switch-to-buffer gomoku-buffer-name) ; no problem.
23dfa4b79c21 (gomoku-buffer-name): New constant.
Glenn Morris <rgm@gnu.org>
parents: 49598
diff changeset
1078 (if gomoku-game-in-progress
23dfa4b79c21 (gomoku-buffer-name): New constant.
Glenn Morris <rgm@gnu.org>
parents: 49598
diff changeset
1079 (gomoku-crash-game)) ; buffer has been killed or something
23dfa4b79c21 (gomoku-buffer-name): New constant.
Glenn Morris <rgm@gnu.org>
parents: 49598
diff changeset
1080 (switch-to-buffer gomoku-buffer-name) ; Anyway, start anew.
23dfa4b79c21 (gomoku-buffer-name): New constant.
Glenn Morris <rgm@gnu.org>
parents: 49598
diff changeset
1081 (gomoku-mode)))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1082
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1083 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1084 ;;; CROSSING WINNING QTUPLES.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1085 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1086
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1087 ;; When someone succeeds in filling a qtuple, we draw a line over the five
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1088 ;; corresponding squares. One problem is that the program does not know which
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1089 ;; squares ! It only knows the square where the last move has been played and
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1090 ;; who won. The solution is to scan the board along all four directions.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1091
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1092 (defun gomoku-find-filled-qtuple (square value)
42205
7308bbc423d5 Doc fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 41347
diff changeset
1093 "Return t if SQUARE belongs to a qtuple filled with VALUEs."
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1094 (or (gomoku-check-filled-qtuple square value 1 0)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1095 (gomoku-check-filled-qtuple square value 0 1)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1096 (gomoku-check-filled-qtuple square value 1 1)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1097 (gomoku-check-filled-qtuple square value -1 1)))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1098
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1099 (defun gomoku-check-filled-qtuple (square value dx dy)
42205
7308bbc423d5 Doc fixes.
Pavel Janík <Pavel@Janik.cz>
parents: 41347
diff changeset
1100 "Return t if SQUARE belongs to a qtuple filled with VALUEs along DX, DY."
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1101 (let ((a 0) (b 0)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1102 (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
1103 (depl (gomoku-xy-to-index dx dy)))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1104 (while (and (> a -4) ; stretch tuple left
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1105 (= value (aref gomoku-board (setq left (- left depl)))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1106 (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
1107 (while (and (< b (+ a 4)) ; stretch tuple right
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1108 (= value (aref gomoku-board (setq right (+ right depl)))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1109 (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
1110 (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
1111 (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
1112 dx dy)
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1113 t))))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1114
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1115 (defun gomoku-cross-qtuple (square1 square2 dx dy)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1116 "Cross every square between SQUARE1 and SQUARE2 in the DX, DY direction."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1117 (save-excursion ; Not moving point from last square
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1118 (let ((depl (gomoku-xy-to-index dx dy))
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1119 (inhibit-read-only t)
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1120 (inhibit-point-motion-hooks t))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1121 ;; 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
1122 (while (/= square1 square2)
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1123 (gomoku-goto-square square1)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1124 (setq square1 (+ square1 depl))
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1125 (cond
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1126 ((= dy 0) ; Horizontal
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1127 (forward-char 1)
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1128 (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
1129 (delete-region (point) (progn
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1130 (skip-chars-forward " \t")
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1131 (point))))
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1132 ((= dx 0) ; Vertical
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1133 (let ((n 1)
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1134 (column (current-column)))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1135 (while (< n gomoku-square-height)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1136 (setq n (1+ n))
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1137 (forward-line 1)
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1138 (indent-to column)
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 ((= 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
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 ?/))
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1144 (t ; 2nd Diagonal
14948
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1145 (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
1146 (forward-line (/ gomoku-square-height 2))))
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1147 (insert-and-inherit ?\\))))))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1148 (sit-for 0)) ; Display NOW
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1149
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1150 ;;;
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1151 ;;; CURSOR MOTION.
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1152 ;;;
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1153 ;; previous-line and next-line don't work right with intangible newlines
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1154 (defun gomoku-move-down ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1155 "Move point down one row on the Gomoku board."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1156 (interactive)
14948
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1157 (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
1158 (let ((column (current-column)))
8fbfc7c3386c (gomoku-move-up, gomoku-move-down):
Richard M. Stallman <rms@gnu.org>
parents: 44081
diff changeset
1159 (forward-line gomoku-square-height)
8fbfc7c3386c (gomoku-move-up, gomoku-move-down):
Richard M. Stallman <rms@gnu.org>
parents: 44081
diff changeset
1160 (move-to-column column))))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1161
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1162 (defun gomoku-move-up ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1163 "Move point up one row on the Gomoku board."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1164 (interactive)
14948
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1165 (if (> (gomoku-point-y) 1)
47508
8fbfc7c3386c (gomoku-move-up, gomoku-move-down):
Richard M. Stallman <rms@gnu.org>
parents: 44081
diff changeset
1166 (let ((column (current-column)))
8fbfc7c3386c (gomoku-move-up, gomoku-move-down):
Richard M. Stallman <rms@gnu.org>
parents: 44081
diff changeset
1167 (forward-line (- 1 gomoku-square-height))
8fbfc7c3386c (gomoku-move-up, gomoku-move-down):
Richard M. Stallman <rms@gnu.org>
parents: 44081
diff changeset
1168 (move-to-column column))))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1169
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1170 (defun gomoku-move-ne ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1171 "Move point North East on the Gomoku board."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1172 (interactive)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1173 (gomoku-move-up)
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1174 (forward-char))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1175
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1176 (defun gomoku-move-se ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1177 "Move point South East on the Gomoku board."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1178 (interactive)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1179 (gomoku-move-down)
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1180 (forward-char))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1181
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1182 (defun gomoku-move-nw ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1183 "Move point North West on the Gomoku board."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1184 (interactive)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1185 (gomoku-move-up)
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1186 (backward-char))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1187
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1188 (defun gomoku-move-sw ()
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1189 "Move point South West on the Gomoku board."
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1190 (interactive)
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1191 (gomoku-move-down)
14860
65dba0cd5306 Ancient leading comment removed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1192 (backward-char))
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1193
14948
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1194 (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
1195 "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
1196 (interactive)
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1197 (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
1198
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1199 (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
1200 "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
1201 (interactive)
044a7dcbeb06 (gomoku-click): Position on nearest square. Adapt keymap accordingly.
Richard M. Stallman <rms@gnu.org>
parents: 14873
diff changeset
1202 (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
1203 (* 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
1204
76953
417f62c55294 Seed RNG on loading.
Glenn Morris <rgm@gnu.org>
parents: 75347
diff changeset
1205 (random t)
417f62c55294 Seed RNG on loading.
Glenn Morris <rgm@gnu.org>
parents: 75347
diff changeset
1206
584
4cd7543be581 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 207
diff changeset
1207 (provide 'gomoku)
36
9697c13298e5 Initial revision
Joseph Arceneaux <jla@gnu.org>
parents:
diff changeset
1208
660
08eb386dd0f3 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 584
diff changeset
1209 ;;; gomoku.el ends here