summaryrefslogtreecommitdiff
path: root/chess-common.el
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2002-04-11 07:57:23 +0000
committerJohn Wiegley <johnw@newartisans.com>2002-04-11 07:57:23 +0000
commit232756b32dab6030f711b197851c9e4b9540994d (patch)
tree083154a439a14c554f9ddfdeb3cecee9a3358dba /chess-common.el
parent4bf636091a501c662213c8bc80242d2f65e97f09 (diff)
*** no comment ***
Diffstat (limited to 'chess-common.el')
-rw-r--r--chess-common.el76
1 files changed, 76 insertions, 0 deletions
diff --git a/chess-common.el b/chess-common.el
new file mode 100644
index 0000000..7019bb9
--- /dev/null
+++ b/chess-common.el
@@ -0,0 +1,76 @@
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;; Define handler functions that are common to the (relatively)
+;; standard chess engine communication protocol:
+;;
+;; http://www.tim-mann.org/xboard/engine-intf.html
+;;
+;; $Revision$
+
+(require 'chess-engine)
+
+(defvar chess-common-temp-files nil)
+(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))
+
+(put 'chess-with-temp-file 'lisp-indent-function 1)
+
+(defun chess-common-handler (event &rest args)
+ "Initialize the network chess engine."
+ (cond
+ ((eq event 'initialize)
+ (let* ((name (car args))
+ (path (intern (concat "chess-" name "-path")))
+ proc)
+ (message "Starting chess program '%s'..." name)
+ (unless (boundp path)
+ (error "Cannot find %s executable; check `%s'" name path))
+ (setq proc (start-process (concat "chess-" name)
+ (current-buffer) (symbol-value path)))
+ (message "Starting chess program '%s'...done" name)
+ proc))
+
+ ((eq event 'ready)
+ (and (chess-engine-game nil)
+ (chess-game-set-data (chess-engine-game nil) 'active t)))
+
+ ((eq event 'shutdown)
+ (chess-engine-send nil "quit\n")
+ (dolist (file chess-common-temp-files)
+ (if (file-exists-p file)
+ (delete-file file)))
+ (setq chess-common-temp-files nil))
+
+ ((eq event 'pass)
+ (chess-engine-send nil "go\n"))
+
+ ((eq event 'resign)
+ (chess-engine-send nil "resign\n"))
+
+ ((eq event 'draw)
+ (message "Your draw offer was declined"))
+
+ ((memq event '(resign abort))
+ (chess-engine-send nil "new\n")
+ (and (chess-engine-game nil)
+ (chess-engine-set-start-position nil)))
+
+ ((eq event 'undo)
+ (when (chess-engine-game nil)
+ (dotimes (i (car args))
+ (chess-engine-send nil "undo\n"))
+ (chess-game-undo (chess-engine-game nil) (car args))))
+
+ ((eq event 'move)
+ (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
+ "\n")))))
+
+(provide 'chess-common)
+
+;;; chess-common.el ends here