1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Play against gnuchess!
7 (require 'chess-engine)
8 (require 'chess-common)
10 (defgroup chess-gnuchess nil
11 "The publically available chess engine 'gnuchess'."
14 (defcustom chess-gnuchess-path (executable-find "gnuchess")
15 "The path to the gnuchess executable."
17 :group 'chess-gnuchess)
19 (defvar chess-gnuchess-bad-board nil)
20 (make-variable-buffer-local 'chess-gnuchess-bad-board)
22 (defvar chess-gnuchess-regexp-alist
24 (cons (concat "My move is : \\(" chess-algebraic-regexp "\\)")
27 (funcall chess-engine-response-handler 'move
28 (chess-engine-convert-algebraic (match-string 1) t)))))
32 (chess-error 'illegal-move))))
33 (cons "Board is wrong!"
36 ;; gnuchess didn't like the given position, which
37 ;; means it won't play against it unless we send a
38 ;; "go" after the user's move
39 (setq chess-gnuchess-bad-board t))))))
41 (defun chess-gnuchess-handler (game event &rest args)
42 (unless chess-engine-handling-event
44 ((eq event 'initialize)
45 (let ((proc (chess-common-handler game 'initialize "gnuchess")))
46 (when (and (processp proc)
47 (eq (process-status proc) 'run))
48 (process-send-string proc "nopost\n")
51 ((eq event 'setup-pos)
52 (let ((file (chess-with-temp-file
53 (insert (chess-pos-to-string (car args)) ?\n))))
54 (chess-engine-send nil (format "epdload %s\n" file))))
56 ((eq event 'setup-game)
57 (let ((file (chess-with-temp-file
58 (insert (chess-game-to-string (car args)) ?\n))))
59 (chess-engine-send nil (format "pgnload %s\n" file))))
62 (chess-engine-send nil (concat (if (chess-pos-side-to-move
63 (chess-engine-position nil))
66 (chess-engine-send nil "go\n")
67 (setq chess-gnuchess-bad-board nil))
70 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
72 (when chess-gnuchess-bad-board
73 (chess-engine-send nil "go\n")
74 (setq chess-gnuchess-bad-board nil)))
77 (apply 'chess-common-handler game event args)))))
79 (provide 'chess-gnuchess)
81 ;;; chess-gnuchess.el ends here