]> code.delx.au - gnu-emacs-elpa/blob - chess-network.el
*** no comment ***
[gnu-emacs-elpa] / chess-network.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Play against an opponent over the network
4 ;;
5 ;; $Revision$
6
7 (require 'chess-engine)
8 (require 'chess-fen)
9 (require 'chess-algebraic)
10
11 (defvar chess-network-regexp-alist
12 (list (cons (concat chess-algebraic-regexp "$")
13 (function
14 (lambda ()
15 (funcall chess-engine-response-handler 'move
16 (match-string 0)))))
17 (cons "chess match\\(\\s-+\\(.+\\)\\)?$"
18 (function
19 (lambda ()
20 (funcall chess-engine-response-handler 'connect
21 (match-string 2)))))
22 (cons "accept\\(\\s-+\\(.+\\)\\)?$"
23 (function
24 (lambda ()
25 (funcall chess-engine-response-handler 'accept
26 (match-string 2)))))
27 (cons "fen\\s-+\\(.+\\)"
28 (function
29 (lambda ()
30 (funcall chess-engine-response-handler 'setup
31 (match-string 1)))))
32 (cons "pass$"
33 (function
34 (lambda ()
35 (funcall chess-engine-response-handler 'pass))))
36 (cons "quit$"
37 (function
38 (lambda ()
39 (funcall chess-engine-response-handler 'quit))))
40 (cons "resign$"
41 (function
42 (lambda ()
43 (funcall chess-engine-response-handler 'resign))))))
44
45 (defun chess-network-handler (event &rest args)
46 "Initialize the network chess engine."
47 (cond
48 ((eq event 'initialize)
49 (let ((which (read-char "Are you the c)lient or s)erver? "))
50 proc)
51 (message "Starting network client/server...")
52 (setq proc (if (eq which ?s)
53 (start-process "*chess-network*"
54 (current-buffer) "/usr/bin/nc"
55 "-l" "-p" (read-string "Port: "))
56 (open-network-stream "*chess-network*" (current-buffer)
57 (read-string "Host: ")
58 (read-string "Port: "))))
59 (if (eq which ?s)
60 (message "Now waiting for your opponent to connect...")
61 (process-send-string proc (format "name %s\n" (user-full-name)))
62 (message "You have connected; pass now or make your move."))
63 proc))
64
65 ((eq event 'shutdown)
66 (chess-engine-send nil "quit\n"))
67
68 ((eq event 'setup)
69 (chess-engine-send nil (format "fen %s\n"
70 (chess-pos-to-fen (car args)))))
71
72 ((eq event 'pass)
73 (chess-engine-send nil "pass\n"))
74
75 ((eq event 'resign)
76 (chess-engine-send nil "resign\n"))
77
78 ((eq event 'move)
79 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
80 "\n")))))
81
82 (provide 'chess-network)
83
84 ;;; chess-network.el ends here