]> code.delx.au - gnu-emacs-elpa/blob - chess-network.el
*** no comment ***
[gnu-emacs-elpa] / chess-network.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Play against an opponent over the network
4 ;;
5 ;; $Revision$
6
7 (require 'chess-engine)
8 (require 'chess-fen)
9 (require 'chess-algebraic)
10
11 (defvar chess-network-now-moving nil)
12
13 (defvar chess-network-regexp-alist
14 (list (cons chess-algebraic-regexp
15 'chess-network-perform-move)
16 (cons "pass"
17 (function
18 (lambda ()
19 (message "Your opponent has passed the move to you"))))
20 (cons "name\\s-+\\(.+\\)"
21 (function
22 (lambda ()
23 ;; jww (2002-04-07): Set the appropriate Black or
24 ;; White tag at this point
25 (message "Your opponent, %s, has connected"
26 (match-string 1)))))
27 (cons "fen\\s-+\\(.+\\)"
28 (function
29 (lambda ()
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)
33 (list ply))))))
34 (cons "quit"
35 (function
36 (lambda ()
37 (message "Your opponent has quit playing"))))))
38
39 (defun chess-network-perform-move ()
40 (let ((move (match-string 1))
41 (ply (chess-algebraic-to-ply (chess-engine-position nil) move)))
42 (unless ply
43 (error "Could not convert engine move: %s" move))
44 (let ((chess-network-now-moving t))
45 (funcall chess-engine-response-handler 'move ply))))
46
47 (defun chess-network-handler (event &rest args)
48 "Initialize the network chess engine."
49 (cond
50 ((eq event 'initialize)
51 (let ((which (read-char "Are you the c)lient or s)erver? "))
52 proc)
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: "))))
61 (if (eq which ?s)
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."))
65 proc))
66
67 ((eq event 'shutdown)
68 (ignore-errors
69 (chess-engine-send nil "quit\n")))
70
71 ((eq event 'setup)
72 (chess-engine-send nil (format "fen %s\n"
73 (chess-pos-to-fen (car args)))))
74
75 ((eq event 'pass)
76 (chess-engine-send nil "pass\n"))
77
78 ((eq event 'move)
79 (unless chess-network-now-moving
80 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
81 "\n"))))))
82
83 (provide 'chess-network)
84
85 ;;; chess-network.el ends here