blob: b321bd1b85914fa2671ca73931e627e53b9b7d3a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Maintain a chess game that is being played or viewed
;;
;; $Revision$
;;; Commentary:
;; A chess game is represented by a set of tags that describe the
;; game, and a list of plies representing the main variation.
(require 'chess-ply)
(require 'chess-algebraic)
(require 'chess-fen)
(defvar chess-illegal nil)
(put 'chess-illegal 'error-conditions '(error))
(defconst chess-game-default-tags
`(("Event" . "Computer chess game")
("Round" . "-")
("Site" . ,(system-name))
("White" . "?")
("Black" . "?")
("Result" . "*")
("TimeControl" . "-")))
(defsubst chess-game-tags (game)
"Return the tags alist associated with GAME."
(car game))
(defsubst chess-game-set-tags (game tags)
"Return the tags alist associated with GAME."
(setcar game tags))
(defsubst chess-game-plies (game)
"Return the tags alist associated with GAME."
(cddr game))
(defsubst chess-game-set-plies (game plies)
"Return the tags alist associated with GAME."
(setcdr (cdr game) plies))
(defsubst chess-game-search-function (game)
"Return the tags alist associated with GAME."
(cadr game))
(defsubst chess-game-set-search-function (game func)
"Return the tags alist associated with GAME."
(setcar (cdr game) func))
(defsubst chess-game-tag (game tag)
"Return the value for TAG in GAME."
(let ((tags (chess-game-tags game)))
(and tags (cdr (assoc tag tags)))))
(defun chess-game-set-tag (game tag value)
"Set a TAG for GAME to VALUE."
(let ((tags (chess-game-tags game)))
(if (null tags)
(setcar game (list (cons tag value)))
(let ((entry (assoc tag tags)))
(if entry
(setcdr entry value)
(nconc (car game)
(list (cons tag value))))))))
(defun chess-game-del-tag (game tag)
"Set a TAG for GAME to VALUE."
(setcar game (assq-delete-all tag (chess-game-tags game))))
(defsubst chess-game-index (game)
"Return the GAME's current position index."
(1- (length (chess-game-plies game))))
(defsubst chess-game-seq (game)
"Return the current GAME sequence."
(1+ (/ (chess-game-index game) 2)))
(defsubst chess-game-side-to-move (game)
(chess-pos-side-to-move (chess-game-pos game)))
(defun chess-game-ply (game &optional index)
"Return the position related to GAME's INDEX position."
(if index
(nth index (chess-game-plies game))
(car (last (chess-game-plies game)))))
(defun chess-game-add-ply (game ply)
"Return the position related to GAME's INDEX position."
(let ((plies (chess-game-plies game)))
(if plies
(nconc plies (list ply))
(chess-game-set-plies game (list ply)))))
(defsubst chess-game-pos (game &optional index)
"Return the position related to GAME's INDEX position."
(car (chess-game-ply game index)))
(defun chess-game-create (&optional position search-func tags)
"Create a new chess game object.
Optionally use the given starting POSITION (which is recorded using
the game's FEN tag).
SEARCH-FUNC specifies the function used to test the legality of moves.
TAGS is the starting set of game tags (which can always be changed
later using the various tag-related methods)."
(let ((game (list tags
(or search-func 'chess-standard-search-position))))
(dolist (tag (cons (cons "Date" (format-time-string "%Y.%m.%d"))
chess-game-default-tags))
(unless (chess-game-tag game (car tag))
(chess-game-set-tag game (car tag) (cdr tag))))
(chess-game-add-ply game (chess-ply-create
(or (and position
(chess-pos-copy position))
(chess-pos-create))))
(if position
(chess-game-set-tag game "FEN" (chess-pos-to-fen position)))
game))
(defun chess-game-move (game ply)
"Make a move in the current GAME, from FROM to TO.
This creates a new position and adds it to the main variation.
The 'changes' of the last ply reflect whether the game is currently in
progress (nil), if it is drawn, resigned, mate, etc."
(let ((current-ply (chess-game-ply game))
(changes (chess-ply-changes ply))
(position (chess-ply-pos ply)))
(unless (equal position (chess-ply-pos current-ply))
(error "Positions do not match"))
(unless (funcall (chess-game-search-function game)
position (cadr (chess-ply-changes ply))
(chess-pos-piece position (car (chess-ply-changes ply))))
(signal 'chess-illegal "Illegal move"))
(chess-ply-set-changes current-ply changes)
(cond
((or (memq ':draw changes)
(memq ':perpetual changes)
(memq ':repetition changes)
(memq ':stalemate changes))
(chess-game-set-tag game "Result" "1/2-1/2"))
((or (memq ':resign changes)
(memq ':checkmate changes))
(chess-game-set-tag game "Result" (if (chess-game-side-to-move game)
"0-1" "1-0")))
(t
(chess-game-add-ply game (chess-ply-create
(chess-ply-next-pos current-ply)))))))
;; A few convenience functions
(defsubst chess-game-legal-plies (game)
"Return all legal plies from GAME's current position."
(chess-legal-plies (chess-game-pos game)
(chess-game-search-function game)))
(defsubst chess-game-algebraic-to-ply (game move)
(chess-algebraic-to-ply (chess-game-pos game) move
(chess-game-search-function game)))
(defsubst chess-game-ply-to-algebraic (game &optional ply long)
(chess-ply-to-algebraic (or ply (chess-game-ply game)) long
(chess-game-search-function game)))
(provide 'chess-game)
;;; chess-game.el ends here
|