1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Play against an opponent over the network
7 (require 'chess-engine)
9 (require 'chess-algebraic)
11 (defvar chess-network-now-moving nil)
13 (defvar chess-network-regexp-alist
14 (list (cons chess-algebraic-regexp
15 'chess-network-perform-move)
19 (message "Your opponent has passed the move to you"))))
20 (cons "name\\s-+\\(.+\\)"
23 (funcall chess-engine-response-handler 'connect
25 (cons "fen\\s-+\\(.+\\)"
28 (let* ((position (chess-fen-to-pos (match-string 1)))
29 (ply (chess-ply-create position)))
30 (chess-game-set-plies (chess-engine-game nil)
35 (message "Your opponent has quit playing"))))))
37 (defun chess-network-perform-move ()
38 (let* ((move (match-string 1))
39 (ply (chess-algebraic-to-ply (chess-engine-position nil) move)))
41 (error "Could not convert engine move: %s" move))
42 (let ((chess-network-now-moving t))
43 (funcall chess-engine-response-handler 'move ply))))
45 (defun chess-network-handler (event &rest args)
46 "Initialize the network chess engine."
48 ((eq event 'initialize)
49 (let ((which (read-char "Are you the c)lient or s)erver? "))
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: "))))
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."))
67 (chess-engine-send nil "quit\n")))
70 (chess-engine-send nil (format "fen %s\n"
71 (chess-pos-to-fen (car args)))))
74 (chess-engine-send nil "pass\n"))
77 (unless chess-network-now-moving
78 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
81 (provide 'chess-network)
83 ;;; chess-network.el ends here