1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Play against an opponent over the network
6 (require 'chess-engine)
8 (require 'chess-algebraic)
10 (defvar chess-network-regexp-alist
12 (cons (concat chess-algebraic-regexp "$")
15 (funcall chess-engine-response-handler 'move
16 (chess-engine-convert-algebraic (match-string 0))))))
17 (cons "chess match\\(\\s-+\\(.+\\)\\)?$"
20 (funcall chess-engine-response-handler 'match
22 (cons "fen\\s-+\\(.+\\)"
25 (funcall chess-engine-response-handler 'setup-pos
26 (chess-engine-convert-fen (match-string 1))))))
27 (cons "pgn\\s-+\\(.+\\)"
30 (funcall chess-engine-response-handler 'setup-game
31 (chess-engine-convert-pgn (match-string 1))))))
35 (funcall chess-engine-response-handler 'pass))))
39 (funcall chess-engine-response-handler 'quit))))
43 (funcall chess-engine-response-handler 'resign))))
47 (funcall chess-engine-response-handler 'draw))))
51 (funcall chess-engine-response-handler 'abort))))
52 (cons "takeback\\s-+\\([0-9]+\\)$"
55 (funcall chess-engine-response-handler 'undo
56 (string-to-int (match-string 1))))))
57 (cons "accept\\(\\s-+\\(.+\\)\\)?$"
60 (funcall chess-engine-response-handler 'accept
65 (funcall chess-engine-response-handler 'decline))))
69 (funcall chess-engine-response-handler 'retract))))))
71 (chess-message-catalog 'english
72 '((network-starting . "Starting network client/server...")
73 (network-waiting . "Now waiting for your opponent to connect...")
74 (network-connected ."You have connected; pass now or make your move.")))
76 (defun chess-network-handler (game event &rest args)
77 "Initialize the network chess engine."
78 (unless chess-engine-handling-event
80 ((eq event 'initialize)
81 (let ((which (read-char "Are you the c)lient or s)erver? "))
83 (chess-message 'network-starting)
84 (setq proc (if (eq which ?s)
85 (start-process "*chess-network*"
86 (current-buffer) "/usr/bin/nc"
87 "-l" "-p" (read-string "Port: "))
88 (open-network-stream "*chess-network*" (current-buffer)
89 (read-string "Host: ")
90 (read-string "Port: "))))
92 (chess-message 'network-waiting)
93 (chess-network-handler 'match)
94 (chess-message 'network-connected))
98 (chess-engine-send nil "quit\n"))
100 ((eq event 'setup-pos)
101 (chess-engine-send nil (format "fen %s\n"
102 (chess-pos-to-string (car args)))))
104 ((eq event 'setup-game)
105 (chess-engine-send nil (format "pgn %s\n"
106 (chess-game-to-string (car args)))))
109 (chess-engine-send nil "pass\n"))
112 (chess-engine-send nil "playing\n"))
115 (setq chess-engine-pending-offer 'match)
116 (chess-engine-send nil (format "chess match %s\n" chess-full-name)))
119 (chess-engine-send nil "resign\n")
120 (chess-game-set-data game 'active nil))
123 (if chess-engine-pending-offer
124 (chess-engine-command nil 'retract))
125 (setq chess-engine-pending-offer 'draw)
126 (chess-engine-send nil "draw\n"))
129 (if chess-engine-pending-offer
130 (chess-engine-command nil 'retract))
131 (setq chess-engine-pending-offer 'abort)
132 (chess-engine-send nil "abort\n"))
135 (if chess-engine-pending-offer
136 (chess-engine-command nil 'retract))
137 (setq chess-engine-pending-offer 'undo
138 chess-engine-pending-arg (car args))
139 (chess-engine-send nil (format "takeback %d\n" (car args))))
142 (chess-engine-send nil "accept\n"))
145 (chess-engine-send nil "decline\n"))
148 (chess-engine-send nil "retract\n"))
151 (chess-engine-send nil "illegal\n"))
154 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args)) "\n"))
155 (if (chess-game-over-p game)
156 (chess-game-set-data game 'active nil))))))
158 (provide 'chess-network)
160 ;;; chess-network.el ends here