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.
13 (require 'chess-algebraic)
16 (defvar chess-illegal nil)
17 (put 'chess-illegal 'error-conditions '(error))
19 (defconst chess-game-default-tags
20 `(("Event" . "Computer chess game")
22 ("Site" . ,(system-name))
26 ("TimeControl" . "-")))
28 (defsubst chess-game-tags (game)
29 "Return the tags alist associated with GAME."
32 (defsubst chess-game-set-tags (game tags)
33 "Return the tags alist associated with GAME."
36 (defsubst chess-game-plies (game)
37 "Return the tags alist associated with GAME."
40 (defsubst chess-game-set-plies (game plies)
41 "Return the tags alist associated with GAME."
42 (setcdr (cdr game) plies))
44 (defsubst chess-game-search-function (game)
45 "Return the tags alist associated with GAME."
48 (defsubst chess-game-set-search-function (game func)
49 "Return the tags alist associated with GAME."
50 (setcar (cdr game) func))
52 (defsubst chess-game-tag (game tag)
53 "Return the value for TAG in GAME."
54 (let ((tags (chess-game-tags game)))
55 (and tags (cdr (assoc tag tags)))))
57 (defun chess-game-set-tag (game tag value)
58 "Set a TAG for GAME to VALUE."
59 (let ((tags (chess-game-tags game)))
61 (setcar game (list (cons tag value)))
62 (let ((entry (assoc tag tags)))
66 (list (cons tag value))))))))
68 (defun chess-game-del-tag (game tag)
69 "Set a TAG for GAME to VALUE."
70 (setcar game (assq-delete-all tag (chess-game-tags game))))
72 (defsubst chess-game-index (game)
73 "Return the GAME's current position index."
74 (1- (length (chess-game-plies game))))
76 (defsubst chess-game-seq (game)
77 "Return the current GAME sequence."
78 (1+ (/ (chess-game-index game) 2)))
80 (defsubst chess-game-side-to-move (game)
81 (chess-pos-side-to-move (chess-game-pos game)))
83 (defun chess-game-ply (game &optional index)
84 "Return the position related to GAME's INDEX position."
86 (nth index (chess-game-plies game))
87 (car (last (chess-game-plies game)))))
89 (defun chess-game-add-ply (game ply)
90 "Return the position related to GAME's INDEX position."
91 (let ((plies (chess-game-plies game)))
93 (nconc plies (list ply))
94 (chess-game-set-plies game (list ply)))))
96 (defsubst chess-game-pos (game &optional index)
97 "Return the position related to GAME's INDEX position."
98 (car (chess-game-ply game index)))
100 (defun chess-game-create (&optional position search-func tags)
101 "Create a new chess game object.
102 Optionally use the given starting POSITION (which is recorded using
104 SEARCH-FUNC specifies the function used to test the legality of moves.
105 TAGS is the starting set of game tags (which can always be changed
106 later using the various tag-related methods)."
107 (let ((game (list tags
108 (or search-func 'chess-standard-search-position))))
109 (dolist (tag (cons (cons "Date" (format-time-string "%Y.%m.%d"))
110 chess-game-default-tags))
111 (unless (chess-game-tag game (car tag))
112 (chess-game-set-tag game (car tag) (cdr tag))))
113 (chess-game-add-ply game (chess-ply-create
115 (chess-pos-create))))
117 (chess-game-set-tag game "FEN" (chess-pos-to-fen position)))
120 (defun chess-game-move (game ply)
121 "Make a move in the current GAME, from FROM to TO.
122 This creates a new position and adds it to the main variation.
123 The 'changes' of the last ply reflect whether the game is currently in
124 progress (nil), if it is drawn, resigned, mate, etc."
125 (let ((current-ply (chess-game-ply game))
126 (changes (chess-ply-changes ply))
127 (position (chess-ply-pos ply)))
128 (unless (equal position (chess-ply-pos current-ply))
129 (error "Positions do not match"))
130 (unless (funcall (chess-game-search-function game)
131 position (cadr (chess-ply-changes ply))
132 (chess-pos-piece position (car (chess-ply-changes ply))))
133 (signal 'chess-illegal "Illegal move"))
134 (chess-ply-set-changes current-ply changes)
136 ((or (memq ':draw changes)
137 (memq ':perpetual changes)
138 (memq ':repetition changes)
139 (memq ':stalemate changes))
140 (chess-game-set-tag game "Result" "1/2-1/2"))
141 ((or (memq ':resign changes)
142 (memq ':checkmate changes))
143 (chess-game-set-tag game "Result" (if (chess-game-side-to-move game)
146 (chess-game-add-ply game (chess-ply-create
147 (chess-ply-next-pos current-ply)))))))
149 ;; A few convenience functions
151 (defsubst chess-game-legal-plies (game)
152 "Return all legal plies from GAME's current position."
153 (chess-legal-plies (chess-game-pos game)
154 (chess-game-search-function game)))
156 (defsubst chess-game-algebraic-to-ply (game move)
157 (chess-algebraic-to-ply (chess-game-pos game) move
158 (chess-game-search-function game)))
160 (defsubst chess-game-ply-to-algebraic (game &optional ply long)
161 (chess-ply-to-algebraic (or ply (chess-game-ply game)) long
162 (chess-game-search-function game)))
164 (provide 'chess-game)
166 ;;; chess-game.el ends here