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