]> code.delx.au - gnu-emacs-elpa/blob - chess-uci.el
chess-uci: Choose a (random) move from the book.
[gnu-emacs-elpa] / chess-uci.el
1 ;;; chess-uci.el --- Universal chess interface protocol for emacs-chess
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Author: Mario Lang <mlang@delysid.org>
6 ;; Keywords: games
7
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)
11 ;; any later version.
12
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.
17
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.
22
23 ;;; Commentary:
24
25 ;; URL: http://en.wikipedia.org/wiki/Universal_Chess_Interface
26
27 ;;; Code:
28
29 (require 'chess-common)
30 (require 'chess-polyglot)
31
32 (defgroup chess-uci nil
33 "Customisations for Chess engines based on the UCI protocol"
34 :group 'chess)
35
36 (defcustom chess-uci-polyglot-book-file nil
37 "The path to a polyglot binary opening book file."
38 :group 'chess-uci
39 :type '(choice (const :tag "Not specified" nil) file))
40
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.")
44
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.")
47
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
64 position
65 (< (- (chess-index-file to) (chess-index-file from)) 0))
66 (nconc (list from to)
67 (when promotion
68 (list :promote (upcase (aref promotion 0)))))))))))
69
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)))
75
76 (defvar chess-uci-regexp-alist
77 (list
78 (cons "^id\\s-+name\\s-+\\(.+\\)$"
79 (function
80 (lambda ()
81 (setq-local chess-engine-opponent-name (match-string 1))
82 'once)))
83 (cons (concat "^bestmove\\s-+\\(" chess-uci-long-algebraic-regexp "\\)")
84 (function
85 (lambda ()
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.")
89
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)))
101 ""))
102 "")))
103 (chess-game-plies game) " ")
104 "\n"))
105
106 (defun chess-uci-handler (game event &rest args)
107 "Default handler for UCI based engines."
108 (unless chess-engine-handling-event
109 (cond
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))
116
117 ((eq event 'move)
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))
121
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)))))
126 (if book-ply
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"))))
130
131 (if (chess-game-over-p game)
132 (chess-game-set-data game 'active nil)))
133
134 (t
135 (apply 'chess-common-handler game event args)))))
136
137 (provide 'chess-uci)
138
139 ;;; chess-uci.el ends here