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")
102 self.fifo_out = open_fifo(self.name+".out","w", FifoPlayer.timeout)
104 raise Exception("FIFO_TIMEOUT")
106 self.fifo_out.write("START "+colour+"\n")
107 self.fifo_out.close()
113 def update(self, result):
114 sys.stderr.write("update fifo called\n")
116 self.fifo_out = open_fifo(self.name+".out", "w", FifoPlayer.timeout)
118 raise Exception("FIFO_TIMEOUT")
120 self.fifo_out.write(result +"\n")
121 self.fifo_out.close()
125 sys.stderr.write("select fifo called\n")
127 self.fifo_out = open_fifo(self.name+".out", "w", FifoPlayer.timeout)
129 #sys.stderr.write("TIMEOUT\n")
130 raise Exception("FIFO_TIMEOUT")
133 self.fifo_out.write("SELECT?\n")
134 self.fifo_out.close()
135 self.fifo_in = open_fifo(self.name+".in", "r", FifoPlayer.timeout)
136 s = map(int, self.fifo_in.readline().strip(" \r\n").split(" "))
141 sys.stderr.write("get_move fifo called\n")
143 self.fifo_out = open_fifo(self.name+".out", "w", FifoPlayer.timeout)
145 raise Exception("FIFO_TIMEOUT")
147 self.fifo_out.write("MOVE?\n")
148 self.fifo_out.close()
149 self.fifo_in = open_fifo(self.name+".in", "r", FifoPlayer.timeout)
150 s = map(int, self.fifo_in.readline().strip(" \r\n").split(" "))
154 def quit(self, result):
156 self.fifo_out = open_fifo(self.name+".out", "w", FifoPlayer.timeout)
158 os.remove(self.name+".in")
159 os.remove(self.name+".out")
160 #raise Exception("FIFO_TIMEOUT")
163 self.fifo_out.write(result + "\n")
164 self.fifo_out.close()
165 os.remove(self.name+".in")
166 os.remove(self.name+".out")
168 # Player that runs from another process
169 class ExternalAgent(Player):
172 def __init__(self, name, colour):
173 Player.__init__(self, name, colour)
174 self.p = subprocess.Popen(name,bufsize=0,stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True,universal_newlines=True)
176 self.send_message(colour)
178 def send_message(self, s):
179 if agent_timeout > 0.0:
180 ready = select.select([], [self.p.stdin], [], agent_timeout)[1]
182 ready = [self.p.stdin]
183 if self.p.stdin in ready:
184 #sys.stderr.write("Writing \'" + s + "\' to " + str(self.p) + "\n")
186 self.p.stdin.write(s + "\n")
188 raise Exception("UNRESPONSIVE")
190 raise Exception("TIMEOUT")
192 def get_response(self):
193 if agent_timeout > 0.0:
194 ready = select.select([self.p.stdout], [], [], agent_timeout)[0]
196 ready = [self.p.stdout]
197 if self.p.stdout in ready:
198 #sys.stderr.write("Reading from " + str(self.p) + " 's stdout...\n")
200 result = self.p.stdout.readline().strip(" \t\r\n")
201 #sys.stderr.write("Read \'" + result + "\' from " + str(self.p) + "\n")
203 except: # Exception, e:
204 raise Exception("UNRESPONSIVE")
206 raise Exception("TIMEOUT")
210 self.send_message("SELECTION?")
211 line = self.get_response()
214 m = re.match("\s*(\d+)\s+(\d+)\s*", line)
215 result = map(int, [m.group(1), m.group(2)])
217 raise Exception("GIBBERISH \"" + str(line) + "\"")
220 def update(self, result):
221 #print "Update " + str(result) + " called for AgentPlayer"
222 self.send_message(result)
227 self.send_message("MOVE?")
228 line = self.get_response()
231 m = re.match("\s*(\d+)\s+(\d+)\s*", line)
232 result = map(int, [m.group(1), m.group(2)])
235 raise Exception("GIBBERISH \"" + str(line) + "\"")
238 def reset_board(self, s):
239 self.send_message("BOARD")
240 for line in s.split("\n"):
241 self.send_message(line.strip(" \r\n"))
242 self.send_message("END BOARD")
244 def quit(self, final_result):
246 self.send_message("QUIT " + final_result)
250 # So you want to be a player here?
251 class HumanPlayer(Player):
252 def __init__(self, name, colour):
253 Player.__init__(self, name, colour)
255 # Select your preferred account
257 if isinstance(graphics, GraphicsThread):
258 # Basically, we let the graphics thread do some shit and then return that information to the game thread
259 graphics.cond.acquire()
260 # We wait for the graphics thread to select a piece
261 while graphics.stopped() == False and graphics.state["select"] == None:
262 graphics.cond.wait() # The difference between humans and machines is that humans sleep
263 select = graphics.state["select"]
266 graphics.cond.release()
267 if graphics.stopped():
269 return [select.x, select.y]
271 # Since I don't display the board in this case, I'm not sure why I filled it in...
273 sys.stdout.write("SELECTION?\n")
275 p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
278 sys.stderr.write("ILLEGAL GIBBERISH\n")
281 # It's your move captain
283 if isinstance(graphics, GraphicsThread):
284 graphics.cond.acquire()
285 while graphics.stopped() == False and graphics.state["dest"] == None:
287 graphics.cond.release()
289 return graphics.state["dest"]
293 sys.stdout.write("MOVE?\n")
295 p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
298 sys.stderr.write("ILLEGAL GIBBERISH\n")
301 # Are you sure you want to quit?
302 def quit(self, final_result):
304 sys.stdout.write("QUIT " + final_result + "\n")
306 # Completely useless function
307 def update(self, result):
308 if isinstance(graphics, GraphicsThread):
311 sys.stdout.write(result + "\n")
315 # Default internal player (makes random moves)
316 class InternalAgent(Player):
317 def __init__(self, name, colour):
318 Player.__init__(self, name, colour)
321 self.board = Board(style = "agent")
325 def update(self, result):
327 self.board.update(result)
331 def reset_board(self, s):
332 self.board.reset_board(s)
334 def quit(self, final_result):
337 class AgentRandom(InternalAgent):
338 def __init__(self, name, colour):
339 InternalAgent.__init__(self, name, colour)
343 self.choice = self.board.pieces[self.colour][random.randint(0, len(self.board.pieces[self.colour])-1)]
345 # Check that the piece has some possibility to move
346 tmp = self.choice.current_type
347 if tmp == "unknown": # For unknown pieces, try both types
348 for t in self.choice.types:
351 self.choice.current_type = t
352 all_moves += self.board.possible_moves(self.choice)
354 all_moves = self.board.possible_moves(self.choice)
355 self.choice.current_type = tmp
356 if len(all_moves) > 0:
358 return [self.choice.x, self.choice.y]
361 moves = self.board.possible_moves(self.choice)
362 move = moves[random.randint(0, len(moves)-1)]
366 # Terrible, terrible hacks
368 def run_agent(agent):
369 #sys.stderr.write(sys.argv[0] + " : Running agent " + str(agent) + "\n")
371 line = sys.stdin.readline().strip(" \r\n")
372 if line == "SELECTION?":
373 #sys.stderr.write(sys.argv[0] + " : Make selection\n")
374 [x,y] = agent.select() # Gets your agent's selection
375 #sys.stderr.write(sys.argv[0] + " : Selection was " + str(agent.choice) + "\n")
376 sys.stdout.write(str(x) + " " + str(y) + "\n")
377 elif line == "MOVE?":
378 #sys.stderr.write(sys.argv[0] + " : Make move\n")
379 [x,y] = agent.get_move() # Gets your agent's move
380 sys.stdout.write(str(x) + " " + str(y) + "\n")
381 elif line.split(" ")[0] == "QUIT":
382 #sys.stderr.write(sys.argv[0] + " : Quitting\n")
383 agent.quit(" ".join(line.split(" ")[1:])) # Quits the game
385 elif line.split(" ")[0] == "BOARD":
387 line = sys.stdin.readline().strip(" \r\n")
388 while line != "END BOARD":
390 line = sys.stdin.readline().strip(" \r\n")
391 agent.board.reset_board(s)
394 agent.update(line) # Updates agent.board
400 class ExternalWrapper(ExternalAgent):
401 def __init__(self, agent):
402 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))\""
404 ExternalAgent.__init__(self, run, agent.colour)