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 ;; jww (2002-04-07): Set the appropriate Black or
24 ;; White tag at this point
25 (message "Your opponent, %s, has connected"
27 (cons "fen\\s-+\\(.+\\)"
30 (let* ((position (chess-fen-to-pos (match-string 1)))
31 (ply (chess-ply-create position)))
32 (chess-game-set-plies (chess-engine-game nil)
37 (message "Your opponent has quit playing"))))))
39 (defun chess-network-perform-move ()
40 (let ((move (match-string 1))
41 (ply (chess-algebraic-to-ply (chess-engine-position nil) move)))
43 (error "Could not convert engine move: %s" move))
44 (let ((chess-network-now-moving t))
45 (funcall chess-engine-response-handler 'move ply))))
47 (defun chess-network-handler (event &rest args)
48 "Initialize the network chess engine."
50 ((eq event 'initialize)
51 (let ((which (read-char "Are you the c)lient or s)erver? "))
53 (message "Starting network client/server...")
54 (setq proc (if (eq which ?s)
55 (start-process "*chess-network*"
56 (current-buffer) "/usr/bin/nc"
57 "-l" "-p" (read-string "Port: "))
58 (open-network-stream "*chess-network*" (current-buffer)
59 (read-string "Host: ")
60 (read-string "Port: "))))
62 (message "Now waiting for your opponent to connect...")
63 (process-send-string proc (format "name %s\n" (user-full-name)))
64 (message "You have connected; pass now or make your move."))
69 (chess-engine-send nil "quit\n")))
72 (chess-engine-send nil (format "fen %s\n"
73 (chess-pos-to-fen (car args)))))
76 (chess-engine-send nil "pass\n"))
79 (unless chess-network-now-moving
80 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
83 (provide 'chess-network)
85 ;;; chess-network.el ends here