diff options
| -rw-r--r-- | TODO | 3 | ||||
| -rw-r--r-- | chess-algebraic.el | 38 | ||||
| -rw-r--r-- | chess-announce.el | 29 | ||||
| -rw-r--r-- | chess-common.el | 12 | ||||
| -rw-r--r-- | chess-game.el | 6 | ||||
| -rw-r--r-- | chess-ply.el | 19 | ||||
| -rw-r--r-- | chess-sound.el | 59 |
7 files changed, 99 insertions, 67 deletions
@@ -9,6 +9,9 @@ ---------------------------------------------------------------------- +- undo is not working with gnuchess + + - test making an en passant capture diff --git a/chess-algebraic.el b/chess-algebraic.el index a1bc1e6..0960a35 100644 --- a/chess-algebraic.el +++ b/chess-algebraic.el @@ -90,6 +90,7 @@ This regexp handles both long and short form.") (list (car candidates) target) (if (null source) (error "Clarify piece to move by rank or file") + (nconc changes (list :which source)) (while candidates (if (if (>= source ?a) (eq (chess-index-file (car candidates)) @@ -120,30 +121,31 @@ If LONG is non-nil, render the move into long notation." (if (let ((source (chess-ply-source ply))) (or (null source) (symbolp source))) "" - (or (and (chess-ply-has-keyword ply :castle) "O-O") - (and (chess-ply-has-keyword ply :long-castle) "O-O-O") + (or (and (chess-ply-keyword ply :castle) "O-O") + (and (chess-ply-keyword ply :long-castle) "O-O-O") (let* ((pos (chess-ply-pos ply)) (from (chess-ply-source ply)) (to (chess-ply-target ply)) (from-piece (chess-pos-piece pos from)) (color (chess-pos-side-to-move pos)) - (candidates (chess-search-position pos to from-piece)) (rank 0) (file 0) (from-rank (/ from 8)) (from-file (mod from 8)) - differentiator) - (when (> (length candidates) 1) - (dolist (candidate candidates) - (if (= (/ candidate 8) from-rank) - (setq rank (1+ rank))) - (if (= (mod candidate 8) from-file) - (setq file (1+ file)))) - (cond - ((= file 1) - (setq differentiator (+ from-file ?a))) - ((= rank 1) - (setq differentiator (+ (- 7 from-rank) ?1))) - (t (error "Could not differentiate piece")))) + (differentiator (cdr (memq :which (chess-ply-changes ply))))) + (unless differentiator + (let ((candidates (chess-search-position pos to from-piece))) + (when (> (length candidates) 1) + (dolist (candidate candidates) + (if (= (/ candidate 8) from-rank) + (setq rank (1+ rank))) + (if (= (mod candidate 8) from-file) + (setq file (1+ file)))) + (cond + ((= file 1) + (setq differentiator (+ from-file ?a))) + ((= rank 1) + (setq differentiator (+ (- 7 from-rank) ?1))) + (t (error "Could not differentiate piece")))))) (concat (unless (= (upcase from-piece) ?P) (char-to-string (upcase from-piece))) @@ -162,8 +164,8 @@ If LONG is non-nil, render the move into long notation." (if promote (concat "=" (char-to-string (upcase (cadr promote)))))) - (if (chess-ply-has-keyword ply :check) "+" - (if (chess-ply-has-keyword ply :checkmate) "#"))))))) + (if (chess-ply-keyword ply :check) "+" + (if (chess-ply-keyword ply :checkmate) "#"))))))) (provide 'chess-algebraic) diff --git a/chess-announce.el b/chess-announce.el index a713057..f4c8f11 100644 --- a/chess-announce.el +++ b/chess-announce.el @@ -52,27 +52,36 @@ See `chess-display-type' for the different kinds of displays." (target (chess-ply-target ply)) (s-piece (chess-pos-piece pos source)) (t-piece (chess-pos-piece pos target)) + (which (chess-ply-keyword ply :which)) text) + (if which + (setq which (char-to-string which))) (cond - ((chess-ply-has-keyword ply :castle) - (setq text "kingside castle")) - ((chess-ply-has-keyword :long-castle) - (setq text "queenside castle")) + ((chess-ply-keyword ply :castle) + (setq text "short castle")) + ((chess-ply-keyword ply :long-castle) + (setq text "long castle")) ((= t-piece ? ) - (setq text (concat (cdr (assq (downcase s-piece) + (setq text (concat which + (cdr (assq (downcase s-piece) chess-announce-names)) " to " (chess-index-to-coord target)))) (t - (setq text (concat (cdr (assq (downcase s-piece) + (setq text (concat which + (cdr (assq (downcase s-piece) chess-announce-names)) - " takes at " + " takes " + (cdr (assq (downcase t-piece) + chess-announce-names)) + " at " (chess-index-to-coord target))))) - (if (chess-ply-has-keyword :check) + + (if (chess-ply-keyword ply :check) (setq text (concat text ", check"))) - (if (chess-ply-has-keyword :checkmate) + (if (chess-ply-keyword ply :checkmate) (setq text (concat text ", checkmate"))) - (if (chess-ply-has-keyword :stalemate) + (if (chess-ply-keyword ply :stalemate) (setq text (concat text ", stalemate"))) (funcall (nth 1 chess-announce-functions) text))))))) diff --git a/chess-common.el b/chess-common.el index 7019bb9..7161e73 100644 --- a/chess-common.el +++ b/chess-common.el @@ -13,11 +13,11 @@ (make-variable-buffer-local 'chess-common-temp-files) (defmacro chess-with-temp-file (&rest body) - (let ((file (make-temp-file "chess"))) - (with-temp-file file - ,@body) - (push file chess-common-temp-files) - file)) + `(let ((file (make-temp-file "chess"))) + (with-temp-file file + ,@body) + (push file chess-common-temp-files) + file)) (put 'chess-with-temp-file 'lisp-indent-function 1) @@ -65,6 +65,8 @@ (when (chess-engine-game nil) (dotimes (i (car args)) (chess-engine-send nil "undo\n")) + (if (= 1 (mod (car args) 2)) + (chess-engine-send nil "go\n")) (chess-game-undo (chess-engine-game nil) (car args)))) ((eq event 'move) diff --git a/chess-game.el b/chess-game.el index 138fc36..cae650f 100644 --- a/chess-game.el +++ b/chess-game.el @@ -213,14 +213,14 @@ progress (nil), if it is drawn, resigned, mate, etc." (chess-game-add-ply game (chess-ply-create (chess-ply-next-pos current-ply))) (cond - ((chess-ply-has-keyword ply :draw :perpetual :repetition :stalemate) + ((chess-ply-any-keyword ply :draw :perpetual :repetition :stalemate) (chess-game-set-tag game "Result" "1/2-1/2") (chess-game-run-hooks game 'game-drawn)) - ((chess-ply-has-keyword ply :resign :checkmate) + ((chess-ply-any-keyword ply :resign :checkmate) (let ((color (chess-game-side-to-move game))) (chess-game-set-tag game "Result" (if color "0-1" "1-0")) - (if (chess-ply-has-keyword ply :resign) + (if (chess-ply-keyword ply :resign) (chess-game-run-hooks game 'resign color) (chess-game-run-hooks game 'move current-ply)))) diff --git a/chess-ply.el b/chess-ply.el index ea6e0ac..80894cb 100644 --- a/chess-ply.el +++ b/chess-ply.el @@ -63,17 +63,28 @@ (defsubst chess-ply-set-changes (ply changes) (setcdr ply changes)) -(defun chess-ply-has-keyword (ply &rest keywords) +(defun chess-ply-any-keyword (ply &rest keywords) (catch 'found (dolist (keyword keywords) (if (memq keyword (chess-ply-changes ply)) (throw 'found keyword))))) +(defun chess-ply-keyword (ply keyword) + (let ((item (memq keyword (chess-ply-changes ply)))) + (if item + (if (memq keyword '(:which :promote)) + (cdr item) + t)))) + (defsubst chess-ply-source (ply) - (car (chess-ply-changes ply))) + (let ((changes (chess-ply-changes ply))) + (and (listp changes) (not (symbolp (car changes))) + (car changes)))) (defsubst chess-ply-target (ply) - (cadr (chess-ply-changes ply))) + (let ((changes (chess-ply-changes ply))) + (and (listp changes) (not (symbolp (car changes))) + (cadr changes)))) (defsubst chess-ply-next-pos (ply) (apply 'chess-pos-move (chess-pos-copy (chess-ply-pos ply)) @@ -206,7 +217,7 @@ maneuver." (defsubst chess-ply-final-p (ply) "Return non-nil if this is the last ply of a game/variation." - (chess-ply-has-keyword ply :draw :perpetual :repetition :stalemate + (chess-ply-any-keyword ply :draw :perpetual :repetition :stalemate :resign :checkmate)) (defun chess-legal-plies (position) diff --git a/chess-sound.el b/chess-sound.el index 61cfa83..7f2f4db 100644 --- a/chess-sound.el +++ b/chess-sound.el @@ -23,7 +23,7 @@ 'play-sound-file 'chess-sound-play) "Non-nil if chess-sound should play sounds ." - :type 'file + :type 'function :group 'chess-sound) (defcustom chess-sound-program (or (executable-find "esdplay") @@ -37,9 +37,14 @@ :type '(repeat string) :group 'chess-sound) +(defcustom chess-sound-my-moves nil + "If non-nil, plays the move.wav sound whenever you make a move." + :type 'boolean + :group 'chess-sound) + (defun chess-sound-available-p () (and (file-directory-p chess-sound-directory) - (file-readable-p (expand-file-name "tap.wav" + (file-readable-p (expand-file-name "move.wav" chess-sound-directory)) (or (eq chess-sound-play-function 'play-sound-file) (file-executable-p chess-sound-program)))) @@ -48,17 +53,10 @@ "Announce the opponent's moves in GAME." (chess-game-add-hook game 'chess-sound-handler)) -(defun chess-sound (ch) - (let ((file - (cond - ((stringp ch) - (format "%s.wav" ch)) - ((memq ch '(?\# ?\+ ?k ?q ?b ?n ?r ?p ?x)) - (format "%c_.wav" ch)) - (t - (format "%s.wav" (chess-index-to-coord ch)))))) - (funcall chess-sound-play-function - (expand-file-name file chess-sound-directory)))) +(defsubst chess-sound (file) + (funcall chess-sound-play-function + (expand-file-name (concat file ".wav") + chess-sound-directory))) (defun chess-sound-play (file) (apply 'call-process chess-sound-program @@ -72,30 +70,37 @@ See `chess-display-type' for the different kinds of displays." (pos (chess-ply-pos ply))) (if (eq (chess-game-data game 'my-color) (chess-pos-side-to-move pos)) - (chess-sound "tap") + (if chess-sound-my-moves + (chess-sound "move")) (let* ((source (chess-ply-source ply)) (target (chess-ply-target ply)) (s-piece (chess-pos-piece pos source)) (t-piece (chess-pos-piece pos target)) + (which (chess-ply-keyword ply :which)) text) (cond - ((chess-ply-has-keyword :castle) + ((chess-ply-keyword ply :castle) (chess-sound "O-O")) - ((chess-ply-has-keyword :long-castle) + ((chess-ply-keyword ply :long-castle) (chess-sound "O-O-O")) ((= t-piece ? ) - (chess-sound (downcase s-piece)) - (chess-sound target)) + (if which + (chess-sound (char-to-string which))) + (chess-sound (format "%c_" (downcase s-piece))) + (chess-sound (chess-index-to-coord target))) (t - (chess-sound (downcase s-piece)) - (chess-sound ?x) - (chess-sound (downcase t-piece)) - (chess-sound target))) - (if (chess-ply-has-keyword :check) - (chess-sound ?+)) - (if (chess-ply-has-keyword :checkmate) - (chess-sound ?#)) - (if (chess-ply-has-keyword :stalemate) + (if which + (chess-sound (char-to-string which))) + (chess-sound (format "%c_" (downcase s-piece))) + (chess-sound "x_") + (chess-sound (format "%c_" (downcase t-piece))) + (chess-sound (chess-index-to-coord target)))) + + (if (chess-ply-keyword ply :check) + (chess-sound "+_")) + (if (chess-ply-keyword ply :checkmate) + (chess-sound "#_")) + (if (chess-ply-keyword ply :stalemate) (chess-sound "smate"))))))) (provide 'chess-sound) |
