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 (concat "\\(Black\\|White\\):\\s-*"
15 chess-algebraic-regexp "\\s-+")
16 'chess-network-perform-move)
20 (message "Your opponent has passed the move to you"))))
21 (cons "CONNECT\\s-+\\(.*\\)"
24 ;; jww (2002-04-07): Set the appropriate Black or
25 ;; White tag at this point
26 (message "Your opponent, %s, has connected"
28 (cons "SETBOARD\\s-+\\(.*\\)"
31 (let* ((position (chess-fen-to-pos (match-string 1)))
32 (ply (chess-ply-create position)))
33 (chess-game-set-plies (chess-engine-game nil)
38 (message "Your opponent has quit playing"))))))
40 (defun chess-network-perform-move ()
41 (let ((position (chess-engine-position nil))
42 (move (match-string 2)) ply)
43 (when (string= (if (chess-pos-side-to-move position)
46 (setq ply (chess-algebraic-to-ply position move))
48 (error "Could not convert engine move: %s" move))
49 (let ((chess-network-now-moving t))
50 (funcall chess-engine-response-handler 'move ply)))))
52 (defun chess-network-handler (event &rest args)
53 "Initialize the network chess engine."
55 ((eq event 'initialize)
56 (let ((which (read-char "Are you the c)lient or s)erver? "))
58 (message "Starting network client/server...")
59 (setq proc (if (eq which ?s)
60 (start-process "*chess-network*"
61 (current-buffer) "/usr/bin/nc"
62 "-l" "-p" (read-string "Port: "))
63 (open-network-stream "*chess-network*" (current-buffer)
64 (read-string "Host: ")
65 (read-string "Port: "))))
67 (message "Now waiting for your opponent to connect...")
68 (process-send-string proc (format "CONNECT %s\n" (user-full-name)))
69 (message "You have connected; pass now or make your move."))
73 (chess-engine-send nil "QUIT\n"))
76 (chess-engine-send nil (format "SETBOARD %s\n"
77 (chess-pos-to-fen (car args)))))
80 (chess-engine-send nil "PASS\n"))
83 (unless chess-network-now-moving
85 nil (concat (if (chess-pos-side-to-move (chess-ply-pos (car args)))
88 (chess-ply-to-algebraic (car args))
91 (provide 'chess-network)
93 ;;; chess-network.el ends here