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 'connect
23 (cons "accept\\(\\s-+\\(.+\\)\\)?$"
26 (funcall chess-engine-response-handler 'accept
28 (cons "fen\\s-+\\(.+\\)"
31 (funcall chess-engine-response-handler 'setup-pos
32 (chess-engine-convert-fen (match-string 1))))))
33 (cons "pgn\\s-+\\(.+\\)"
36 (funcall chess-engine-response-handler 'setup-game
37 (chess-engine-convert-pgn (match-string 1))))))
41 (funcall chess-engine-response-handler 'pass))))
45 (funcall chess-engine-response-handler 'quit))))
49 (funcall chess-engine-response-handler 'resign))))
53 (funcall chess-engine-response-handler 'abort))))))
55 (defun chess-network-handler (event &rest args)
56 "Initialize the network chess engine."
58 ((eq event 'initialize)
59 (let ((which (read-char "Are you the c)lient or s)erver? "))
61 (message "Starting network client/server...")
62 (setq proc (if (eq which ?s)
63 (start-process "*chess-network*"
64 (current-buffer) "/usr/bin/nc"
65 "-l" "-p" (read-string "Port: "))
66 (open-network-stream "*chess-network*" (current-buffer)
67 (read-string "Host: ")
68 (read-string "Port: "))))
70 (message "Now waiting for your opponent to connect...")
71 (process-send-string proc (format "chess match %s\n" chess-full-name))
72 (message "You have connected; pass now or make your move."))
76 (chess-engine-send nil "quit\n"))
78 ((eq event 'setup-pos)
79 (chess-engine-send nil (format "fen %s\n"
80 (chess-pos-to-string (car args)))))
82 ((eq event 'setup-game)
83 (chess-engine-send nil (format "pgn %s\n"
84 (chess-game-to-string (car args)))))
87 (chess-engine-send nil "pass\n"))
90 (chess-engine-send nil "playing\n"))
93 (chess-engine-send nil (format "accept %s\n" chess-full-name)))
96 (chess-engine-send nil "decline\n"))
99 (chess-engine-send nil "resign\n"))
102 (chess-engine-send nil "abort\n"))
105 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
108 (provide 'chess-network)
110 ;;; chess-network.el ends here