]> code.delx.au - gnu-emacs-elpa/blob - chess-game.el
Gnuchess can be played against (up until a pawn take occurs).
[gnu-emacs-elpa] / chess-game.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Maintain a chess game that is being played or viewed
4 ;;
5 ;; $Revision$
6
7 ;;; Commentary:
8
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.
11
12 (require 'chess-ply)
13 (require 'chess-algebraic)
14 (require 'chess-fen)
15
16 (defvar chess-illegal nil)
17 (put 'chess-illegal 'error-conditions '(error))
18
19 (defconst chess-game-default-tags
20 `(("Event" . "Computer chess game")
21 ("Round" . "-")
22 ("Site" . ,(system-name))
23 ("White" . "?")
24 ("Black" . "?")
25 ("Result" . "*")
26 ("TimeControl" . "-")))
27
28 (defsubst chess-game-tags (game)
29 "Return the tags alist associated with GAME."
30 (car game))
31
32 (defsubst chess-game-set-tags (game tags)
33 "Return the tags alist associated with GAME."
34 (setcar game tags))
35
36 (defsubst chess-game-plies (game)
37 "Return the tags alist associated with GAME."
38 (cddr game))
39
40 (defsubst chess-game-set-plies (game plies)
41 "Return the tags alist associated with GAME."
42 (setcdr (cdr game) plies))
43
44 (defsubst chess-game-search-function (game)
45 "Return the tags alist associated with GAME."
46 (cadr game))
47
48 (defsubst chess-game-set-search-function (game func)
49 "Return the tags alist associated with GAME."
50 (setcar (cdr game) func))
51
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)))))
56
57 (defun chess-game-set-tag (game tag value)
58 "Set a TAG for GAME to VALUE."
59 (let ((tags (chess-game-tags game)))
60 (if (null tags)
61 (setcar game (list (cons tag value)))
62 (let ((entry (assoc tag tags)))
63 (if entry
64 (setcdr entry value)
65 (nconc (car game)
66 (list (cons tag value))))))))
67
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))))
71
72 (defsubst chess-game-index (game)
73 "Return the GAME's current position index."
74 (1- (length (chess-game-plies game))))
75
76 (defsubst chess-game-seq (game)
77 "Return the current GAME sequence."
78 (1+ (/ (chess-game-index game) 2)))
79
80 (defsubst chess-game-side-to-move (game)
81 (chess-pos-side-to-move (chess-game-pos game)))
82
83 (defun chess-game-ply (game &optional index)
84 "Return the position related to GAME's INDEX position."
85 (if index
86 (nth index (chess-game-plies game))
87 (car (last (chess-game-plies game)))))
88
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)))
92 (if plies
93 (nconc plies (list ply))
94 (chess-game-set-plies game (list ply)))))
95
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)))
99
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
103 the game's FEN tag).
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
114 (or position
115 (chess-pos-create))))
116 (if position
117 (chess-game-set-tag game "FEN" (chess-pos-to-fen position)))
118 game))
119
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)
135 (cond
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)
144 "0-1" "1-0")))
145 (t
146 (chess-game-add-ply game (chess-ply-create
147 (chess-ply-next-pos current-ply)))))))
148
149 ;; A few convenience functions
150
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)))
155
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)))
159
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)))
163
164 (provide 'chess-game)
165
166 ;;; chess-game.el ends here