summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2002-04-12 09:07:01 +0000
committerJohn Wiegley <johnw@newartisans.com>2002-04-12 09:07:01 +0000
commitde037adf033425192efefb66f098037766c6932e (patch)
treee8414ef3ac94a986ed93e9497da2f7354662f4e7
parente66b028def9af45cc545c973255df42c9b90cde1 (diff)
*** no comment ***
-rw-r--r--chess-algebraic.el3
-rw-r--r--chess-display.el84
-rw-r--r--chess-irc.el8
-rw-r--r--chess-none.el4
-rw-r--r--chess-ply.el170
-rw-r--r--chess-pos.el6
6 files changed, 142 insertions, 133 deletions
diff --git a/chess-algebraic.el b/chess-algebraic.el
index 3207498..ed6f020 100644
--- a/chess-algebraic.el
+++ b/chess-algebraic.el
@@ -66,8 +66,7 @@ This regexp handles both long and short form.")
(let ((long (= (length (match-string 1 move)) 5)))
(if (chess-pos-can-castle position (if long (if color ?Q ?q)
(if color ?K ?k)))
- (setq ply (chess-ply-create-castle position long)
- changes (chess-ply-changes ply))))
+ (setq changes (chess-ply-create-castle position long))))
(let ((promotion (match-string 8 move)))
(setq changes
(let ((source (match-string 4 move))
diff --git a/chess-display.el b/chess-display.el
index 38d2a5a..464c9df 100644
--- a/chess-display.el
+++ b/chess-display.el
@@ -524,18 +524,19 @@ The key bindings available in this mode are:
(if ply
(setq chess-display-mode-line
(concat
+ " "
(let ((final (chess-ply-final-p ply)))
(cond
((eq final :checkmate)
- " CHECKMATE")
+ "CHECKMATE")
((eq final :resign)
- " RESIGNED")
+ "RESIGNED")
((eq final :stalemate)
- " STALEMATE")
+ "STALEMATE")
((eq final :draw)
- " DRAWN")
+ "DRAWN")
(t
- (concat " " (if color "White" "Black")))))
+ (if color "White" "Black"))))
(if index
(concat " " (int-to-string
(if (> index 1)
@@ -974,38 +975,47 @@ Clicking once on a piece selects it; then click on the target location."
(let ((coord (get-text-property (point) 'chess-coord))
(position (chess-display-position nil)))
(when coord
- (catch 'invalid
- (if chess-display-last-selected
- (let ((last-sel chess-display-last-selected))
- ;; if they select the same square again, just deselect it
- (if (= (point) (car last-sel))
- (chess-display-update nil)
- (let ((s-piece (chess-pos-piece position (cadr last-sel)))
- (t-piece (chess-pos-piece position coord)) ply)
- (when (and (not (eq t-piece ? ))
- (if (chess-pos-side-to-move position)
- (< t-piece ?a)
- (> t-piece ?a)))
- (message "Cannot capture your own pieces.")
- (throw 'invalid t))
- (setq ply (chess-ply-create position (cadr last-sel) coord))
- (unless ply
- (message "That is not a legal move.")
- (throw 'invalid t))
- (chess-display-move nil ply)))
- (setq chess-display-last-selected nil))
- (let ((piece (chess-pos-piece position coord)))
- (cond
- ((eq piece ? )
- (message "Cannot select an empty square.")
- (throw 'invalid t))
- ((if (chess-pos-side-to-move position)
- (> piece ?a)
- (< piece ?a))
- (message "Cannot move your opponent's pieces.")
- (throw 'invalid t)))
- (setq chess-display-last-selected (list (point) coord))
- (chess-display-highlight nil coord 'selected)))))))
+ (condition-case err
+ (if chess-display-last-selected
+ (let ((last-sel chess-display-last-selected))
+ ;; if they select the same square again, just deselect it
+ (if (= (point) (car last-sel))
+ (throw 'invalid t)
+ (let ((s-piece (chess-pos-piece position (cadr last-sel)))
+ (t-piece (chess-pos-piece position coord)) ply)
+ (if (and (/= t-piece ? )
+ (or (and (< t-piece ?a)
+ (< s-piece ?a))
+ (and (> t-piece ?a)
+ (> s-piece ?a))))
+ (error "You cannot move pieces on top of each other"))
+ (unless (setq ply (chess-ply-create position
+ (cadr last-sel) coord))
+ (error "That is not a legal move"))
+ (chess-display-move nil ply)))
+ (setq chess-display-last-selected nil))
+ (let ((piece (chess-pos-piece position coord)))
+ (cond
+ ((and (chess-display-active-p)
+ ;; `active' means we're playing somebody via an
+ ;; engine
+ (chess-game-data chess-display-game 'active)
+ (not (eq (chess-game-data chess-display-game
+ 'my-color)
+ (chess-pos-side-to-move position))))
+ (error "It is not your turn to move"))
+ ((eq piece ? )
+ (error "You cannot select an empty square"))
+ ((if (chess-pos-side-to-move position)
+ (> piece ?a)
+ (< piece ?a))
+ (error "You cannot move your opponent's pieces")))
+ (setq chess-display-last-selected (list (point) coord))
+ (chess-display-highlight nil coord 'selected)))
+ (error
+ (setq chess-display-last-selected nil)
+ (chess-display-update nil)
+ (message (error-message-string err)))))))
(defun chess-display-mouse-select-piece (event)
"Select the piece the user clicked on."
diff --git a/chess-irc.el b/chess-irc.el
index 1588b22..9cb39bb 100644
--- a/chess-irc.el
+++ b/chess-irc.el
@@ -10,13 +10,13 @@
"Use an IRC bot for sending/receiving moves."
:group 'chess-engine)
-(defcustom chess-irc-host "irc.openprojects.net"
+(defcustom chess-irc-server "irc.openprojects.net"
"The IRC host to connect your chess-irc engine to."
:type 'string
:group 'chess-irc)
(defcustom chess-irc-port 6667
- "The port of the IRC host specified by `chess-irc-host'."
+ "The port of the IRC host specified by `chess-irc-server'."
:type 'string
:group 'chess-irc)
@@ -50,12 +50,12 @@
(cond
((eq event 'initialize)
(message "Connecting to IRC server '%s:%d'..."
- chess-irc-host chess-irc-port)
+ chess-irc-server chess-irc-port)
(let ((engine (current-buffer)) proc)
(with-current-buffer (generate-new-buffer " *chess-irc*")
(setq chess-irc-engine engine
proc (open-network-stream "*chess-irc*" (current-buffer)
- chess-irc-host chess-irc-port))
+ chess-irc-server chess-irc-port))
(message "Connected, now logging in as '%s'..." chess-irc-nick)
(when (and proc (eq (process-status proc) 'open))
(process-send-string proc (format "USER %s 0 * :%s\n"
diff --git a/chess-none.el b/chess-none.el
index d916adf..db628bf 100644
--- a/chess-none.el
+++ b/chess-none.el
@@ -11,10 +11,6 @@
(cond
((eq event 'send))
- ((eq event 'ready)
- (and (chess-engine-game nil)
- (chess-game-set-data (chess-engine-game nil) 'active t)))
-
((memq event '(resign abort))
(and (chess-engine-game nil)
(chess-engine-set-start-position nil)))
diff --git a/chess-ply.el b/chess-ply.el
index c14f44a..845ff00 100644
--- a/chess-ply.el
+++ b/chess-ply.el
@@ -117,91 +117,91 @@ maneuver."
(delq :valid changes)))
(color (chess-pos-side-to-move position))
piece)
-
- ;; validate that `changes' can be legally applied to the given
- ;; position
- (when (and changes (not (symbolp (car changes)))
- (or valid-p
- (member (car changes)
- (chess-search-position
- position (cadr changes)
- (chess-pos-piece position (car changes))))))
- (setq piece (chess-pos-piece position (car changes)))
-
- ;; is this a castling maneuver?
- (if (and (= piece (if color ?K ?k))
- (not (or (memq :castle changes)
- (memq :long-castle changes))))
- (let* ((target (cadr changes))
- (file (chess-index-file target))
- (long (= 2 file))
- new-changes)
- (if (and (or (and (= file 6)
- (chess-pos-can-castle position
- (if color ?K ?k)))
- (and long
- (chess-pos-can-castle position
- (if color ?Q ?q))))
- (setq new-changes
- (chess-ply-create-castle position long)))
- (setcdr ply new-changes))))
-
- ;; is this a pawn move to the ultimate rank? if so, and we
- ;; haven't already been told, ask for the piece to promote it to
- (if (and (= piece (if color ?P ?p))
- (not (memq :promote changes))
- (= (if color 0 7) (chess-index-rank (cadr changes))))
- (let ((new-piece (completing-read
- "Promote pawn to queen/rook/knight/bishop? "
- chess-piece-name-table nil t "queen")))
- (setq new-piece
- (cdr (assoc new-piece chess-piece-name-table)))
- (if color
- (setq new-piece (upcase new-piece)))
- (nconc changes (list :promote new-piece))))
-
- (unless (or (memq :check changes)
- (memq :checkmate changes)
- (memq :stalemate changes))
- (let* ((next-pos (chess-ply-next-pos ply))
- (next-color (not color)))
- ;; is the opponent's king in check/mate or stalemate now, as
- ;; a result of the changes?
- (let ((can-move
- (catch 'can-move
- ;; find out if any of `color's pieces can move. We
- ;; start the search on the home row for that color,
- ;; as it's likier to find a legal move faster.
- (let ((rank (if next-color 7 0))
- (file 0))
- (while (funcall (if next-color '>= '<) rank
- (if next-color 0 8))
- (while (< file 8)
- (let* ((to (chess-rf-to-index rank file))
- (piece (chess-pos-piece next-pos to)))
- (when (or (eq piece ? )
- (if next-color
- (> piece ?a)
- (< piece ?a)))
- (if (chess-search-position next-pos to next-color)
- (throw 'can-move t))))
- (setq file (1+ file)))
- (setq file 0 rank (funcall (if next-color '1- '1+)
- rank)))))))
-
- ;; see if anyone from the other side is attacking the king
- ;; in the new position
- (if (chess-search-position next-pos
- (car (chess-pos-search
- next-pos (if next-color ?K ?k)))
- (not next-color))
- (nconc changes (list (if can-move :check :checkmate)))
- ;; no, but is he in stalemate?
- (unless can-move
- (nconc changes (list :stalemate))))))))
-
- ;; return the annotated ply
- ply))
+ (if (or (null changes) (symbolp (car changes)))
+ ply
+ ;; validate that `changes' can be legally applied to the given
+ ;; position
+ (when (or valid-p
+ (member (car changes)
+ (chess-search-position position (cadr changes)
+ (chess-pos-piece position
+ (car changes)))))
+ (setq piece (chess-pos-piece position (car changes)))
+
+ ;; is this a castling maneuver?
+ (if (and (= piece (if color ?K ?k))
+ (not (or (memq :castle changes)
+ (memq :long-castle changes))))
+ (let* ((target (cadr changes))
+ (file (chess-index-file target))
+ (long (= 2 file))
+ new-changes)
+ (if (and (or (and (= file 6)
+ (chess-pos-can-castle position
+ (if color ?K ?k)))
+ (and long
+ (chess-pos-can-castle position
+ (if color ?Q ?q))))
+ (setq new-changes
+ (chess-ply-create-castle position long)))
+ (setcdr ply new-changes))))
+
+ ;; is this a pawn move to the ultimate rank? if so, and we
+ ;; haven't already been told, ask for the piece to promote it to
+ (if (and (= piece (if color ?P ?p))
+ (not (memq :promote changes))
+ (= (if color 0 7) (chess-index-rank (cadr changes))))
+ (let ((new-piece (completing-read
+ "Promote pawn to queen/rook/knight/bishop? "
+ chess-piece-name-table nil t "queen")))
+ (setq new-piece
+ (cdr (assoc new-piece chess-piece-name-table)))
+ (if color
+ (setq new-piece (upcase new-piece)))
+ (nconc changes (list :promote new-piece))))
+
+ (unless (or (memq :check changes)
+ (memq :checkmate changes)
+ (memq :stalemate changes))
+ (let* ((next-pos (chess-ply-next-pos ply))
+ (next-color (not color)))
+ ;; is the opponent's king in check/mate or stalemate now, as
+ ;; a result of the changes?
+ (let ((can-move
+ (catch 'can-move
+ ;; find out if any of `color's pieces can move. We
+ ;; start the search on the home row for that color,
+ ;; as it's likier to find a legal move faster.
+ (let ((rank (if next-color 7 0))
+ (file 0))
+ (while (funcall (if next-color '>= '<) rank
+ (if next-color 0 8))
+ (while (< file 8)
+ (let* ((to (chess-rf-to-index rank file))
+ (piece (chess-pos-piece next-pos to)))
+ (when (or (eq piece ? )
+ (if next-color
+ (> piece ?a)
+ (< piece ?a)))
+ (if (chess-search-position next-pos to next-color)
+ (throw 'can-move t))))
+ (setq file (1+ file)))
+ (setq file 0 rank (funcall (if next-color '1- '1+)
+ rank)))))))
+
+ ;; see if anyone from the other side is attacking the king
+ ;; in the new position
+ (if (chess-search-position next-pos
+ (car (chess-pos-search
+ next-pos (if next-color ?K ?k)))
+ (not next-color))
+ (nconc changes (list (if can-move :check :checkmate)))
+ ;; no, but is he in stalemate?
+ (unless can-move
+ (nconc changes (list :stalemate)))))))
+
+ ;; return the annotated ply
+ ply))))
(defsubst chess-ply-final-p (ply)
"Return non-nil if this is the last ply of a game/variation."
diff --git a/chess-pos.el b/chess-pos.el
index 6c89e15..4390072 100644
--- a/chess-pos.el
+++ b/chess-pos.el
@@ -263,7 +263,8 @@ trying to move a blank square."
(to (cadr ch))
(piece (chess-pos-piece position from)))
(if (= piece ? )
- (error "Attempted piece move from blank square %s" from))
+ (error "Attempted piece move from blank square %s"
+ (chess-index-to-coord from)))
(chess-pos-set-piece position from ? )
(chess-pos-set-piece position to piece))
(setq ch (cddr ch)))))
@@ -351,6 +352,9 @@ indices which indicate where a piece may have moved from."
(if piece p (downcase p)))))
(setq candidates (cdr candidates)))
+ ;; skip erroneous space requests
+ ((= test-piece ? ))
+
;; pawn movement, which is diagonal 1 when taking, but forward
;; 1 or 2 when moving (the most complex piece, actually)
((= test-piece ?P)