]> code.delx.au - gnu-emacs-elpa/blob - chess-stockfish.el
Parse UCI long algebraic moves correctly.
[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
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 ;;; Code:
24
25 (require 'chess-uci)
26
27 (defgroup chess-stockfish nil
28 "The publically available chess engine 'stockfish'."
29 :group 'chess-engine
30 :link '(url-link "http://www.stockfishchess.com"))
31
32 (defcustom chess-stockfish-path (executable-find "stockfish")
33 "*The path to the stockfish executable."
34 :type 'file
35 :group 'chess-stockfish)
36
37 (defvar chess-stockfish-regexp-alist
38 (append
39 chess-uci-regexp-alist
40 (list
41 (cons (concat "^info\\s-+.*nps\\s-+\\([0-9]+\\).*pv\\s-+\\("
42 chess-uci-long-algebraic-regexp
43 "\\(\\s-+" chess-uci-long-algebraic-regexp "\\)+\\)")
44 (function
45 (lambda ()
46 (setq-local chess-stockfish-nps (string-to-number (match-string 1)))
47 (setq-local chess-stockfish-pv
48 (split-string (match-string 2) " ")))))))
49 "Patterns used to match stockfish output.")
50
51 (defun chess-stockfish-handler (game event &rest args)
52 (unless chess-engine-handling-event
53 (cond
54 ((eq event 'initialize)
55 (let ((proc (chess-uci-handler game 'initialize "stockfish")))
56 (when (and proc (processp proc) (eq (process-status proc) 'run))
57 (process-send-string proc "uci\n")
58 (setq chess-engine-process proc)
59 t)))
60
61 (t
62 (if (and (eq event 'undo)
63 (= 1 (mod (car args) 2)))
64 (error "Cannot undo until after stockfish moves"))
65
66 (apply 'chess-uci-handler game event args)))))
67
68 (provide 'chess-stockfish)
69
70 ;;; chess-stockfish.el ends here