1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Play against an opponent over the network
7 (require 'chess-engine)
9 (require 'chess-algebraic)
11 (defvar chess-network-regexp-alist
13 (cons (concat chess-algebraic-regexp "$")
16 (funcall chess-engine-response-handler 'move
17 (chess-engine-convert-algebraic (match-string 0))))))
18 (cons "chess match\\(\\s-+\\(.+\\)\\)?$"
21 (funcall chess-engine-response-handler 'match
23 (cons "fen\\s-+\\(.+\\)"
26 (funcall chess-engine-response-handler 'setup-pos
27 (chess-engine-convert-fen (match-string 1))))))
28 (cons "pgn\\s-+\\(.+\\)"
31 (funcall chess-engine-response-handler 'setup-game
32 (chess-engine-convert-pgn (match-string 1))))))
36 (funcall chess-engine-response-handler 'pass))))
40 (funcall chess-engine-response-handler 'quit))))
44 (funcall chess-engine-response-handler 'resign))))
48 (funcall chess-engine-response-handler 'draw))))
52 (funcall chess-engine-response-handler 'abort))))
53 (cons "takeback\\s-+\\([0-9]+\\)$"
56 (funcall chess-engine-response-handler 'undo
57 (string-to-int (match-string 1))))))
58 (cons "accept\\(\\s-+\\(.+\\)\\)?$"
61 (funcall chess-engine-response-handler 'accept
66 (funcall chess-engine-response-handler 'decline))))
70 (funcall chess-engine-response-handler 'retract))))))
72 (defun chess-network-handler (event &rest args)
73 "Initialize the network chess engine."
75 ((eq event 'initialize)
76 (let ((which (read-char "Are you the c)lient or s)erver? "))
78 (message "Starting network client/server...")
79 (setq proc (if (eq which ?s)
80 (start-process "*chess-network*"
81 (current-buffer) "/usr/bin/nc"
82 "-l" "-p" (read-string "Port: "))
83 (open-network-stream "*chess-network*" (current-buffer)
84 (read-string "Host: ")
85 (read-string "Port: "))))
87 (message "Now waiting for your opponent to connect...")
88 (chess-network-handler 'match)
89 (message "You have connected; pass now or make your move."))
93 (chess-engine-send nil "quit\n"))
95 ((eq event 'setup-pos)
96 (chess-engine-send nil (format "fen %s\n"
97 (chess-pos-to-string (car args)))))
99 ((eq event 'setup-game)
100 (chess-engine-send nil (format "pgn %s\n"
101 (chess-game-to-string (car args)))))
104 (chess-engine-send nil "pass\n"))
107 (chess-engine-send nil "playing\n"))
110 (setq chess-engine-pending-offer 'match)
111 (chess-engine-send nil (format "chess match %s\n" chess-full-name)))
114 (chess-engine-send nil "resign\n")
115 (chess-game-set-data chess-engine-game 'active nil))
118 (if chess-engine-pending-offer
119 (chess-engine-command nil 'retract))
120 (setq chess-engine-pending-offer 'draw)
121 (chess-engine-send nil "draw\n"))
124 (if chess-engine-pending-offer
125 (chess-engine-command nil 'retract))
126 (setq chess-engine-pending-offer 'abort)
127 (chess-engine-send nil "abort\n"))
130 (if chess-engine-pending-offer
131 (chess-engine-command nil 'retract))
132 (setq chess-engine-pending-offer 'undo
133 chess-engine-pending-arg (car args))
134 (chess-engine-send nil (format "takeback %d\n" (car args))))
137 (chess-engine-send nil "accept\n"))
140 (chess-engine-send nil "decline\n"))
143 (chess-engine-send nil "retract\n"))
146 (chess-engine-send nil "illegal\n"))
149 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args)) "\n"))
150 (if (chess-game-over-p chess-engine-game)
151 (chess-game-set-data chess-engine-game 'active nil)))))
153 (provide 'chess-network)
155 ;;; chess-network.el ends here