summaryrefslogtreecommitdiff
path: root/chess-network.el
blob: 3468abd674f89f487ab552b5cea73cfcd7be0958 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Play against an opponent over the network
;;
;; $Revision$

(require 'chess-engine)
(require 'chess-fen)
(require 'chess-algebraic)

(defvar chess-network-regexp-alist
  (list
   (cons (concat chess-algebraic-regexp "$")
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'move
		     (chess-engine-convert-algebraic (match-string 0))))))
   (cons "chess match\\(\\s-+\\(.+\\)\\)?$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'connect
		     (match-string 2)))))
   (cons "accept match\\(\\s-+\\(.+\\)\\)?$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'accept-connect
		     (match-string 2)))))
   (cons "fen\\s-+\\(.+\\)"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'setup-pos
		     (chess-engine-convert-fen (match-string 1))))))
   (cons "pgn\\s-+\\(.+\\)"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'setup-game
		     (chess-engine-convert-pgn (match-string 1))))))
   (cons "pass$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'pass))))
   (cons "quit$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'quit))))
   (cons "resign$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'resign))))
   (cons "draw$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'draw))))
   (cons "accept draw$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'accept-draw))))
   (cons "decline draw$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'decline-draw))))
   (cons "abort$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'abort))))
   (cons "accept abort$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'accept-abort))))
   (cons "decline abort$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'decline-abort))))
   (cons "takeback\\s-+\\([0-9]+\\)$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'undo
		     (string-to-int (match-string 1))))))
   (cons "accept takeback\\s-+\\([0-9]+\\)$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'accept-undo
		     (string-to-int (match-string 1))))))
   (cons "decline takeback$"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'decline-undo))))))

(defun chess-network-handler (event &rest args)
  "Initialize the network chess engine."
  (cond
   ((eq event 'initialize)
    (let ((which (read-char "Are you the c)lient or s)erver? "))
	  proc)
      (message "Starting network client/server...")
      (setq proc (if (eq which ?s)
		     (start-process "*chess-network*"
				    (current-buffer) "/usr/bin/nc"
				    "-l" "-p" (read-string "Port: "))
		   (open-network-stream "*chess-network*" (current-buffer)
					(read-string "Host: ")
					(read-string "Port: "))))
      (if (eq which ?s)
	  (message "Now waiting for your opponent to connect...")
	(process-send-string proc (format "chess match %s\n" chess-full-name))
	(message "You have connected; pass now or make your move."))
      proc))

   ((eq event 'shutdown)
    (chess-engine-send nil "quit\n"))

   ((eq event 'setup-pos)
    (chess-engine-send nil (format "fen %s\n"
				   (chess-pos-to-string (car args)))))

   ((eq event 'setup-game)
    (chess-engine-send nil (format "pgn %s\n"
				   (chess-game-to-string (car args)))))

   ((eq event 'pass)
    (chess-engine-send nil "pass\n"))

   ((eq event 'busy)
    (chess-engine-send nil "playing\n"))

   ((eq event 'connect)
    (chess-engine-send nil (format "chess match %s\n" chess-full-name)))

   ((eq event 'accept-connect)
    (chess-engine-send nil (format "accept match %s\n" chess-full-name)))

   ((eq event 'decline)
    (chess-engine-send nil "decline\n"))

   ((eq event 'resign)
    (chess-engine-send nil "resign\n"))

   ((eq event 'draw)
    (chess-engine-send nil "draw\n"))

   ((eq event 'accept-draw)
    (chess-engine-send nil "accept draw\n"))

   ((eq event 'decline-draw)
    (chess-engine-send nil "decline draw\n"))

   ((eq event 'abort)
    (chess-engine-send nil "abort\n"))

   ((eq event 'accept-abort)
    (chess-engine-send nil "accept abort\n"))

   ((eq event 'decline-abort)
    (chess-engine-send nil "decline abort\n"))

   ((eq event 'undo)
    (chess-engine-send nil (format "takeback %d\n" (car args))))

   ((eq event 'accept-undo)
    (chess-engine-send nil (format "accept takeback %d\n" (car args))))

   ((eq event 'decline-undo)
    (chess-engine-send nil "decline takeback\n"))

   ((eq event 'move)
    (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
				   "\n")))))

(provide 'chess-network)

;;; chess-network.el ends here