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.
16 (defvar chess-game-inhibit-events nil)
18 (defconst chess-game-default-tags
19 `(("Event" . "Computer chess game")
21 ("Site" . ,(system-name))
25 ("TimeControl" . "-")))
27 (defsubst chess-game-hooks (game)
28 "Return the event hooks associated with GAME."
32 (defsubst chess-game-set-hooks (game hooks)
33 "Set the event hooks associated with GAME."
35 (assert (or hooks (eq hooks nil)))
38 (defun chess-game-add-hook (game function &optional data prepend)
39 "Add to GAME an event hook FUNCTION."
42 (let ((hooks (chess-game-hooks game)))
44 (chess-game-set-hooks game (list (cons function data)))
46 (chess-game-set-hooks game (cons (cons function data) hooks))
47 (nconc hooks (list (cons function data)))))))
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
55 (let* ((hooks (chess-game-hooks game))
58 (if (and (eq (caar h) function)
62 (setcdr last-hook (cdr h))
66 (chess-game-set-hooks game hooks)))
68 (defsubst chess-game-run-hooks (game &rest args)
69 "Run the event hooks of GAME and pass ARGS."
71 (unless chess-game-inhibit-events
73 (dolist (hook (chess-game-hooks game) result)
74 (setq result (apply (car hook) game (cdr hook) args))))))
76 (defsubst chess-game-tags (game)
77 "Return the tags alist associated with GAME."
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."
85 (assert (or tags (eq tags nil)))
86 (setcar (cdr game) tags)
87 (chess-game-run-hooks game 'set-tags))
89 (defsubst chess-game-tag (game tag)
90 "Return the value for TAG in GAME."
93 (let ((tags (chess-game-tags game)))
94 (and tags (cdr (assoc tag tags)))))
96 (defun chess-game-set-tag (game tag value)
97 "Set TAG for GAME to VALUE."
101 (let ((tags (chess-game-tags game)))
103 (chess-game-set-tags game (list (cons tag value)))
104 (let ((entry (assoc tag tags)))
107 (nconc tags (list (cons tag value)))))))
108 (chess-game-run-hooks game 'set-tag tag))
110 (defsubst chess-game-del-tag (game tag)
111 "Delete TAG from GAME."
114 (chess-game-set-tags game (assq-delete-all tag (chess-game-tags game)))
115 (chess-game-run-hooks game 'delete-tag tag))
118 (defsubst chess-game-data-alist (game)
119 "Return the data alist associated with GAME."
123 (defsubst chess-game-set-data-alist (game value)
124 "Set the data alist associated with GAME."
126 (setcar (nthcdr 2 game) value))
128 (defun chess-game-set-data (game key value)
129 "Set GAME data KEY to VALUE."
131 (assert (symbolp key))
132 (let* ((alist (chess-game-data-alist game))
133 (cell (assq key 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)
143 (defun chess-game-data (game key)
144 "Return the value of GAME data KEY."
146 (assert (symbolp key))
147 (let ((alist (chess-game-data-alist game)))
149 (cdr (assq key alist)))))
151 (defun chess-game-del-data (game key)
152 "Delete KEY from GAME's data alist."
154 (assert (symbolp key))
155 (let ((alist (chess-game-data-alist game)))
157 (assq-delete-all key alist))))
160 (defsubst chess-game-plies (game)
161 "Return the main variation of GAME as a list of plies."
165 (defalias 'chess-game-main-var 'chess-game-plies)
167 (defsubst chess-game-set-plies (game plies)
168 "Set the list of plies which represents the main variation of GAME."
170 (setcdr (nthcdr 2 game) (if plies (list plies) nil))
171 (chess-game-run-hooks game 'setup-game game))
173 (defsubst chess-game-set-start-position (game position)
174 "Set the initial POSITION of GAME."
176 (assert (vectorp position))
177 (chess-game-set-plies game (list (chess-ply-create* position))))
179 (defsubst chess-game-pos (game &optional index)
180 "Return the current position of GAME or a position of a given INDEX."
182 (chess-ply-pos (chess-game-ply game index)))
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."
188 (or (chess-pos-status (chess-game-pos game index))
189 (chess-ply-final-p (chess-game-ply game index))))
191 (defsubst chess-game-index (game)
192 "Return the GAME's current position index."
194 (1- (length (chess-game-plies game))))
196 (defsubst chess-game-seq (game)
197 "Return the current GAME sequence."
199 (/ (+ 2 (chess-game-index game)) 2))
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
205 (chess-pos-side-to-move (chess-game-pos game index)))
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."
212 (nth index (chess-game-plies game))
213 (car (last (chess-game-plies game)))))
215 (defun chess-game-add-ply (game ply)
216 "Return the position related to GAME's INDEX position."
219 (let ((plies (chess-game-plies game)))
221 (nconc plies (list ply))
222 (let ((chess-game-inhibit-events t))
223 (chess-game-set-plies game (list ply))))))
225 (chess-message-catalog 'english
226 '((undo-limit-reached . "Cannot undo further")
227 (add-to-completed . "Cannot add moves to a completed game")))
229 (defun chess-game-undo (game count)
230 "Undo the last COUNT plies of GAME."
232 (assert (integerp count))
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))
241 (defun chess-game-strip-annotations (game)
242 "Strip all annotations from the given GAME."
244 (dotimes (i (chess-game-index game))
245 (let ((position (chess-game-pos game i)))
246 (chess-pos-set-annotations position nil))))
249 (defsubst chess-game-over-p (game)
250 "Return non-nil if GAME is at a final positionn."
252 (let ((last-ply (car (last (nth 3 game) 2))))
253 (and last-ply (chess-ply-final-p last-ply))))
256 (defsubst chess-game-to-string (game &optional indented)
257 "Convert GAME to a string in PGN format."
259 (chess-game-to-pgn game indented t))
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))
267 (defsubst chess-game-copy-game (game new-game)
270 (chess-game-set-tags game (chess-game-tags new-game))
271 (chess-game-set-plies game (chess-game-plies new-game)))
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))))
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."
295 (let ((current-ply (chess-game-ply game))
296 (position (chess-ply-pos ply))
297 (changes (chess-ply-changes ply)))
300 (assert (and position (eq position (chess-ply-pos current-ply))))
303 (if (chess-ply-final-p current-ply)
304 (chess-error 'add-to-completed))
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))))
312 (let ((long (> (length changes) 2)))
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
321 (chess-game-set-tag game "Result" "1/2-1/2"))))
323 (if (chess-ply-keyword ply :resign)
324 (chess-game-run-hooks game 'resign)
325 (chess-game-run-hooks game 'move current-ply))))
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)))
331 (provide 'chess-game)
333 ;;; chess-game.el ends here