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.
13 (defvar chess-game-inhibit-events nil)
15 (defconst chess-game-default-tags
16 `(("Event" . "Computer chess game")
18 ("Site" . ,(system-name))
22 ("TimeControl" . "-")))
24 (defsubst chess-game-hooks (game)
25 "Return the tags alist associated with GAME."
28 (defsubst chess-game-set-hooks (game hooks)
29 "Return the tags alist associated with GAME."
32 (defun chess-game-add-hook (game function &optional data prepend)
33 "Return the tags alist associated with GAME."
34 (let ((hooks (chess-game-hooks game)))
36 (chess-game-set-hooks game (list (cons function data)))
38 (chess-game-set-hooks game (cons (cons function data) hooks))
39 (nconc hooks (list (cons function data)))))))
41 (defun chess-game-remove-hook (game function &optional data)
42 "Remove from GAME all event hooks that match FUNCTION.
43 If DATA is specified, only remove those hooks whose associated data
45 (let* ((hooks (chess-game-hooks game))
48 (if (and (eq (caar h) function)
52 (setcdr last-hook (cdr h))
56 (chess-game-set-hooks game hooks)))
58 (defsubst chess-game-run-hooks (game &rest args)
59 "Return the tags alist associated with GAME."
60 (unless chess-game-inhibit-events
62 (dolist (hook (chess-game-hooks game) result)
63 (setq result (apply (car hook) game (cdr hook) args))))))
66 (defsubst chess-game-tags (game)
67 "Return the tags alist associated with GAME."
70 (defsubst chess-game-set-tags (game tags)
71 "Return the tags alist associated with GAME."
72 (setcar (cdr game) tags)
73 (chess-game-run-hooks game 'set-tags))
75 (defsubst chess-game-tag (game tag)
76 "Return the value for TAG in GAME."
77 (let ((tags (chess-game-tags game)))
78 (and tags (cdr (assoc tag tags)))))
80 (defun chess-game-set-tag (game tag value)
81 "Set a TAG for GAME to VALUE."
82 (let ((tags (chess-game-tags game)))
84 (chess-game-set-tags game (list (cons tag value)))
85 (let ((entry (assoc tag tags)))
88 (nconc tags (list (cons tag value)))))))
89 (chess-game-run-hooks game 'set-tag tag))
91 (defsubst chess-game-del-tag (game tag)
92 "Set a TAG for GAME to VALUE."
93 (chess-game-set-tags game (assq-delete-all tag (chess-game-tags game)))
94 (chess-game-run-hooks game 'delete-tag tag))
97 (defsubst chess-game-data-alist (game)
100 (defsubst chess-game-set-data-alist (game value)
101 (setcar (nthcdr 2 game) value))
103 (defun chess-game-set-data (game key value)
104 (let* ((alist (chess-game-data-alist game))
105 (cell (assq key alist)))
109 (setcar (nthcdr 2 game) (list (cons key value)))
110 (push (cons key value) alist)
111 (setcar (nthcdr 2 game) alist)))
112 (chess-game-run-hooks game 'set-data key)))
114 (defun chess-game-data (game key)
115 (let ((alist (chess-game-data-alist game)))
117 (cdr (assq key alist)))))
119 (defun chess-game-del-data (game key)
120 (let ((alist (chess-game-data-alist game)))
122 (assq-delete-all key alist))))
125 (defsubst chess-game-plies (game)
126 "Return the tags alist associated with GAME."
129 (defalias 'chess-game-main-var 'chess-game-plies)
131 (defsubst chess-game-set-plies (game plies)
132 "Return the tags alist associated with GAME."
133 (setcdr (nthcdr 2 game) (list plies))
134 (chess-game-run-hooks game 'setup-game game))
136 (defsubst chess-game-set-start-position (game position)
137 "Return the tags alist associated with GAME."
138 (chess-game-set-plies game (list (chess-ply-create* position))))
140 (defsubst chess-game-pos (game &optional index)
141 "Return the position related to GAME's INDEX position."
142 (chess-ply-pos (chess-game-ply game index)))
144 (defsubst chess-game-index (game)
145 "Return the GAME's current position index."
146 (1- (length (chess-game-plies game))))
148 (defun chess-game-seq (game)
149 "Return the current GAME sequence."
150 (let ((index (chess-game-index game)))
155 (defsubst chess-game-side-to-move (game)
156 (chess-pos-side-to-move (chess-game-pos game)))
158 (defun chess-game-ply (game &optional index)
159 "Return the position related to GAME's INDEX position."
161 (nth index (chess-game-plies game))
162 (car (last (chess-game-plies game)))))
164 (defun chess-game-add-ply (game ply)
165 "Return the position related to GAME's INDEX position."
166 (let ((plies (chess-game-plies game)))
168 (nconc plies (list ply))
169 (let ((chess-game-inhibit-events t))
170 (chess-game-set-plies game (list ply))))))
172 (chess-message-catalog 'english
173 '((undo-limit-reached . "Cannot undo further")
174 (add-to-completed . "Cannot add moves to a completed game")))
176 (defun chess-game-undo (game count)
177 "Undo the last COUNT plies of GAME."
178 (if (> count (chess-game-index game))
179 (chess-error 'undo-limit-reached))
180 (let ((chess-game-inhibit-events t))
181 (chess-game-set-plies game (nbutlast (chess-game-plies game) count)))
182 (chess-game-run-hooks game 'post-undo count))
185 (defsubst chess-game-over-p (game)
186 "Return the position related to GAME's INDEX position."
187 (let ((last-ply (car (last game 2))))
188 (and last-ply (chess-ply-final-p last-ply))))
191 (defsubst chess-game-to-string (game &optional indented)
192 (chess-game-to-pgn game indented t))
194 (defsubst chess-game-from-string (pgn)
195 (chess-pgn-to-game pgn))
198 (defsubst chess-game-copy-game (game new-game)
199 (chess-game-set-tags game (chess-game-tags new-game))
200 (chess-game-set-plies game (chess-game-plies new-game)))
203 (defun chess-game-create (&optional position tags)
204 "Create a new chess game object.
205 Optionally use the given starting POSITION.
206 TAGS is the starting set of game tags (which can always be changed
207 later using the various tag-related methods)."
208 (let ((game (list nil tags nil
209 (list (chess-ply-create* (or position
212 (dolist (tag (cons (cons "Date" (format-time-string "%Y.%m.%d"))
213 chess-game-default-tags))
214 (unless (chess-game-tag game (car tag))
215 (chess-game-set-tag game (car tag) (cdr tag))))
218 (defun chess-game-move (game ply)
219 "Make a move in the current GAME, from FROM to TO.
220 This creates a new position and adds it to the main variation.
221 The 'changes' of the last ply reflect whether the game is currently in
222 progress (nil), if it is drawn, resigned, mate, etc."
223 (let ((current-ply (chess-game-ply game))
224 (changes (chess-ply-changes ply))
225 (position (chess-ply-pos ply)))
226 (if (chess-ply-final-p current-ply)
227 (chess-error 'add-to-completed))
228 (assert (equal position (chess-ply-pos current-ply)))
229 (chess-ply-set-changes current-ply changes)
230 (chess-game-add-ply game (chess-ply-create*
231 (chess-ply-next-pos current-ply) t))
233 ((chess-ply-any-keyword ply :draw :perpetual :repetition :stalemate)
234 (chess-game-set-tag game "Result" "1/2-1/2")
235 (chess-game-run-hooks game 'game-drawn))
237 ((chess-ply-any-keyword ply :resign :checkmate)
238 (let ((color (chess-game-side-to-move game)))
239 (chess-game-set-tag game "Result" (if color "0-1" "1-0"))
240 (if (chess-ply-keyword ply :resign)
241 (chess-game-run-hooks game 'resign color)
242 (chess-game-run-hooks game 'move current-ply))))
245 (chess-game-run-hooks game 'move current-ply)))
246 (chess-game-run-hooks game 'post-move)))
248 (defsubst chess-game-end (game keyword)
249 "End the current game, by resignation, draw, etc."
250 (chess-game-move game (list (chess-game-pos game) keyword)))
252 (provide 'chess-game)
254 ;;; chess-game.el ends here