1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Maintain a chess game that is being played or viewed
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.
14 (defvar chess-game-inhibit-events nil)
16 (defconst chess-game-default-tags
17 `(("Event" . "Computer chess game")
19 ("Site" . ,(system-name))
23 ("TimeControl" . "-")))
25 (defsubst chess-game-hooks (game)
26 "Return the tags alist associated with GAME."
30 (defsubst chess-game-set-hooks (game hooks)
31 "Return the tags alist associated with GAME."
33 (assert (or hooks (eq hooks nil)))
36 (defun chess-game-add-hook (game function &optional data prepend)
37 "Return the tags alist associated with GAME."
40 (let ((hooks (chess-game-hooks game)))
42 (chess-game-set-hooks game (list (cons function data)))
44 (chess-game-set-hooks game (cons (cons function data) hooks))
45 (nconc hooks (list (cons function data)))))))
47 (defun chess-game-remove-hook (game function &optional data)
48 "Remove from GAME all event hooks that match FUNCTION.
49 If DATA is specified, only remove those hooks whose associated data
53 (let* ((hooks (chess-game-hooks game))
56 (if (and (eq (caar h) function)
60 (setcdr last-hook (cdr h))
64 (chess-game-set-hooks game hooks)))
66 (defsubst chess-game-run-hooks (game &rest args)
67 "Return the tags alist associated with GAME."
69 (unless chess-game-inhibit-events
71 (dolist (hook (chess-game-hooks game) result)
72 (setq result (apply (car hook) game (cdr hook) args))))))
75 (defsubst chess-game-tags (game)
76 "Return the tags alist associated with GAME."
80 (defsubst chess-game-set-tags (game tags)
81 "Return the tags alist associated with GAME."
83 (assert (or tags (eq tags nil)))
84 (setcar (cdr game) tags)
85 (chess-game-run-hooks game 'set-tags))
87 (defsubst chess-game-tag (game tag)
88 "Return the value for TAG in GAME."
91 (let ((tags (chess-game-tags game)))
92 (and tags (cdr (assoc tag tags)))))
94 (defun chess-game-set-tag (game tag value)
95 "Set a TAG for GAME to VALUE."
99 (let ((tags (chess-game-tags game)))
101 (chess-game-set-tags game (list (cons tag value)))
102 (let ((entry (assoc tag tags)))
105 (nconc tags (list (cons tag value)))))))
106 (chess-game-run-hooks game 'set-tag tag))
108 (defsubst chess-game-del-tag (game tag)
109 "Set a TAG for GAME to VALUE."
112 (chess-game-set-tags game (assq-delete-all tag (chess-game-tags game)))
113 (chess-game-run-hooks game 'delete-tag tag))
116 (defsubst chess-game-data-alist (game)
120 (defsubst chess-game-set-data-alist (game value)
122 (setcar (nthcdr 2 game) value))
124 (defun chess-game-set-data (game key value)
126 (assert (symbolp key))
127 (let* ((alist (chess-game-data-alist game))
128 (cell (assq key alist)))
132 (setcar (nthcdr 2 game) (list (cons key value)))
133 (push (cons key value) alist)
134 (setcar (nthcdr 2 game) alist)))
135 (chess-game-run-hooks game 'set-data key)
138 (defun chess-game-data (game key)
140 (assert (symbolp key))
141 (let ((alist (chess-game-data-alist game)))
143 (cdr (assq key alist)))))
145 (defun chess-game-del-data (game key)
147 (assert (symbolp key))
148 (let ((alist (chess-game-data-alist game)))
150 (assq-delete-all key alist))))
153 (defsubst chess-game-plies (game)
154 "Return the tags alist associated with GAME."
158 (defalias 'chess-game-main-var 'chess-game-plies)
160 (defsubst chess-game-set-plies (game plies)
161 "Return the tags alist associated with GAME."
163 (setcdr (nthcdr 2 game) (if plies (list plies) nil))
164 (chess-game-run-hooks game 'setup-game game))
166 (defsubst chess-game-set-start-position (game position)
167 "Return the tags alist associated with GAME."
169 (assert (vectorp position))
170 (chess-game-set-plies game (list (chess-ply-create* position))))
172 (defsubst chess-game-pos (game &optional index)
173 "Return the position related to GAME's INDEX position."
175 (chess-ply-pos (chess-game-ply game index)))
177 (defun chess-game-status (game &optional index)
178 "Return a symbol, such as :checkmate, :resign, etc.
179 This conveys the status of the game at the given index."
181 (or (chess-pos-status (chess-game-pos game index))
182 (let ((final (chess-ply-final-p (chess-game-ply game index))))
183 (and (memq final '(:aborted :resign :drawn :perpetual :repetition
187 (defsubst chess-game-index (game)
188 "Return the GAME's current position index."
190 (1- (length (chess-game-plies game))))
192 (defun chess-game-seq (game)
193 "Return the current GAME sequence."
195 (let ((index (chess-game-index game)))
197 (if (= (mod index 2) 0)
202 (defsubst chess-game-side-to-move (game &optional index)
204 (chess-pos-side-to-move (chess-game-pos game index)))
206 (defun chess-game-ply (game &optional index)
207 "Return the position related to GAME's INDEX position."
210 (nth index (chess-game-plies game))
211 (car (last (chess-game-plies game)))))
213 (defun chess-game-add-ply (game ply)
214 "Return the position related to GAME's INDEX position."
217 (let ((plies (chess-game-plies game)))
219 (nconc plies (list ply))
220 (let ((chess-game-inhibit-events t))
221 (chess-game-set-plies game (list ply))))))
223 (chess-message-catalog 'english
224 '((undo-limit-reached . "Cannot undo further")
225 (add-to-completed . "Cannot add moves to a completed game")))
227 (defun chess-game-undo (game count)
228 "Undo the last COUNT plies of GAME."
230 (assert (integerp count))
231 (if (> count (chess-game-index game))
232 (chess-error 'undo-limit-reached))
233 (let ((chess-game-inhibit-events t))
234 (chess-game-set-plies game (nbutlast (chess-game-plies game) count)))
235 (chess-game-run-hooks game 'post-undo count))
238 (defun chess-game-strip-annotations (game)
239 "Strip all annotations from the given GAME."
241 (dotimes (i (chess-game-index game))
242 (let ((position (chess-game-pos game i)))
243 (chess-pos-set-annotations position nil))))
246 (defsubst chess-game-over-p (game)
247 "Return the position related to GAME's INDEX position."
249 (let ((last-ply (car (last (nth 3 game) 2))))
250 (and last-ply (chess-ply-final-p last-ply))))
253 (defsubst chess-game-to-string (game &optional indented)
255 (chess-game-to-pgn game indented t))
257 (defsubst chess-game-from-string (pgn)
258 (assert (stringp pgn))
259 (chess-pgn-to-game pgn))
262 (defsubst chess-game-copy-game (game new-game)
265 (chess-game-set-tags game (chess-game-tags new-game))
266 (chess-game-set-plies game (chess-game-plies new-game)))
269 (defun chess-game-create (&optional position tags)
270 "Create a new chess game object.
271 Optionally use the given starting POSITION.
272 TAGS is the starting set of game tags (which can always be changed
273 later using the various tag-related methods)."
274 (let ((game (list nil tags nil
275 (list (chess-ply-create* (or position
276 chess-starting-position))))))
277 (dolist (tag (cons (cons "Date" (format-time-string "%Y.%m.%d"))
278 chess-game-default-tags))
279 (unless (chess-game-tag game (car tag))
280 (chess-game-set-tag game (car tag) (cdr tag))))
283 (defun chess-game-move (game ply)
284 "Make a move in the current GAME, from FROM to TO.
285 This creates a new position and adds it to the main variation.
286 The 'changes' of the last ply reflect whether the game is currently in
287 progress (nil), if it is drawn, resigned, mate, etc."
290 (let ((current-ply (chess-game-ply game))
291 (changes (chess-ply-changes ply))
292 (position (chess-ply-pos ply)))
294 (if (chess-ply-final-p current-ply)
295 (chess-error 'add-to-completed))
298 (assert (and position (eq position (chess-ply-pos current-ply))))
301 (chess-ply-set-changes current-ply changes)
302 (unless (chess-ply-any-keyword ply :drawn :perpetual :repetition
303 :resign :aborted :flag-fell)
304 (chess-game-add-ply game (chess-ply-create*
305 (chess-ply-next-pos current-ply))))
307 (let ((long (> (length changes) 2)))
309 ((and long (chess-ply-any-keyword ply :resign :checkmate))
310 (let ((color (chess-game-side-to-move game)))
311 (if (chess-ply-any-keyword ply :resign :flag-fell)
312 (chess-game-set-tag game "Result" (if color "0-1" "1-0"))
313 (chess-game-set-tag game "Result" (if color "1-0" "0-1")))))
314 ((and long (chess-ply-any-keyword ply :drawn :perpetual :repetition
316 (chess-game-set-tag game "Result" "1/2-1/2"))))
318 (chess-game-run-hooks game 'move current-ply)
319 (chess-game-run-hooks game 'post-move)))
321 (defsubst chess-game-end (game keyword)
322 "End the current game, by resignation, draw, etc."
323 (chess-game-move game (list (chess-game-pos game) keyword)))
325 (provide 'chess-game)
327 ;;; chess-game.el ends here