1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Maintain a chess game that is being played or viewed
9 ;; A chess game is represented by a set of tags that describe the
10 ;; 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."
29 (defsubst chess-game-set-hooks (game hooks)
30 "Return the tags alist associated with GAME."
33 (defun chess-game-add-hook (game function &optional data prepend)
34 "Return the tags alist associated with GAME."
35 (let ((hooks (chess-game-hooks game)))
37 (chess-game-set-hooks game (list (cons function data)))
39 (chess-game-set-hooks game (cons (cons function data) hooks))
40 (nconc hooks (list (cons function data)))))))
42 (defun chess-game-remove-hook (game function &optional data)
43 "Remove from GAME all event hooks that match FUNCTION.
44 If DATA is specified, only remove those hooks whose associated data
46 (let* ((hooks (chess-game-hooks game))
49 (if (and (eq (caar h) function)
53 (setcdr last-hook (cdr h))
57 (chess-game-set-hooks game hooks)))
59 (defsubst chess-game-run-hooks (game &rest args)
60 "Return the tags alist associated with GAME."
61 (unless chess-game-inhibit-events
62 (dolist (hook (chess-game-hooks game))
63 (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 (defun chess-game-set-data (game key value)
101 (let* ((alist (chess-game-data-alist game))
102 (cell (assq key alist)))
106 (setcar (nthcdr 2 game) (list (cons key value)))
107 (push (cons key value) alist)
108 (setcar (nthcdr 2 game) alist)))
109 (chess-game-run-hooks game 'set-data key)))
111 (defun chess-game-data (game key)
112 (let ((alist (chess-game-data-alist game)))
114 (cdr (assq key alist)))))
116 (defun chess-game-del-data (game key)
117 (let ((alist (chess-game-data-alist game)))
119 (assq-delete-all key alist))))
122 (defsubst chess-game-plies (game)
123 "Return the tags alist associated with GAME."
126 (defalias 'chess-game-main-var 'chess-game-plies)
128 (defsubst chess-game-set-plies (game plies)
129 "Return the tags alist associated with GAME."
130 (setcdr (nthcdr 2 game) (list plies))
131 (chess-game-run-hooks game 'setup-game game))
133 (defsubst chess-game-set-start-position (game position)
134 "Return the tags alist associated with GAME."
135 (chess-game-set-plies game (list (chess-ply-create position))))
137 (defsubst chess-game-pos (game &optional index)
138 "Return the position related to GAME's INDEX position."
139 (chess-ply-pos (chess-game-ply game index)))
141 (defsubst chess-game-index (game)
142 "Return the GAME's current position index."
143 (1- (length (chess-game-plies game))))
145 (defsubst chess-game-seq (game)
146 "Return the current GAME sequence."
147 (1+ (/ (chess-game-index game) 2)))
149 (defsubst chess-game-side-to-move (game)
150 (chess-pos-side-to-move (chess-game-pos game)))
152 (defun chess-game-ply (game &optional index)
153 "Return the position related to GAME's INDEX position."
155 (nth index (chess-game-plies game))
156 (car (last (chess-game-plies game)))))
158 (defun chess-game-add-ply (game ply)
159 "Return the position related to GAME's INDEX position."
160 (let ((plies (chess-game-plies game)))
162 (nconc plies (list ply))
163 (let ((chess-game-inhibit-events t))
164 (chess-game-set-plies game (list ply))))))
166 (defun chess-game-undo (game count)
167 "Undo the last COUNT plies of GAME."
168 (if (> count (chess-game-index game))
169 (error "Cannot undo further")
170 (chess-game-set-plies game (nbutlast (chess-game-plies game) count))))
173 (defsubst chess-game-over-p (game)
174 "Return the position related to GAME's INDEX position."
175 (let ((last-ply (car (last game 2))))
176 (and last-ply (chess-ply-final-p last-ply))))
179 (defsubst chess-game-to-string (game &optional indented)
180 (chess-game-to-pgn game indented t))
182 (defsubst chess-game-from-string (pgn)
183 (chess-pgn-to-game pgn))
186 (defun chess-game-create (&optional position tags)
187 "Create a new chess game object.
188 Optionally use the given starting POSITION.
189 TAGS is the starting set of game tags (which can always be changed
190 later using the various tag-related methods)."
191 (let ((game (list nil tags nil
192 (list (chess-ply-create (or position
193 (chess-pos-create)))))))
194 (dolist (tag (cons (cons "Date" (format-time-string "%Y.%m.%d"))
195 chess-game-default-tags))
196 (unless (chess-game-tag game (car tag))
197 (chess-game-set-tag game (car tag) (cdr tag))))
200 (defun chess-game-move (game ply)
201 "Make a move in the current GAME, from FROM to TO.
202 This creates a new position and adds it to the main variation.
203 The 'changes' of the last ply reflect whether the game is currently in
204 progress (nil), if it is drawn, resigned, mate, etc."
205 (let ((current-ply (chess-game-ply game))
206 (changes (chess-ply-changes ply))
207 (position (chess-ply-pos ply)))
208 (if (chess-ply-final-p current-ply)
209 (error "Cannot add moves to a completed game"))
210 (unless (equal position (chess-ply-pos current-ply))
211 (error "Positions do not match"))
212 (chess-ply-set-changes current-ply changes)
213 (chess-game-add-ply game (chess-ply-create
214 (chess-ply-next-pos current-ply)))
216 ((chess-ply-any-keyword ply :draw :perpetual :repetition :stalemate)
217 (chess-game-set-tag game "Result" "1/2-1/2")
218 (chess-game-run-hooks game 'game-drawn))
220 ((chess-ply-any-keyword ply :resign :checkmate)
221 (let ((color (chess-game-side-to-move game)))
222 (chess-game-set-tag game "Result" (if color "0-1" "1-0"))
223 (if (chess-ply-keyword ply :resign)
224 (chess-game-run-hooks game 'resign color)
225 (chess-game-run-hooks game 'move current-ply))))
228 (chess-game-run-hooks game 'move current-ply)))))
230 (defsubst chess-game-end (game keyword)
231 "End the current game, by resignation, draw, etc."
232 (chess-game-move game (list (chess-game-pos game) keyword)))
234 (provide 'chess-game)
236 ;;; chess-game.el ends here