6 agent_timeout = -1.0 # Timeout in seconds for AI players to make moves
7 # WARNING: Won't work for windows based operating systems
9 if platform.system() == "Windows":
10 agent_timeout = -1 # Hence this
12 # A player who can't play
14 def __init__(self, name, colour):
18 def update(self, result):
21 def reset_board(self, s):
25 return self.name + "<"+str(self.colour)+">"
27 def base_player(self):
32 def open_fifo(name, mode, timeout=None):
34 return open(name, mode)
37 class Worker(threading.Thread):
39 threading.Thread.__init__(self)
46 self.result = open(name, mode)
56 while time.time() - start < timeout:
57 if w.is_alive() == False:
59 if w.exception != None:
66 #sys.stderr.write("FIFO_TIMEOUT!\n")
67 # Recursive to deal with possible race condition
70 f = open_fifo(name, "w", 1)
72 f = open_fifo(name, "r", 1)
76 #sys.stderr.write("Opened other end!\n")
83 raise Exception("FIFO_TIMEOUT")
86 if w.exception != None:
91 # Player that runs through a fifo
92 class FifoPlayer(Player):
96 def __init__(self, name, colour):
97 Player.__init__(self, name, colour)
98 os.mkfifo(self.name+".in")
99 os.mkfifo(self.name+".out")
105 def update(self, result):
106 sys.stderr.write("update fifo called\n")
108 self.fifo_out = open_fifo(self.name+".out", "w", FifoPlayer.timeout)
110 raise Exception("FIFO_TIMEOUT")
112 self.fifo_out.write(result +"\n")
113 self.fifo_out.close()
117 sys.stderr.write("select fifo called\n")
119 self.fifo_out = open_fifo(self.name+".out", "w", FifoPlayer.timeout)
121 #sys.stderr.write("TIMEOUT\n")
122 raise Exception("FIFO_TIMEOUT")
125 self.fifo_out.write("SELECT?\n")
126 self.fifo_out.close()
127 self.fifo_in = open_fifo(self.name+".in", "r", FifoPlayer.timeout)
128 s = map(int, self.fifo_in.readline().strip(" \r\n").split(" "))
133 sys.stderr.write("get_move fifo called\n")
135 self.fifo_out = open_fifo(self.name+".out", "w", FifoPlayer.timeout)
137 raise Exception("FIFO_TIMEOUT")
139 self.fifo_out.write("MOVE?\n")
140 self.fifo_out.close()
141 self.fifo_in = open_fifo(self.name+".in", "r", FifoPlayer.timeout)
142 s = map(int, self.fifo_in.readline().strip(" \r\n").split(" "))
146 def quit(self, result):
148 self.fifo_out = open_fifo(self.name+".out", "w", FifoPlayer.timeout)
150 os.remove(self.name+".in")
151 os.remove(self.name+".out")
152 #raise Exception("FIFO_TIMEOUT")
155 self.fifo_out.write(result + "\n")
156 self.fifo_out.close()
157 os.remove(self.name+".in")
158 os.remove(self.name+".out")
160 # Player that runs from another process
161 class ExternalAgent(Player):
164 def __init__(self, name, colour):
165 Player.__init__(self, name, colour)
166 self.p = subprocess.Popen(name,bufsize=0,stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True,universal_newlines=True)
168 self.send_message(colour)
170 def send_message(self, s):
171 if agent_timeout > 0.0:
172 ready = select.select([], [self.p.stdin], [], agent_timeout)[1]
174 ready = [self.p.stdin]
175 if self.p.stdin in ready:
176 #sys.stderr.write("Writing \'" + s + "\' to " + str(self.p) + "\n")
178 self.p.stdin.write(s + "\n")
180 raise Exception("UNRESPONSIVE")
182 raise Exception("TIMEOUT")
184 def get_response(self):
185 if agent_timeout > 0.0:
186 ready = select.select([self.p.stdout], [], [], agent_timeout)[0]
188 ready = [self.p.stdout]
189 if self.p.stdout in ready:
190 #sys.stderr.write("Reading from " + str(self.p) + " 's stdout...\n")
192 result = self.p.stdout.readline().strip(" \t\r\n")
193 #sys.stderr.write("Read \'" + result + "\' from " + str(self.p) + "\n")
195 except: # Exception, e:
196 raise Exception("UNRESPONSIVE")
198 raise Exception("TIMEOUT")
202 self.send_message("SELECTION?")
203 line = self.get_response()
206 m = re.match("\s*(\d+)\s+(\d+)\s*", line)
207 result = map(int, [m.group(1), m.group(2)])
209 raise Exception("GIBBERISH \"" + str(line) + "\"")
212 def update(self, result):
213 #print "Update " + str(result) + " called for AgentPlayer"
214 self.send_message(result)
219 self.send_message("MOVE?")
220 line = self.get_response()
223 m = re.match("\s*(\d+)\s+(\d+)\s*", line)
224 result = map(int, [m.group(1), m.group(2)])
227 raise Exception("GIBBERISH \"" + str(line) + "\"")
230 def reset_board(self, s):
231 self.send_message("BOARD")
232 for line in s.split("\n"):
233 self.send_message(line.strip(" \r\n"))
234 self.send_message("END BOARD")
236 def quit(self, final_result):
238 self.send_message("QUIT " + final_result)
242 # So you want to be a player here?
243 class HumanPlayer(Player):
244 def __init__(self, name, colour):
245 Player.__init__(self, name, colour)
247 # Select your preferred account
249 if isinstance(graphics, GraphicsThread):
250 # Basically, we let the graphics thread do some shit and then return that information to the game thread
251 graphics.cond.acquire()
252 # We wait for the graphics thread to select a piece
253 while graphics.stopped() == False and graphics.state["select"] == None:
254 graphics.cond.wait() # The difference between humans and machines is that humans sleep
255 select = graphics.state["select"]
258 graphics.cond.release()
259 if graphics.stopped():
261 return [select.x, select.y]
263 # Since I don't display the board in this case, I'm not sure why I filled it in...
265 sys.stdout.write("SELECTION?\n")
267 p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
269 sys.stderr.write("ILLEGAL GIBBERISH\n")
271 # It's your move captain
273 if isinstance(graphics, GraphicsThread):
274 graphics.cond.acquire()
275 while graphics.stopped() == False and graphics.state["dest"] == None:
277 graphics.cond.release()
279 return graphics.state["dest"]
283 sys.stdout.write("MOVE?\n")
285 p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
287 sys.stderr.write("ILLEGAL GIBBERISH\n")
290 # Are you sure you want to quit?
291 def quit(self, final_result):
293 sys.stdout.write("QUIT " + final_result + "\n")
295 # Completely useless function
296 def update(self, result):
297 if isinstance(graphics, GraphicsThread):
300 sys.stdout.write(result + "\n")
304 # Default internal player (makes random moves)
305 class InternalAgent(Player):
306 def __init__(self, name, colour):
307 Player.__init__(self, name, colour)
310 self.board = Board(style = "agent")
314 def update(self, result):
316 self.board.update(result)
320 def reset_board(self, s):
321 self.board.reset_board(s)
323 def quit(self, final_result):
326 class AgentRandom(InternalAgent):
327 def __init__(self, name, colour):
328 InternalAgent.__init__(self, name, colour)
332 self.choice = self.board.pieces[self.colour][random.randint(0, len(self.board.pieces[self.colour])-1)]
334 # Check that the piece has some possibility to move
335 tmp = self.choice.current_type
336 if tmp == "unknown": # For unknown pieces, try both types
337 for t in self.choice.types:
340 self.choice.current_type = t
341 all_moves += self.board.possible_moves(self.choice)
343 all_moves = self.board.possible_moves(self.choice)
344 self.choice.current_type = tmp
345 if len(all_moves) > 0:
347 return [self.choice.x, self.choice.y]
350 moves = self.board.possible_moves(self.choice)
351 move = moves[random.randint(0, len(moves)-1)]
355 # Terrible, terrible hacks
357 def run_agent(agent):
358 #sys.stderr.write(sys.argv[0] + " : Running agent " + str(agent) + "\n")
360 line = sys.stdin.readline().strip(" \r\n")
361 if line == "SELECTION?":
362 #sys.stderr.write(sys.argv[0] + " : Make selection\n")
363 [x,y] = agent.select() # Gets your agent's selection
364 #sys.stderr.write(sys.argv[0] + " : Selection was " + str(agent.choice) + "\n")
365 sys.stdout.write(str(x) + " " + str(y) + "\n")
366 elif line == "MOVE?":
367 #sys.stderr.write(sys.argv[0] + " : Make move\n")
368 [x,y] = agent.get_move() # Gets your agent's move
369 sys.stdout.write(str(x) + " " + str(y) + "\n")
370 elif line.split(" ")[0] == "QUIT":
371 #sys.stderr.write(sys.argv[0] + " : Quitting\n")
372 agent.quit(" ".join(line.split(" ")[1:])) # Quits the game
374 elif line.split(" ")[0] == "BOARD":
376 line = sys.stdin.readline().strip(" \r\n")
377 while line != "END BOARD":
379 line = sys.stdin.readline().strip(" \r\n")
380 agent.board.reset_board(s)
383 agent.update(line) # Updates agent.board
389 class ExternalWrapper(ExternalAgent):
390 def __init__(self, agent):
391 run = "python -u -c \"import sys;import os;from qchess import *;agent = " + agent.__class__.__name__ + "('" + agent.name + "','"+agent.colour+"');sys.stdin.readline();sys.exit(run_agent(agent))\""
393 ExternalAgent.__init__(self, run, agent.colour)