diff options
| author | John Wiegley <johnw@newartisans.com> | 2002-04-13 08:27:24 +0000 |
|---|---|---|
| committer | John Wiegley <johnw@newartisans.com> | 2002-04-13 08:27:24 +0000 |
| commit | 9b3364910b621d1ddbd350300c8da153fdaa2a6e (patch) | |
| tree | 0f823feddd5fe57f72c7eeab1085c1c661714b0a | |
| parent | acd0fdb7106a8b49f27a74987d28e22315bbc471 (diff) | |
displays and engines now always have a single object associated with
them
| -rw-r--r-- | chess-common.el | 17 | ||||
| -rw-r--r-- | chess-display.el | 10 | ||||
| -rw-r--r-- | chess-engine.el | 448 | ||||
| -rw-r--r-- | chess-ics.el | 30 | ||||
| -rw-r--r-- | chess-link.el | 23 | ||||
| -rw-r--r-- | chess-network.el | 10 | ||||
| -rw-r--r-- | chess-none.el | 13 | ||||
| -rw-r--r-- | chess.el | 40 |
8 files changed, 261 insertions, 330 deletions
diff --git a/chess-common.el b/chess-common.el index 7161e73..9e05c96 100644 --- a/chess-common.el +++ b/chess-common.el @@ -37,8 +37,7 @@ proc)) ((eq event 'ready) - (and (chess-engine-game nil) - (chess-game-set-data (chess-engine-game nil) 'active t))) + (chess-game-set-data chess-engine-game 'active t)) ((eq event 'shutdown) (chess-engine-send nil "quit\n") @@ -58,16 +57,14 @@ ((memq event '(resign abort)) (chess-engine-send nil "new\n") - (and (chess-engine-game nil) - (chess-engine-set-start-position nil))) + (chess-engine-set-position nil)) ((eq event 'undo) - (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)))) + (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 (car args))) ((eq event 'move) (chess-engine-send nil (concat (chess-ply-to-algebraic (car args)) diff --git a/chess-display.el b/chess-display.el index 931acbb..5fd3fc4 100644 --- a/chess-display.el +++ b/chess-display.el @@ -138,10 +138,8 @@ makes moves, or any other changes to the underlying game." (defun chess-display-set-ply (display ply) (chess-with-current-buffer display - (setq chess-game-index 1) - (chess-game-set-plies chess-display-game - (list ply (chess-ply-create - (chess-ply-next-pos ply)))))) + ;; jww (2002-04-13): NYI + )) (defun chess-display-ply (display) (chess-with-current-buffer display @@ -154,8 +152,8 @@ the user able to scroll back and forth through the moves in the variation. Any moves made on the board will extend/change the variation that was passed in." (chess-with-current-buffer display - (setq chess-game-index (or index (chess-var-index variation))) - (chess-game-set-plies chess-display-game variation))) + ;; jww (2002-04-13): NYI + )) (defun chess-display-variation (display) (chess-with-current-buffer display diff --git a/chess-engine.el b/chess-engine.el index f90c504..a207c34 100644 --- a/chess-engine.el +++ b/chess-engine.el @@ -18,7 +18,6 @@ (defvar chess-engine-event-handler nil) (defvar chess-engine-response-handler nil) (defvar chess-engine-current-marker nil) -(defvar chess-engine-position nil) (defvar chess-engine-game nil) (defvar chess-engine-pending-offer nil) (defvar chess-engine-pending-arg nil) @@ -27,7 +26,6 @@ (make-variable-buffer-local 'chess-engine-event-handler) (make-variable-buffer-local 'chess-engine-response-handler) (make-variable-buffer-local 'chess-engine-current-marker) -(make-variable-buffer-local 'chess-engine-position) (make-variable-buffer-local 'chess-engine-game) (make-variable-buffer-local 'chess-engine-pending-offer) (make-variable-buffer-local 'chess-engine-pending-arg) @@ -57,13 +55,6 @@ ,@body) ,@body))) -(defun chess-engine-do-move (ply) - (cond - (chess-engine-game - (chess-game-move chess-engine-game ply)) - (chess-engine-position - (setq chess-engine-position (chess-ply-next-pos ply))))) - (defsubst chess-engine-convert-algebraic (move &optional trust-check) (or (chess-algebraic-to-ply (chess-engine-position nil) move trust-check) (ignore @@ -80,201 +71,183 @@ (message "Received invalid PGN text")))) (defun chess-engine-default-handler (event &rest args) - (let ((game (chess-engine-game nil))) - (cond - ((eq event 'move) - (if (null game) + (cond + ((eq event 'move) + (if (chess-game-data chess-engine-game 'active) + ;; we don't want the `move' event coming back to us + (let ((chess-engine-handling-event t)) (when (car args) - (setq chess-engine-position (chess-ply-next-pos (car args))) - t) - (if (chess-game-data game 'active) - ;; we don't want the `move' event coming back to us + ;; if the game index is still 0, then our opponent + ;; is white, and we need to pass over the move + (when (and (not chess-engine-inhibit-auto-pass) + (chess-game-data chess-engine-game 'my-color) + (= (chess-game-index chess-engine-game) 0)) + (message "Your opponent played the first move, you are now black") + (chess-game-run-hooks chess-engine-game 'pass) + ;; if no one else flipped my-color, we'll do it + (if (chess-game-data chess-engine-game 'my-color) + (chess-game-set-data chess-engine-game 'my-color nil))) + (chess-game-move chess-engine-game (car args)) + t)))) + + ((eq event 'pass) + (when (chess-game-data chess-engine-game 'active) + (message "Your opponent has passed the move to you") + t)) + + ((eq event 'match) + (if (chess-game-data chess-engine-game 'active) + (chess-engine-command nil 'busy) + (if (y-or-n-p + (if (and (car args) (> (length (car args)) 0)) + (format "Do you wish to play a chess game against %s? " + (car args)) + (format "Do you wish to play a chess game against an anonymous opponent? "))) + (progn (let ((chess-engine-handling-event t)) - (when (car args) - ;; if the game index is still 0, then our opponent - ;; is white, and we need to pass over the move - (when (and game - (not chess-engine-inhibit-auto-pass) - (chess-game-data game 'my-color) - (= (chess-game-index game) 0)) - (message "Your opponent played the first move, you are now black") - (chess-game-run-hooks game 'pass) - ;; if no one else flipped my-color, we'll do it - (if (chess-game-data game 'my-color) - (chess-game-set-data game 'my-color nil))) - (chess-engine-do-move (car args)) - t))))) - - ((eq event 'pass) - (when (and game (chess-game-data game 'active)) - (message "Your opponent has passed the move to you") - t)) - - ((eq event 'match) - (if (and game (chess-game-data game 'active)) - (chess-engine-command nil 'busy) - (if (y-or-n-p - (if (and (car args) (> (length (car args)) 0)) - (format "Do you wish to play a chess game against %s? " - (car args)) - (format "Do you wish to play a chess game against an anonymous opponent? "))) - (progn - (let ((chess-engine-handling-event t)) - (unless game - (setq game (chess-engine-set-game nil (chess-game-create)))) - (chess-engine-set-start-position nil)) - (chess-engine-command nil 'accept)) - (chess-engine-command nil 'decline))) - t) - - ((eq event 'setup-pos) - (when (car args) - ;; we don't want the `setup-game' event coming back to us - (let ((chess-engine-handling-event t)) - (chess-engine-set-start-position nil (car args) t)) - t)) - - ((eq event 'setup-game) - (when (car args) - ;; we don't want the `setup-game' event coming back to us - (let ((chess-engine-handling-event t)) - (if (null game) - (chess-engine-set-game nil (car args)) - (let ((chess-game-inhibit-events t)) - (chess-engine-copy-game nil (car args)) - (chess-game-set-data game 'active t) - (if (string= chess-full-name (chess-game-tag game "White")) - (chess-game-set-data game 'my-color t) - (chess-game-set-data game 'my-color nil))) - (chess-game-run-hooks game 'orient))) - t)) - - ((eq event 'quit) - (message "Your opponent has quit playing") - (if game + (chess-engine-set-position nil)) + (chess-engine-command nil 'accept)) + (chess-engine-command nil 'decline))) + t) + + ((eq event 'setup-pos) + (when (car args) + ;; we don't want the `setup-game' event coming back to us + (let ((chess-engine-handling-event t)) + (chess-engine-set-position nil (car args) t)) + t)) + + ((eq event 'setup-game) + (when (car args) + ;; we don't want the `setup-game' event coming back to us + (let ((chess-engine-handling-event t)) + (let ((chess-game-inhibit-events t)) + (chess-engine-set-game nil (car args)) + (chess-game-set-data chess-engine-game 'active t) + (if (string= chess-full-name + (chess-game-tag chess-engine-game "White")) + (chess-game-set-data chess-engine-game 'my-color t) + (chess-game-set-data chess-engine-game 'my-color nil)))) + t)) + + ((eq event 'quit) + (message "Your opponent has quit playing") + (let ((chess-engine-handling-event t)) + (chess-game-set-data chess-engine-game 'active nil)) + t) + + ((eq event 'resign) + (let ((chess-engine-handling-event t)) + (message "Your opponent has resigned") + (chess-game-end chess-engine-game :resign) + (chess-game-set-data chess-engine-game 'active nil) + t)) + + ((eq event 'draw) + (if (y-or-n-p "Your opponent offers a draw, accept? ") + (progn (let ((chess-engine-handling-event t)) - (chess-game-set-data game 'active nil))) - t) - - ((eq event 'resign) - (when game - (let ((chess-engine-handling-event t)) - (message "Your opponent has resigned") - (chess-game-end game :resign) - (chess-game-set-data game 'active nil)) - t)) - - ((eq event 'draw) - (when game - (if (y-or-n-p "Your opponent offers a draw, accept? ") - (progn - (let ((chess-engine-handling-event t)) - (chess-game-end game :draw) - (chess-game-set-data game 'active nil)) - (chess-engine-command nil 'accept)) - (chess-engine-command nil 'decline)) - t)) - - ((eq event 'abort) - (when game - (if (y-or-n-p "Your opponent wants to abort this game, accept? ") - (progn - (let ((chess-engine-handling-event t)) - (chess-game-set-data game 'active nil)) - (chess-engine-command nil 'accept)) - (chess-engine-command nil 'decline)) - t)) - - ((eq event 'undo) - (when game - (if (y-or-n-p - (format "Your opponent wants to take back %d moves, accept? " - (car args))) - (progn - (let ((chess-engine-handling-event t)) - (chess-game-undo game (car args))) - (chess-engine-command nil 'accept)) - (chess-engine-command nil 'decline)) - t)) - - ((eq event 'accept) - (when chess-engine-pending-offer - (if (eq chess-engine-pending-offer 'match) - (unless (and game (chess-game-data game 'active)) - (if (and (car args) (> (length (car args)) 0)) - (message "Your opponent, %s, is now ready to play" - (car args)) - (message "Your opponent is now ready to play")) - (let ((chess-engine-handling-event t)) - ;; NOTE: There will be no display for this game object! This - ;; is really only useful if you are using a computer on the - ;; accepting side - (unless game - (setq game (chess-engine-set-game nil (chess-game-create)))) - (chess-engine-set-start-position nil))) + (chess-game-end chess-engine-game :draw) + (chess-game-set-data chess-engine-game 'active nil)) + (chess-engine-command nil 'accept)) + (chess-engine-command nil 'decline)) + t) + + ((eq event 'abort) + (if (y-or-n-p "Your opponent wants to abort this game, accept? ") + (progn (let ((chess-engine-handling-event t)) - (cond - ((eq chess-engine-pending-offer 'draw) - (message "Your draw offer was accepted") - (chess-game-end game :draw) - (chess-game-set-data game 'active nil)) - - ((eq chess-engine-pending-offer 'abort) - (message "Your offer to abort was accepted") - (chess-game-set-data game 'active nil)) - - ((eq chess-engine-pending-offer 'undo) - (message "Request to undo %d moves was accepted" - chess-engine-pending-arg) - (chess-game-undo game (car args)))))) - (setq chess-engine-pending-offer nil - chess-engine-pending-arg nil) - t)) - - ((eq event 'decline) - (when (and game chess-engine-pending-offer) - (cond - ((eq chess-engine-pending-offer 'draw) - (message "Your draw offer was declined")) - - ((eq chess-engine-pending-offer 'abort) - (message "Your offer to abort was declined")) - - ((eq chess-engine-pending-offer 'undo) - (message "Your request to undo %d moves was decline" - chess-engine-pending-arg))) - - (setq chess-engine-pending-offer nil - chess-engine-pending-arg nil) - t)) - - ((eq event 'retract) - (when (and game chess-engine-pending-offer) - (cond - ((eq chess-engine-pending-offer 'draw) - (message "Your opponent has retracted their draw offer")) - - ((eq chess-engine-pending-offer 'abort) - (message "Your opponent has retracted their offer to abort")) - - ((eq chess-engine-pending-offer 'undo) - (message "Your opponent has retracted their request to undo %d moves" - chess-engine-pending-arg))) - - (setq chess-engine-pending-offer nil - chess-engine-pending-arg nil) - t))))) - -(defun chess-engine-create (module &optional response-handler &rest args) + (chess-game-set-data chess-engine-game 'active nil)) + (chess-engine-command nil 'accept)) + (chess-engine-command nil 'decline)) + t) + + ((eq event 'undo) + (if (y-or-n-p + (format "Your opponent wants to take back %d moves, accept? " + (car args))) + (progn + (let ((chess-engine-handling-event t)) + (chess-game-undo chess-engine-game (car args))) + (chess-engine-command nil 'accept)) + (chess-engine-command nil 'decline)) + t) + + ((eq event 'accept) + (when chess-engine-pending-offer + (if (eq chess-engine-pending-offer 'match) + (unless (chess-game-data chess-engine-game 'active) + (if (and (car args) (> (length (car args)) 0)) + (message "Your opponent, %s, is now ready to play" + (car args)) + (message "Your opponent is now ready to play")) + (let ((chess-engine-handling-event t)) + (chess-engine-set-position nil))) + (let ((chess-engine-handling-event t)) + (cond + ((eq chess-engine-pending-offer 'draw) + (message "Your draw offer was accepted") + (chess-game-end chess-engine-game :draw) + (chess-game-set-data chess-engine-game 'active nil)) + + ((eq chess-engine-pending-offer 'abort) + (message "Your offer to abort was accepted") + (chess-game-set-data chess-engine-game 'active nil)) + + ((eq chess-engine-pending-offer 'undo) + (message "Request to undo %d moves was accepted" + chess-engine-pending-arg) + (chess-game-undo chess-engine-game (car args)))))) + (setq chess-engine-pending-offer nil + chess-engine-pending-arg nil) + t)) + + ((eq event 'decline) + (when chess-engine-pending-offer + (cond + ((eq chess-engine-pending-offer 'draw) + (message "Your draw offer was declined")) + + ((eq chess-engine-pending-offer 'abort) + (message "Your offer to abort was declined")) + + ((eq chess-engine-pending-offer 'undo) + (message "Your request to undo %d moves was decline" + chess-engine-pending-arg))) + + (setq chess-engine-pending-offer nil + chess-engine-pending-arg nil) + t)) + + ((eq event 'retract) + (when chess-engine-pending-offer + (cond + ((eq chess-engine-pending-offer 'draw) + (message "Your opponent has retracted their draw offer")) + + ((eq chess-engine-pending-offer 'abort) + (message "Your opponent has retracted their offer to abort")) + + ((eq chess-engine-pending-offer 'undo) + (message "Your opponent has retracted their request to undo %d moves" + chess-engine-pending-arg))) + + (setq chess-engine-pending-offer nil + chess-engine-pending-arg nil) + t)))) + +(defun chess-engine-create (game module &optional response-handler + &rest handler-ctor-args) (let ((regexp-alist (intern-soft (concat (symbol-name module) "-regexp-alist"))) (handler (intern-soft (concat (symbol-name module) "-handler")))) (with-current-buffer (generate-new-buffer " *chess-engine*") - (let ((proc (apply handler 'initialize args))) + (let ((proc (apply handler 'initialize handler-ctor-args))) (setq chess-engine-regexp-alist (symbol-value regexp-alist) chess-engine-event-handler handler chess-engine-response-handler (or response-handler 'chess-engine-default-handler)) + (chess-engine-set-game* nil game t) (when (processp proc) (unless (memq (process-status proc) '(run open)) (error "Failed to start chess engine process")) @@ -299,8 +272,8 @@ (defun chess-engine-command (engine event &rest args) (chess-with-current-buffer engine - (apply 'chess-engine-event-handler - (chess-engine-game nil) engine event args))) + (apply 'chess-engine-event-handler chess-engine-game + engine event args))) ;; 'ponder ;; 'search-depth @@ -323,75 +296,44 @@ (chess-with-current-buffer engine chess-engine-response-handler)) -(defun chess-engine-set-position (engine position) +(defun chess-engine-set-position (engine &optional position my-color) (chess-with-current-buffer engine - (if chess-engine-game - (chess-engine-detach-game nil)) - (setq chess-engine-game nil - chess-engine-position position) - (chess-engine-command nil 'setup-pos position))) + (let ((chess-game-inhibit-events t)) + (if position + (progn + (chess-game-set-start-position chess-engine-game position) + (chess-game-set-data chess-engine-game 'my-color my-color)) + (chess-game-set-start-position chess-engine-game + chess-starting-position) + (chess-game-set-data chess-engine-game 'my-color t)) + (chess-game-set-data chess-engine-game 'active t)))) (defun chess-engine-position (engine) (chess-with-current-buffer engine - (or (and chess-engine-game - (chess-game-pos chess-engine-game)) - chess-engine-position))) + (chess-game-pos chess-engine-game))) -(defun chess-engine-set-start-position (engine &optional position my-color) +(defun chess-engine-set-game (engine game &optional no-setup) (chess-with-current-buffer engine - (let ((game (chess-engine-game nil))) - (if (null game) - (chess-engine-set-position nil (or position - chess-starting-position)) - (let ((chess-game-inhibit-events t)) - (if position - (progn - (chess-game-set-start-position game position) - (chess-game-set-data game 'my-color my-color)) - (chess-game-set-start-position game chess-starting-position) - (chess-game-set-data game 'my-color t)) - (chess-game-set-data game 'active t)) - (chess-game-run-hooks game 'orient))))) + (chess-game-set-tags chess-engine-game (chess-game-tags game)) + ;; this call triggers `setup-game' for us + (let ((chess-game-inhibit-events no-setup)) + (chess-game-set-plies chess-engine-game (chess-game-plies game))))) -(defun chess-engine-set-game (engine game &optional no-setup) +(defun chess-engine-set-game* (engine game &optional no-setup) (chess-with-current-buffer engine (if chess-engine-game (chess-engine-detach-game nil)) - (setq chess-engine-game game - chess-engine-position nil) - (when game - (chess-game-add-hook game 'chess-engine-event-handler engine) - (unless no-setup - (chess-engine-command nil 'setup-game game))))) - -(defsubst chess-engine-set-game* (engine game) - "This function is a special variant of `chess-engine-set-game'. -It should be used only if: - ENGINE is an engine which is newly created, and has not been used. - GAME is a new game at the starting position, which has not been used. - -This function exists because all engines start out assuming the -starting position, which in effect means that `setup-game's work has -already been done, and therefore does not need to be duplicated. - -There is no harm in calling `chess-engine-set-game' instead of this -function in all cases; this is merely a bandwidth-saver." - (chess-engine-set-game engine game t)) - -(defun chess-engine-copy-game (engine game) - (chess-with-current-buffer engine - (if (null chess-engine-game) - (chess-engine-set-game nil game) - (chess-game-set-tags chess-engine-game game) - ;; this call triggers `setup-game' for us - (chess-game-set-plies chess-engine-game game)))) + (setq chess-engine-game game) + (chess-game-add-hook game 'chess-engine-event-handler + (or engine (current-buffer))) + (unless no-setup + (chess-engine-command nil 'setup-game game)))) (defun chess-engine-detach-game (engine) (chess-with-current-buffer engine - (if chess-engine-game - (chess-game-remove-hook chess-engine-game - 'chess-engine-event-handler - (or engine (current-buffer)))))) + (chess-game-remove-hook chess-engine-game + 'chess-engine-event-handler + (or engine (current-buffer))))) (defun chess-engine-game (engine) (chess-with-current-buffer engine @@ -399,12 +341,11 @@ function in all cases; this is merely a bandwidth-saver." (defun chess-engine-index (engine) (chess-with-current-buffer engine - (if chess-engine-game - (chess-game-index chess-engine-game)))) + (chess-game-index chess-engine-game))) (defun chess-engine-move (engine ply) (chess-with-current-buffer engine - (chess-engine-do-move ply) + (chess-game-move chess-engine-game ply) (chess-engine-command engine 'move ply))) (defun chess-engine-send (engine string) @@ -437,7 +378,6 @@ function in all cases; this is merely a bandwidth-saver." "Handle any commands being sent to this instance of this module." (unless chess-engine-handling-event (chess-with-current-buffer engine - (assert (eq game (chess-engine-game nil))) (apply chess-engine-event-handler event args)) (cond diff --git a/chess-ics.el b/chess-ics.el index 5bd9639..96a410d 100644 --- a/chess-ics.el +++ b/chess-ics.el @@ -126,25 +126,25 @@ who is black." (let ((chess-engine-handling-event t) (begin (match-beginning 1)) (end (match-end 1)) - (info (chess-ics12-parse (match-string 3))) - (game (chess-engine-game nil))) - (assert game) - (if (and (chess-game-data game 'active) - (> (chess-game-index game) 0)) + (info (chess-ics12-parse (match-string 3)))) + (if (and (chess-game-data chess-engine-game 'active) + (> (chess-game-index chess-engine-game) 0)) (when (and (cadr info) (eq (chess-pos-side-to-move (car info)) - (chess-game-data game 'my-color))) - (chess-engine-do-move - (chess-algebraic-to-ply - (chess-ply-pos (car (last (chess-game-plies game)))) - (cadr info) t)) + (chess-game-data chess-engine-game 'my-color))) + (chess-game-move chess-engine-game + (chess-algebraic-to-ply + (chess-ply-pos + (car (last (chess-game-plies chess-engine-game)))) + (cadr info) t)) (assert (equal (car info) (chess-engine-position nil)))) (let ((chess-game-inhibit-events t) plies) - (chess-game-set-data game 'my-color (string= (nth 2 info) - chess-ics-handle)) - (chess-game-set-data game 'active t) - (chess-game-set-start-position game (car info))) - (chess-game-run-hooks game 'orient)) + (chess-game-set-data chess-engine-game + 'my-color (string= (nth 2 info) + chess-ics-handle)) + (chess-game-set-data chess-engine-game 'active t) + (chess-game-set-start-position chess-engine-game (car info))) + (chess-game-run-hooks chess-engine-game 'orient)) (delete-region begin end) t)) diff --git a/chess-link.el b/chess-link.el index a6dea4e..9552a0f 100644 --- a/chess-link.el +++ b/chess-link.el @@ -12,10 +12,11 @@ (defun chess-link-response-handler (event &rest args) "This function handles responses from the bot's computing engine." - (let* ((game (chess-engine-game nil)) - (first-engine (chess-game-data game 'first-engine)) - (second-engine (chess-game-data game 'second-engine)) - return-value) + (let ((first-engine + (chess-game-data chess-engine-game 'first-engine)) + (second-engine + (chess-game-data chess-engine-game 'second-engine)) + return-value) (cond ((eq event 'match) (chess-engine-command nil 'accept) @@ -51,23 +52,23 @@ engine, and the computer the second engine." second-engine-type (intern (concat "chess-" second-engine-type))) (require chess-default-display) (let* ((my-color t) ; we start out as white always - (display (chess-display-create chess-default-display my-color)) - (game (chess-game-create))) + (game (chess-game-create)) + (display (chess-display-create game chess-default-display + my-color))) (chess-game-set-data game 'my-color my-color) - (chess-display-set-game display game) (chess-display-set-main display) (chess-display-disable-popup display) (condition-case err (when (and (require first-engine-type) (require second-engine-type)) - (let ((first-engine (chess-engine-create first-engine-type)) - (second-engine (chess-engine-create second-engine-type))) + (let ((first-engine + (chess-engine-create game first-engine-type)) + (second-engine + (chess-engine-create game second-engine-type))) - (chess-engine-set-game* first-engine game) (chess-game-set-data game 'first-engine first-engine) (chess-engine-command first-engine 'ready) - (chess-engine-set-game* second-engine game) (chess-game-set-data game 'second-engine second-engine) (chess-link-connect first-engine second-engine) (chess-engine-command second-engine 'ready) diff --git a/chess-network.el b/chess-network.el index c80487e..c5e92c9 100644 --- a/chess-network.el +++ b/chess-network.el @@ -112,8 +112,7 @@ ((eq event 'resign) (chess-engine-send nil "resign\n") - (and (chess-engine-game nil) - (chess-game-set-data (chess-engine-game nil) 'active nil))) + (chess-game-set-data chess-engine-game 'active nil)) ((eq event 'draw) (if chess-engine-pending-offer @@ -144,10 +143,9 @@ (chess-engine-send nil "retract\n")) ((eq event 'move) - (chess-engine-send nil (concat (chess-ply-to-algebraic (car args)) - "\n")) - (if (chess-game-over-p (chess-engine-game nil)) - (chess-game-set-data (chess-engine-game nil) 'active nil))))) + (chess-engine-send nil (concat (chess-ply-to-algebraic (car args)) "\n")) + (if (chess-game-over-p chess-engine-game) + (chess-game-set-data chess-engine-game 'active nil))))) (provide 'chess-network) diff --git a/chess-none.el b/chess-none.el index db628bf..edd2a29 100644 --- a/chess-none.el +++ b/chess-none.el @@ -7,17 +7,16 @@ (require 'chess-engine) (defun chess-none-handler (event &rest args) - "Initialize the network chess engine." + "An empty chess engine, used for fielding key events. +This is only useful when two humans are playing each other, in which +case this engine will do the job of accepting undos, handling +resignations, etc." (cond - ((eq event 'send)) - ((memq event '(resign abort)) - (and (chess-engine-game nil) - (chess-engine-set-start-position nil))) + (chess-engine-set-position nil)) ((eq event 'undo) - (if (chess-engine-game nil) - (chess-game-undo (chess-engine-game nil) (car args)))))) + (chess-game-undo chess-engine-game (car args))))) (provide 'chess-none) @@ -128,8 +128,9 @@ minibuffer, which works well for Emacspeak users." (require chess-default-display) (let* ((my-color t) ; we start out as white always - (display (chess-display-create chess-default-display my-color)) - (game (chess-game-create))) + (game (chess-game-create)) + (display (chess-display-create game chess-default-display + my-color))) (when (and (eq chess-default-display 'chess-images) (with-current-buffer display @@ -137,19 +138,17 @@ minibuffer, which works well for Emacspeak users." (message "Could not find suitable chess images; using ics1 display") (chess-display-destroy display) (require 'chess-ics1) - (setq display (chess-display-create 'chess-ics1 my-color))) + (setq display (chess-display-create game 'chess-ics1 my-color))) (chess-game-set-data game 'my-color my-color) (if disable-popup (chess-display-disable-popup display)) - (chess-display-set-game display game) (chess-display-set-main display) (let ((engine-module (or engine chess-default-engine))) (when (and engine-module (require engine-module nil t)) - (let ((engine (apply 'chess-engine-create engine-module nil + (let ((engine (apply 'chess-engine-create game engine-module nil engine-ctor-args))) - (chess-engine-set-game* engine game) ;; for the sake of engines which are ready to play now, and ;; which don't need connect/accept negotiation (most ;; computerized engines fall into this category), we need to @@ -187,9 +186,8 @@ minibuffer, which works well for Emacspeak users." (let ((game (chess-pgn-to-game))) (when game (require chess-default-display) - (chess-display-set-game - (chess-display-create chess-default-display - (chess-game-side-to-move game)) game)))) + (chess-display-create game chess-default-display + (chess-game-side-to-move game))))) (defvar chess-puzzle-locations nil) @@ -220,23 +218,23 @@ making it easy to go on to the next puzzle once you've solved one." (let ((game (chess-pgn-to-game))) (when game (require chess-default-display) - (let ((puzzle-display - (or (and (buffer-live-p (aref chess-puzzle-locations 2)) - (aref chess-puzzle-locations 2)) - (chess-display-create chess-default-display - (chess-game-side-to-move game))))) - (chess-display-set-game puzzle-display game) + (let (puzzle-display) + (if (buffer-live-p (aref chess-puzzle-locations 2)) + (progn + (setq puzzle-display (aref chess-puzzle-locations 2)) + (chess-display-set-game puzzle-display game)) + (setq puzzle-display + (chess-display-create game chess-default-display + (chess-game-side-to-move game)))) (aset chess-puzzle-locations 2 puzzle-display) ;; setup spacebar as a convenient way to jump to the next puzzle (with-current-buffer puzzle-display (define-key (current-local-map) [? ] 'chess-puzzle-next))) (require chess-default-engine) - (let ((puzzle-engine - (or (and (buffer-live-p (aref chess-puzzle-locations 3)) - (aref chess-puzzle-locations 3)) - (chess-engine-create chess-default-engine)))) - (chess-engine-set-game puzzle-engine game) - (aset chess-puzzle-locations 3 puzzle-engine))))))) + (aset chess-puzzle-locations 3 + (or (and (buffer-live-p (aref chess-puzzle-locations 3)) + (aref chess-puzzle-locations 3)) + (chess-engine-create game chess-default-engine)))))))) (provide 'chess) |
