]> code.delx.au - gnu-emacs-elpa/blobdiff - chess-uci.el
chess-uci: Choose a (random) move from the book.
[gnu-emacs-elpa] / chess-uci.el
index faf77efeb2912a9979dc2490d6722534f9cdf186..ad6852c492bad12575f9e93a251cd301d47fce1c 100644 (file)
 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 ;; Boston, MA 02111-1307, USA.
 
+;;; Commentary:
+
+;; URL: http://en.wikipedia.org/wiki/Universal_Chess_Interface
+
 ;;; Code:
 
 (require 'chess-common)
+(require 'chess-polyglot)
+
+(defgroup chess-uci nil
+  "Customisations for Chess engines based on the UCI protocol"
+  :group 'chess)
+
+(defcustom chess-uci-polyglot-book-file nil
+  "The path to a polyglot binary opening book file."
+  :group 'chess-uci
+  :type '(choice (const :tag "Not specified" nil) file))
+
+(defvar chess-uci-book nil
+  "A (polyglot) opening book object.
+See `chess-uci-polyglot-book-file' for details on how to enable this.")
 
 (defvar chess-uci-long-algebraic-regexp "\\([a-h][1-8]\\)\\([a-h][1-8]\\)\\([nbrq]\\)?"
-  "A regular expression matching a UCI move.")
+  "A regular expression matching a UCI log algebraic move.")
 
 (defun chess-uci-long-algebraic-to-ply (position move)
   "Convert the long algebraic notation MOVE for POSITION to a ply."
@@ -48,7 +66,7 @@
                 (nconc (list from to)
                        (when promotion
                          (list :promote (upcase (aref promotion 0)))))))))))
-                                        
+
 (defsubst chess-uci-convert-long-algebraic (move)
   "Convert long algebraic MOVE to a ply in reference to the engine position.
 If conversion fails, this function fired an 'illegal event."
@@ -70,6 +88,7 @@ If conversion fails, this function fired an 'illegal event."
   "Patterns matching responses of a standard UCI chess engine.")
 
 (defun chess-uci-position (game)
+  "Convert the current GAME position to a UCI position command string."
   (concat "position fen " (chess-pos-to-fen (chess-game-pos game 0) t)
          " moves " (mapconcat (lambda (ply)
                                 (let ((source (chess-ply-source ply))
@@ -85,14 +104,30 @@ If conversion fails, this function fired an 'illegal event."
          "\n"))
 
 (defun chess-uci-handler (game event &rest args)
+  "Default handler for UCI based engines."
   (unless chess-engine-handling-event
     (cond
+     ((eq event 'initialize)
+      (when chess-uci-polyglot-book-file
+       (unless chess-uci-book
+         (setq chess-uci-book (chess-polyglot-book-open
+                               chess-uci-polyglot-book-file))))
+      (apply #'chess-common-handler game event args))
+
      ((eq event 'move)
       (when (= 1 (chess-game-index game))
        (chess-game-set-tag game "White" chess-full-name)
        (chess-game-set-tag game "Black" chess-engine-opponent-name))
 
-      (chess-engine-send nil (concat (chess-uci-position game) "go\n"))
+      (let ((book-ply (and chess-uci-book (bufferp chess-uci-book)
+                          (buffer-live-p chess-uci-book)
+                          (chess-polyglot-book-ply chess-uci-book
+                                                   (chess-game-pos game)))))
+       (if book-ply
+           (let ((chess-display-handling-event nil))
+             (funcall chess-engine-response-handler 'move book-ply))
+         (chess-engine-send nil (concat (chess-uci-position game) "go\n"))))
+
       (if (chess-game-over-p game)
          (chess-game-set-data game 'active nil)))