]> code.delx.au - gnu-emacs-elpa/blob - chess-stockfish.el
doc/chess.texi: Briefly document chess-eco.el and chess-polyglot.el.
[gnu-emacs-elpa] / chess-stockfish.el
1 ;;; chess-stockfish.el --- Play against stockfish!
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Author: Mario Lang <mlang@delysid.org>
6 ;; Keywords: games, processes
7
8 ;; This program 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 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Code:
22
23 (require 'chess-uci)
24
25 (defgroup chess-stockfish nil
26 "The publically available chess engine 'stockfish'."
27 :group 'chess-engine
28 :link '(url-link "http://www.stockfishchess.com"))
29
30 (defcustom chess-stockfish-path (executable-find "stockfish")
31 "*The path to the stockfish executable."
32 :type 'file
33 :group 'chess-stockfish)
34
35 (defvar chess-stockfish-regexp-alist
36 (append
37 chess-uci-regexp-alist
38 (list
39 (cons (concat "^info\\s-+.*nps\\s-+\\([0-9]+\\).*pv\\s-+\\("
40 chess-uci-long-algebraic-regexp
41 "\\(\\s-+" chess-uci-long-algebraic-regexp "\\)+\\)")
42 (function
43 (lambda ()
44 (setq-local chess-stockfish-nps (string-to-number (match-string 1)))
45 (setq-local chess-stockfish-pv
46 (split-string (match-string 2) " ")))))))
47 "Patterns used to match stockfish output.")
48
49 (defun chess-stockfish-handler (game event &rest args)
50 (unless chess-engine-handling-event
51 (cond
52 ((eq event 'initialize)
53 (let ((proc (chess-uci-handler game 'initialize "stockfish")))
54 (when (and proc (processp proc) (eq (process-status proc) 'run))
55 (process-send-string proc "uci\n")
56 (setq chess-engine-process proc)
57 t)))
58
59 (t
60 (if (and (eq event 'undo)
61 (= 1 (mod (car args) 2)))
62 (error "Cannot undo until after stockfish moves"))
63
64 (apply 'chess-uci-handler game event args)))))
65
66 (provide 'chess-stockfish)
67
68 ;;; chess-stockfish.el ends here