1 ;;; chess-uci.el --- Universal chess interface protocol for emacs-chess
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
5 ;; Author: Mario Lang <mlang@delysid.org>
8 ;; This file is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; This file is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
25 ;; URL: http://en.wikipedia.org/wiki/Universal_Chess_Interface
29 (require 'chess-common)
30 (require 'chess-polyglot)
32 (defgroup chess-uci nil
33 "Customisations for Chess engines based on the UCI protocol"
36 (defcustom chess-uci-polyglot-book-file nil
37 "The path to a polyglot binary opening book file."
39 :type '(choice (const :tag "Not specified" nil) file))
41 (defvar chess-uci-book nil
42 "A (polyglot) opening book object.
43 See `chess-uci-polyglot-book-file' for details on how to enable this.")
45 (defvar chess-uci-long-algebraic-regexp "\\([a-h][1-8]\\)\\([a-h][1-8]\\)\\([nbrq]\\)?"
46 "A regular expression matching a UCI log algebraic move.")
48 (defun chess-uci-long-algebraic-to-ply (position move)
49 "Convert the long algebraic notation MOVE for POSITION to a ply."
50 (assert (vectorp position))
51 (assert (stringp move))
52 (let ((case-fold-search nil))
53 (when (string-match chess-uci-long-algebraic-regexp move)
54 (let ((color (chess-pos-side-to-move position))
55 (from (chess-coord-to-index (match-string 1 move)))
56 (to (chess-coord-to-index (match-string 2 move)))
57 (promotion (match-string 3 move)))
58 (apply #'chess-ply-create position nil
59 (if (and (= from (chess-pos-king-index position color))
60 (= (chess-index-rank from) (chess-index-rank to))
61 (> (abs (- (chess-index-file from)
62 (chess-index-file to))) 1))
63 (chess-ply-castling-changes
65 (< (- (chess-index-file to) (chess-index-file from)) 0))
68 (list :promote (upcase (aref promotion 0)))))))))))
70 (defsubst chess-uci-convert-long-algebraic (move)
71 "Convert long algebraic MOVE to a ply in reference to the engine position.
72 If conversion fails, this function fired an 'illegal event."
73 (or (chess-uci-long-algebraic-to-ply (chess-engine-position nil) move)
74 (chess-engine-command nil 'illegal)))
76 (defvar chess-uci-regexp-alist
78 (cons "^id\\s-+name\\s-+\\(.+\\)$"
81 (setq-local chess-engine-opponent-name (match-string 1))
83 (cons (concat "^bestmove\\s-+\\(" chess-uci-long-algebraic-regexp "\\)")
86 (funcall chess-engine-response-handler 'move
87 (chess-uci-convert-long-algebraic (match-string 1)))))))
88 "Patterns matching responses of a standard UCI chess engine.")
90 (defun chess-uci-position (game)
91 "Convert the current GAME position to a UCI position command string."
92 (concat "position fen " (chess-pos-to-fen (chess-game-pos game 0) t)
93 " moves " (mapconcat (lambda (ply)
94 (let ((source (chess-ply-source ply))
95 (target (chess-ply-target ply)))
96 (if (and source target)
97 (concat (chess-index-to-coord source)
98 (chess-index-to-coord target)
99 (if (chess-ply-keyword ply :promote)
100 (string (downcase (chess-ply-keyword ply :promote)))
103 (chess-game-plies game) " ")
106 (defun chess-uci-handler (game event &rest args)
107 "Default handler for UCI based engines."
108 (unless chess-engine-handling-event
110 ((eq event 'initialize)
111 (when chess-uci-polyglot-book-file
112 (unless chess-uci-book
113 (setq chess-uci-book (chess-polyglot-book-open
114 chess-uci-polyglot-book-file))))
115 (apply #'chess-common-handler game event args))
118 (when (= 1 (chess-game-index game))
119 (chess-game-set-tag game "White" chess-full-name)
120 (chess-game-set-tag game "Black" chess-engine-opponent-name))
122 (let ((book-ply (and chess-uci-book (bufferp chess-uci-book)
123 (buffer-live-p chess-uci-book)
124 (chess-polyglot-book-ply chess-uci-book
125 (chess-game-pos game)))))
127 (let ((chess-display-handling-event nil))
128 (funcall chess-engine-response-handler 'move book-ply))
129 (chess-engine-send nil (concat (chess-uci-position game) "go\n"))))
131 (if (chess-game-over-p game)
132 (chess-game-set-data game 'active nil)))
135 (apply 'chess-common-handler game event args)))))
139 ;;; chess-uci.el ends here