]> 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 "pass$"
18 (function
19 (lambda ()
20 (funcall chess-engine-response-handler 'pass))))
21 (cons "name\\s-+\\(.+\\)"
22 (function
23 (lambda ()
24 (funcall chess-engine-response-handler 'connect
25 (match-string 1)))))
26 (cons "fen\\s-+\\(.+\\)"
27 (function
28 (lambda ()
29 (funcall chess-engine-response-handler 'setup
30 (match-string 1)))))
31 (cons "quit$"
32 (function
33 (lambda ()
34 (funcall chess-engine-response-handler 'quit))))
35 (cons "resign$"
36 (function
37 (lambda ()
38 (funcall chess-engine-response-handler 'resign))))))
39
40 (defun chess-network-handler (event &rest args)
41 "Initialize the network chess engine."
42 (cond
43 ((eq event 'initialize)
44 (let ((which (read-char "Are you the c)lient or s)erver? "))
45 proc)
46 (message "Starting network client/server...")
47 (setq proc (if (eq which ?s)
48 (start-process "*chess-network*"
49 (current-buffer) "/usr/bin/nc"
50 "-l" "-p" (read-string "Port: "))
51 (open-network-stream "*chess-network*" (current-buffer)
52 (read-string "Host: ")
53 (read-string "Port: "))))
54 (if (eq which ?s)
55 (message "Now waiting for your opponent to connect...")
56 (process-send-string proc (format "name %s\n" (user-full-name)))
57 (message "You have connected; pass now or make your move."))
58 proc))
59
60 ((eq event 'shutdown)
61 (ignore-errors
62 (chess-engine-send nil "quit\n")))
63
64 ((eq event 'setup)
65 (chess-engine-send nil (format "fen %s\n"
66 (chess-pos-to-fen (car args)))))
67
68 ((eq event 'pass)
69 (chess-engine-send nil "pass\n"))
70
71 ((eq event 'resign)
72 (chess-engine-send nil "resign\n"))
73
74 ((eq event 'move)
75 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
76 "\n")))))
77
78 (provide 'chess-network)
79
80 ;;; chess-network.el ends here