1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Play against gnuchess!
6 (require 'chess-common)
8 (defgroup chess-gnuchess nil
9 "The publically available chess engine 'gnuchess'."
12 (defcustom chess-gnuchess-path (let ((exec-path (cons "/usr/games" exec-path)))
13 (executable-find "gnuchess"))
14 "The path to the gnuchess executable."
16 :group 'chess-gnuchess)
18 (defvar chess-gnuchess-bad-board nil)
19 (make-variable-buffer-local 'chess-gnuchess-bad-board)
21 (defvar chess-gnuchess-regexp-alist
23 (cons (concat "My move is : \\(" chess-algebraic-regexp "\\)")
26 (funcall chess-engine-response-handler 'move
27 (chess-engine-convert-algebraic (match-string 1) t)))))
31 (chess-error 'illegal-move))))
32 (cons "Board is wrong!"
35 ;; gnuchess didn't like the given position, which
36 ;; means it won't play against it unless we send a
37 ;; "go" after the user's move
38 (setq chess-gnuchess-bad-board t))))))
40 (defun chess-gnuchess-handler (game event &rest args)
41 (unless chess-engine-handling-event
43 ((eq event 'initialize)
44 (let ((proc (chess-common-handler game 'initialize "gnuchess")))
45 (when (and proc (processp proc)
46 (eq (process-status proc) 'run))
47 (process-send-string proc "nopost\n")
48 (setq chess-engine-process proc
49 chess-engine-opponent-name "GnuChess")
52 ((eq event 'setup-pos)
53 (let ((file (chess-with-temp-file
54 (insert (chess-pos-to-string (car args)) ?\n))))
55 (chess-engine-send nil (format "epdload %s\n" file))))
57 ((eq event 'setup-game)
58 (if (zerop (chess-game-index (car args)))
59 (chess-gnuchess-handler game 'setup-pos (chess-game-pos game 0))
60 (let ((file (chess-with-temp-file
61 (insert (chess-game-to-string (car args)) ?\n))))
62 (chess-engine-send nil (format "pgnload %s\n" file)))))
65 (chess-engine-send nil (concat (if (chess-pos-side-to-move
66 (chess-engine-position nil))
69 (chess-engine-send nil "go\n")
70 (setq chess-gnuchess-bad-board nil))
73 (chess-common-handler game 'move (car args))
74 (when chess-gnuchess-bad-board
75 (chess-engine-send nil "go\n")
76 (setq chess-gnuchess-bad-board nil)))
79 (apply 'chess-common-handler game event args)))))
81 (provide 'chess-gnuchess)
83 ;;; chess-gnuchess.el ends here