]> code.delx.au - gnu-emacs-elpa/blob - chess-game.el
Add proper file header.
[gnu-emacs-elpa] / chess-game.el
1 ;;; chess-game.el --- Maintain a chess game that is being played or viewed
2
3 ;; Copyright (C) 2002, 2004, 2014 Free Software Foundation, Inc.
4
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Maintainer: Mario Lang <mlang@delysid.org>
7 ;; Keywords: games
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;; A chess game is represented by a set of tags that describe the
25 ;; game, and a list of plies representing the main variation.
26
27 ;;; Code:
28
29 (eval-when-compile
30 (require 'cl))
31 (require 'chess-ply)
32 (require 'chess-pgn)
33
34 (defvar chess-game-inhibit-events nil)
35
36 (defconst chess-game-default-tags
37 `(("Event" . "Computer chess game")
38 ("Round" . "-")
39 ("Site" . ,(system-name))
40 ("White" . "?")
41 ("Black" . "?")
42 ("Result" . "*")
43 ("TimeControl" . "-")))
44
45 (defsubst chess-game-hooks (game)
46 "Return the event hooks associated with GAME."
47 (assert game)
48 (car game))
49
50 (defsubst chess-game-set-hooks (game hooks)
51 "Set the event hooks associated with GAME."
52 (assert game)
53 (assert (or hooks (eq hooks nil)))
54 (setcar game hooks))
55
56 (defun chess-game-add-hook (game function &optional data prepend)
57 "Add to GAME an event hook FUNCTION."
58 (assert game)
59 (assert function)
60 (let ((hooks (chess-game-hooks game)))
61 (if (null hooks)
62 (chess-game-set-hooks game (list (cons function data)))
63 (if prepend
64 (chess-game-set-hooks game (cons (cons function data) hooks))
65 (nconc hooks (list (cons function data)))))))
66
67 (defun chess-game-remove-hook (game function &optional data)
68 "Remove from GAME all event hooks that match FUNCTION.
69 If DATA is specified, only remove those hooks whose associated data
70 matches."
71 (assert game)
72 (assert function)
73 (let* ((hooks (chess-game-hooks game))
74 (h hooks) last-hook)
75 (while h
76 (if (and (eq (caar h) function)
77 (or (null data)
78 (eq data (cdar h))))
79 (if last-hook
80 (setcdr last-hook (cdr h))
81 (setq hooks (cdr h)))
82 (setq last-hook h))
83 (setq h (cdr h)))
84 (chess-game-set-hooks game hooks)))
85
86 (defsubst chess-game-run-hooks (game &rest args)
87 "Run the event hooks of GAME and pass ARGS."
88 (assert game)
89 (unless chess-game-inhibit-events
90 (let (result)
91 (dolist (hook (chess-game-hooks game) result)
92 (setq result (apply (car hook) game (cdr hook) args))))))
93
94 (defsubst chess-game-tags (game)
95 "Return the tags alist associated with GAME."
96 (assert game)
97 (cadr game))
98
99 (defsubst chess-game-set-tags (game tags)
100 "Set the tags alist associated with GAME.
101 After the TAGS alist was set the 'set-tags event is triggered."
102 (assert game)
103 (assert (or tags (eq tags nil)))
104 (setcar (cdr game) tags)
105 (chess-game-run-hooks game 'set-tags))
106
107 (defsubst chess-game-tag (game tag)
108 "Return the value for TAG in GAME."
109 (assert game)
110 (assert tag)
111 (let ((tags (chess-game-tags game)))
112 (and tags (cdr (assoc tag tags)))))
113
114 (defun chess-game-set-tag (game tag value)
115 "Set TAG for GAME to VALUE."
116 (assert game)
117 (assert tag)
118 (assert value)
119 (let ((tags (chess-game-tags game)))
120 (if (null tags)
121 (chess-game-set-tags game (list (cons tag value)))
122 (let ((entry (assoc tag tags)))
123 (if entry
124 (setcdr entry value)
125 (nconc tags (list (cons tag value)))))))
126 (chess-game-run-hooks game 'set-tag tag))
127
128 (defsubst chess-game-del-tag (game tag)
129 "Delete TAG from GAME."
130 (assert game)
131 (assert tag)
132 (chess-game-set-tags game (assq-delete-all tag (chess-game-tags game)))
133 (chess-game-run-hooks game 'delete-tag tag))
134
135
136 (defsubst chess-game-data-alist (game)
137 "Return the data alist associated with GAME."
138 (assert game)
139 (nth 2 game))
140
141 (defsubst chess-game-set-data-alist (game value)
142 "Set the data alist associated with GAME."
143 (assert game)
144 (setcar (nthcdr 2 game) value))
145
146 (defun chess-game-set-data (game key value)
147 "Set GAME data KEY to VALUE."
148 (assert game)
149 (assert (symbolp key))
150 (let* ((alist (chess-game-data-alist game))
151 (cell (assq key alist)))
152 (if cell
153 (setcdr cell value)
154 (if (null alist)
155 (setcar (nthcdr 2 game) (list (cons key value)))
156 (push (cons key value) alist)
157 (setcar (nthcdr 2 game) alist)))
158 (chess-game-run-hooks game 'set-data key)
159 value))
160
161 (defun chess-game-data (game key)
162 "Return the value of GAME data KEY."
163 (assert game)
164 (assert (symbolp key))
165 (let ((alist (chess-game-data-alist game)))
166 (if alist
167 (cdr (assq key alist)))))
168
169 (defun chess-game-del-data (game key)
170 "Delete KEY from GAME's data alist."
171 (assert game)
172 (assert (symbolp key))
173 (let ((alist (chess-game-data-alist game)))
174 (if alist
175 (assq-delete-all key alist))))
176
177
178 (defsubst chess-game-plies (game)
179 "Return the main variation of GAME as a list of plies."
180 (assert game)
181 (nth 3 game))
182
183 (defalias 'chess-game-main-var 'chess-game-plies)
184
185 (defsubst chess-game-set-plies (game plies)
186 "Set the list of plies which represents the main variation of GAME."
187 (assert game)
188 (setcdr (nthcdr 2 game) (if plies (list plies) nil))
189 (chess-game-run-hooks game 'setup-game game))
190
191 (defsubst chess-game-set-start-position (game position)
192 "Set the initial POSITION of GAME."
193 (assert game)
194 (assert (vectorp position))
195 (chess-game-set-plies game (list (chess-ply-create* position))))
196
197 (defsubst chess-game-pos (game &optional index)
198 "Return the current position of GAME or a position of a given INDEX."
199 (assert game)
200 (chess-ply-pos (chess-game-ply game index)))
201
202 (defun chess-game-status (game &optional index)
203 "Return a symbol, such as :checkmate, :resign, etc.
204 This conveys the status of the game at the given INDEX."
205 (assert game)
206 (or (chess-pos-status (chess-game-pos game index))
207 (chess-ply-final-p (chess-game-ply game index))))
208
209 (defsubst chess-game-index (game)
210 "Return the GAME's current position index."
211 (assert game)
212 (1- (length (chess-game-plies game))))
213
214 (defsubst chess-game-seq (game)
215 "Return the current GAME sequence."
216 (assert game)
217 (/ (+ 2 (chess-game-index game)) 2))
218
219 (defsubst chess-game-side-to-move (game &optional index)
220 "Return the color whose move it is in GAME at INDEX (or at the last position
221 if INDEX is nil)."
222 (assert game)
223 (chess-pos-side-to-move (chess-game-pos game index)))
224
225 (defun chess-game-ply (game &optional index)
226 "Return a ply of GAME.
227 If INDEX is non-nil, the last played ply is returned."
228 (assert game)
229 (if index
230 (nth index (chess-game-plies game))
231 (car (last (chess-game-plies game)))))
232
233 (defun chess-game-add-ply (game ply)
234 "Return the position related to GAME's INDEX position."
235 (assert game)
236 (assert (listp ply))
237 (let ((plies (chess-game-plies game)))
238 (if plies
239 (nconc plies (list ply))
240 (let ((chess-game-inhibit-events t))
241 (chess-game-set-plies game (list ply))))))
242
243 (chess-message-catalog 'english
244 '((undo-limit-reached . "Cannot undo further")
245 (add-to-completed . "Cannot add moves to a completed game")))
246
247 (defun chess-game-undo (game count)
248 "Undo the last COUNT plies of GAME."
249 (assert game)
250 (assert (integerp count))
251 (assert (> count 0))
252 (if (> count (chess-game-index game))
253 (chess-error 'undo-limit-reached))
254 (let ((chess-game-inhibit-events t))
255 (chess-game-set-plies game (nbutlast (chess-game-plies game) count)))
256 (chess-game-run-hooks game 'post-undo count))
257
258
259 (defun chess-game-strip-annotations (game)
260 "Strip all annotations from the given GAME."
261 (assert game)
262 (dotimes (i (chess-game-index game))
263 (let ((position (chess-game-pos game i)))
264 (chess-pos-set-annotations position nil))))
265
266
267 (defsubst chess-game-over-p (game)
268 "Return non-nil if GAME is at a final positionn."
269 (assert game)
270 (let ((last-ply (car (last (nth 3 game) 2))))
271 (and last-ply (chess-ply-final-p last-ply))))
272
273
274 (defsubst chess-game-to-string (game &optional indented)
275 "Convert GAME to a string in PGN format."
276 (assert game)
277 (chess-game-to-pgn game indented t))
278
279 (defsubst chess-game-from-string (pgn)
280 "Convert a PGN format string to a chess game object."
281 (assert (stringp pgn))
282 (chess-pgn-to-game pgn))
283
284
285 (defsubst chess-game-copy-game (game new-game)
286 (assert game)
287 (assert new-game)
288 (chess-game-set-tags game (chess-game-tags new-game))
289 (chess-game-set-plies game (chess-game-plies new-game)))
290
291 (defun chess-game-create (&optional position tags)
292 "Create a new chess game object.
293 Optionally use the given starting POSITION (see also
294 `chess-game-set-start-position').
295 TAGS is the starting set of game tags (which can always be changed
296 later using the various tag-related methods)."
297 (let ((game (list nil tags nil
298 (list (chess-ply-create* (or position
299 chess-starting-position))))))
300 (dolist (tag (cons (cons "Date" (format-time-string "%Y.%m.%d"))
301 chess-game-default-tags))
302 (unless (chess-game-tag game (car tag))
303 (chess-game-set-tag game (car tag) (cdr tag))))
304 game))
305
306 (defun chess-game-move (game ply)
307 "Make a move in the current GAME using PLY.
308 This creates a new position and adds it to the main variation.
309 The 'changes' of the last ply reflect whether the game is currently in
310 progress (nil), if it is drawn, resigned, mate, etc."
311 (assert game)
312 (assert (listp ply))
313 (let ((current-ply (chess-game-ply game))
314 (position (chess-ply-pos ply))
315 (changes (chess-ply-changes ply)))
316
317 (assert current-ply)
318 (assert (and position (eq position (chess-ply-pos current-ply))))
319 (assert changes)
320
321 (if (chess-ply-final-p current-ply)
322 (chess-error 'add-to-completed))
323
324 (chess-ply-set-changes current-ply changes)
325 (unless (chess-ply-any-keyword ply :drawn :perpetual :repetition
326 :resign :aborted :flag-fell)
327 (chess-game-add-ply game (chess-ply-create*
328 (chess-ply-next-pos current-ply))))
329
330 (let ((long (> (length changes) 2)))
331 (cond
332 ((and long (chess-ply-any-keyword ply :resign :checkmate))
333 (let ((color (chess-game-side-to-move game)))
334 (if (chess-ply-any-keyword ply :resign :flag-fell)
335 (chess-game-set-tag game "Result" (if color "0-1" "1-0"))
336 (chess-game-set-tag game "Result" (if color "1-0" "0-1")))))
337 ((and long (chess-ply-any-keyword ply :drawn :perpetual :repetition
338 :stalemate))
339 (chess-game-set-tag game "Result" "1/2-1/2"))))
340
341 (if (chess-ply-keyword ply :resign)
342 (chess-game-run-hooks game 'resign)
343 (chess-game-run-hooks game 'move current-ply))))
344
345 (defsubst chess-game-end (game keyword)
346 "End GAME, by resignation, draw, etc."
347 (chess-game-move game (list (chess-game-pos game) keyword)))
348
349 (provide 'chess-game)
350
351 ;;; chess-game.el ends here