]> code.delx.au - gnu-emacs-elpa/blob - chess-uci.el
Make the strength of book replies configurable.
[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 :must-match t)))
40
41 (defcustom chess-uci-polyglot-book-strength 1.0
42 "Influence random distribution when picking a ply from the book.
43 A value above 1.0 means to prefer known good moves while a value below
44 1.0 means to penalize known good moves. 0.0 will stop to consider
45 move weights and simply pick a move at random. For simple
46 reasons of numerical overflow, this should be strictly less than 4.0."
47 :group 'chess-uci
48 :type '(float :match (lambda (widget value) (and (>= value 0) (< value 4)))))
49
50 (defvar chess-uci-book nil
51 "A (polyglot) opening book object.
52 See `chess-uci-polyglot-book-file' for details on how to enable this.")
53
54 (defvar chess-uci-long-algebraic-regexp "\\([a-h][1-8]\\)\\([a-h][1-8]\\)\\([nbrq]\\)?"
55 "A regular expression matching a UCI log algebraic move.")
56
57 (defun chess-uci-long-algebraic-to-ply (position move)
58 "Convert the long algebraic notation MOVE for POSITION to a ply."
59 (assert (vectorp position))
60 (assert (stringp move))
61 (let ((case-fold-search nil))
62 (when (string-match chess-uci-long-algebraic-regexp move)
63 (let ((color (chess-pos-side-to-move position))
64 (from (chess-coord-to-index (match-string 1 move)))
65 (to (chess-coord-to-index (match-string 2 move)))
66 (promotion (match-string 3 move)))
67 (apply #'chess-ply-create position nil
68 (if (and (= from (chess-pos-king-index position color))
69 (= (chess-index-rank from) (chess-index-rank to))
70 (> (abs (- (chess-index-file from)
71 (chess-index-file to))) 1))
72 (chess-ply-castling-changes
73 position
74 (< (- (chess-index-file to) (chess-index-file from)) 0))
75 (nconc (list from to)
76 (when promotion
77 (list :promote (upcase (aref promotion 0)))))))))))
78
79 (defsubst chess-uci-convert-long-algebraic (move)
80 "Convert long algebraic MOVE to a ply in reference to the engine position.
81 If conversion fails, this function fired an 'illegal event."
82 (or (chess-uci-long-algebraic-to-ply (chess-engine-position nil) move)
83 (chess-engine-command nil 'illegal)))
84
85 (defvar chess-uci-regexp-alist
86 (list
87 (cons "^id\\s-+name\\s-+\\(.+\\)$"
88 (function
89 (lambda ()
90 (setq-local chess-engine-opponent-name (match-string 1))
91 'once)))
92 (cons (concat "^bestmove\\s-+\\(" chess-uci-long-algebraic-regexp "\\)")
93 (function
94 (lambda ()
95 (funcall chess-engine-response-handler 'move
96 (chess-uci-convert-long-algebraic (match-string 1)))))))
97 "Patterns matching responses of a standard UCI chess engine.")
98
99 (defun chess-uci-position (game)
100 "Convert the current GAME position to a UCI position command string."
101 (concat "position fen " (chess-pos-to-fen (chess-game-pos game 0) t)
102 " moves " (mapconcat (lambda (ply)
103 (let ((source (chess-ply-source ply))
104 (target (chess-ply-target ply)))
105 (if (and source target)
106 (concat (chess-index-to-coord source)
107 (chess-index-to-coord target)
108 (if (chess-ply-keyword ply :promote)
109 (string (downcase (chess-ply-keyword ply :promote)))
110 ""))
111 "")))
112 (chess-game-plies game) " ")
113 "\n"))
114
115 (defun chess-uci-handler (game event &rest args)
116 "Default handler for UCI based engines."
117 (unless chess-engine-handling-event
118 (cond
119 ((eq event 'initialize)
120 (when chess-uci-polyglot-book-file
121 (unless chess-uci-book
122 (setq chess-uci-book (chess-polyglot-book-open
123 chess-uci-polyglot-book-file))))
124 (apply #'chess-common-handler game event args))
125
126 ((eq event 'move)
127 (when (= 1 (chess-game-index game))
128 (chess-game-set-tag game "White" chess-full-name)
129 (chess-game-set-tag game "Black" chess-engine-opponent-name))
130
131 (let ((book-ply (and chess-uci-book (bufferp chess-uci-book)
132 (buffer-live-p chess-uci-book)
133 (chess-polyglot-book-ply
134 chess-uci-book
135 (chess-game-pos game)
136 chess-uci-polyglot-book-strength))))
137 (if book-ply
138 (let ((chess-display-handling-event nil))
139 (funcall chess-engine-response-handler 'move book-ply))
140 (chess-engine-send nil (concat (chess-uci-position game) "go\n"))))
141
142 (if (chess-game-over-p game)
143 (chess-game-set-data game 'active nil)))
144
145 (t
146 (apply 'chess-common-handler game event args)))))
147
148 (provide 'chess-uci)
149
150 ;;; chess-uci.el ends here