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