]> code.delx.au - gnu-emacs-elpa/blob - chess-crafty.el
Added support for network play
[gnu-emacs-elpa] / chess-crafty.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Play against crafty!
4 ;;
5 ;; $Revision$
6
7 (require 'chess-engine)
8 (require 'chess-fen)
9 (require 'chess-algebraic)
10
11 (defvar chess-crafty-now-moving nil)
12
13 (defvar chess-crafty-regexp-alist
14 (list (cons
15 (concat "\\s-*\\(White\\|Black\\)\\s-*([0-9]+):\\s-+\\("
16 chess-algebraic-regexp "\\)\\s-*$")
17 'chess-crafty-perform-move)
18 (cons "Illegal move:\\s-*\\(.*\\)"
19 (function
20 (lambda ()
21 (signal 'chess-illegal (match-string 1)))))))
22
23 (defun chess-crafty-perform-move ()
24 (let ((position (chess-engine-position nil))
25 (move (match-string 2)) ply)
26 (when (string= (if (chess-pos-side-to-move position)
27 "White" "Black")
28 (match-string 1))
29 (setq ply (chess-algebraic-to-ply position move))
30 (unless ply
31 (error "Could not convert engine move: %s" move))
32 (let ((chess-crafty-now-moving t))
33 (funcall chess-engine-response-handler 'move ply)))))
34
35 (defun chess-crafty-handler (event &rest args)
36 (cond
37 ((eq event 'initialize)
38 (let (proc)
39 (message "Starting chess program 'crafty'...")
40 (setq proc (start-process "chess-process" (current-buffer)
41 (or (executable-find "crafty")
42 (executable-find "wcrafty"))))
43 (message "Starting chess program 'crafty'...done")
44 (process-send-string proc (concat "display nogeneral\n"
45 "display nochanges\n"
46 "display noextstats\n"
47 "display nohashstats\n"
48 "display nomoves\n"
49 "display nonodes\n"
50 "display noply1\n"
51 "display nostats\n"
52 "display notime\n"
53 "display novariation\n"
54 "alarm off\n"
55 "ansi off\n"))
56 proc))
57
58 ((eq event 'shutdown)
59 (chess-engine-send nil "quit\n"))
60
61 ((eq event 'setup)
62 (chess-engine-send nil (format "setboard %s\n"
63 (chess-pos-to-fen (car args)))))
64
65 ((eq event 'pass)
66 (chess-engine-send nil "go\n"))
67
68 ((eq event 'move)
69 (unless chess-crafty-now-moving
70 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
71 "\n"))))))
72
73 (provide 'chess-crafty)
74
75 ;;; chess-crafty.el ends here