]> code.delx.au - gnu-emacs-elpa/blob - chess-gnuchess.el
*** no comment ***
[gnu-emacs-elpa] / chess-gnuchess.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Play against gnuchess!
4 ;;
5 ;; $Revision$
6
7 (require 'chess-engine)
8 (require 'chess-common)
9
10 (defgroup chess-gnuchess nil
11 "The publically available chess engine 'gnuchess'."
12 :group 'chess-engine)
13
14 (defcustom chess-gnuchess-path (executable-find "gnuchess")
15 "The path to the gnuchess executable."
16 :type 'file
17 :group 'chess-gnuchess)
18
19 (defvar chess-gnuchess-bad-board nil)
20 (make-variable-buffer-local 'chess-gnuchess-bad-board)
21
22 (defvar chess-gnuchess-regexp-alist
23 (list
24 (cons (concat "My move is : \\(" chess-algebraic-regexp "\\)")
25 (function
26 (lambda ()
27 (funcall chess-engine-response-handler 'move
28 (chess-engine-convert-algebraic (match-string 1) t)))))
29 (cons "Illegal move:"
30 (function
31 (lambda ()
32 (chess-error 'illegal-move))))
33 (cons "Board is wrong!"
34 (function
35 (lambda ()
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))))))
40
41 (defun chess-gnuchess-handler (game event &rest args)
42 (unless chess-engine-handling-event
43 (cond
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")
49 t)))
50
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))))
55
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))))
60
61 ((eq event 'pass)
62 (chess-engine-send nil (concat (if (chess-pos-side-to-move
63 (chess-engine-position nil))
64 "white" "black")
65 "\n"))
66 (chess-engine-send nil "go\n")
67 (setq chess-gnuchess-bad-board nil))
68
69 ((eq event 'move)
70 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
71 "\n"))
72 (when chess-gnuchess-bad-board
73 (chess-engine-send nil "go\n")
74 (setq chess-gnuchess-bad-board nil)))
75
76 (t
77 (apply 'chess-common-handler game event args)))))
78
79 (provide 'chess-gnuchess)
80
81 ;;; chess-gnuchess.el ends here