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)
36 (defun chess-network-perform-move ()
37 (let ((position (chess-engine-position nil))
38 (move (match-string 2)) ply)
39 (when (string= (if (chess-pos-side-to-move position)
42 (setq ply (chess-algebraic-to-ply position move))
44 (error "Could not convert engine move: %s" move))
45 (let ((chess-network-now-moving t))
46 (funcall chess-engine-response-handler 'move ply)))))
48 (defun chess-network-handler (event &rest args)
49 "Initialize the network chess engine."
51 ((eq event 'initialize)
52 (let ((which (read-char "cAre you the c)lient or s)erver? "))
54 (message "Starting network client/server...")
55 (setq proc (if (eq which ?s)
56 (start-process "*chess-network*"
57 (current-buffer) "/usr/bin/nc"
58 "-l" "-p" (read-string "Port: "))
59 (open-network-stream "*chess-network*" (current-buffer)
60 (read-string "Host: ")
61 (read-string "Port: "))))
63 (message "Now waiting for your opponent to connect...")
64 (process-send-string proc (format "CONNECT %s\n" (user-full-name)))
65 (message "You have connected; pass now or make your move."))
69 (chess-engine-send nil "QUIT\n"))
72 (chess-engine-send nil (format "SETBOARD %s\n"
73 (chess-pos-to-fen (car args)))))
76 (chess-engine-send nil "PASS\n"))
79 (unless chess-network-now-moving
81 nil (concat (if (chess-pos-side-to-move (chess-ply-pos (car args)))
84 (chess-ply-to-algebraic (car args))
87 (provide 'chess-network)
89 ;;; chess-network.el ends here