6 # A thread that runs the game
7 class GameThread(StoppableThread):
8 def __init__(self, board, players, server = True):
9 StoppableThread.__init__(self)
11 self.players = players
12 self.state = {"turn" : None} # The game state
13 self.error = 0 # Whether the thread exits with an error
14 self.lock = threading.RLock() #lock for access of self.state
15 self.cond = threading.Condition() # conditional for some reason, I forgot
16 self.final_result = ""
18 self.retry_illegal = False
23 # Run the game (run in new thread with start(), run in current thread with run())
26 while not self.stopped():
28 for p in self.players:
30 self.state["turn"] = p.base_player()
33 [x,y] = p.select() # Player selects a square
35 #debug("Quitting in select")
38 if isinstance(p, NetworkPlayer):
39 if p.network.server == True:
40 result = self.board.select(x, y, colour = p.colour)
45 result = self.board.select(x, y, colour = p.colour)
47 result = p.update(result)
50 for p2 in self.players:
53 p2.update(result) # Inform players of what happened
63 target = self.board.grid[x][y]
64 if isinstance(graphics, GraphicsThread):
66 graphics.state["moves"] = self.board.possible_moves(target)
67 graphics.state["select"] = target
69 time.sleep(turn_delay)
72 if len(self.board.possible_moves(target)) == 0:
73 #print "Piece cannot move"
75 if isinstance(graphics, GraphicsThread):
77 graphics.state["moves"] = None
78 graphics.state["select"] = None
79 graphics.state["dest"] = None
83 [x2,y2] = p.get_move() # Player selects a destination
88 #debug("Quitting in get_move")
91 if isinstance(p, NetworkPlayer):
92 if p.network.server == True:
93 result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
94 self.board.update_move(x, y, x2, y2)
99 result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
100 self.board.update_move(x, y, x2, y2)
102 result = p.update(result)
105 for p2 in self.players:
108 p2.update(result) # Inform players of what happened
122 if isinstance(graphics, GraphicsThread):
124 graphics.state["moves"] = [[x2,y2]]
126 time.sleep(turn_delay)
128 if isinstance(graphics, GraphicsThread):
130 graphics.state["select"] = None
131 graphics.state["dest"] = None
132 graphics.state["moves"] = None
135 end = self.board.end_condition()
139 self.final_result = self.state["turn"].colour + " " + end
141 self.final_result = end
151 if self.retry_illegal:
152 self.state["turn"].update(result);
154 sys.stderr.write("qchess.py exception: "+result + "\n")
157 self.final_result = self.state["turn"].colour + " " + e.message
162 for p2 in self.players:
163 p2.quit(self.final_result)
165 log(self.final_result)
167 if isinstance(graphics, GraphicsThread):
171 # A thread that replays a log file
172 class ReplayThread(GameThread):
173 def __init__(self, players, src, end=False,max_moves=None):
174 self.board = Board(style="empty")
175 self.board.max_moves = max_moves
176 GameThread.__init__(self, self.board, players)
180 self.reset_board(self.src.readline())
182 def reset_board(self, line):
185 while line != "# Start game" and line != "# EOF":
188 line = self.src.readline().strip(" \r\n")
192 line = self.src.readline().strip(" \r\n")
195 self_str += line + "\n"
197 if self.players[0].name == "dummy" and self.players[1].name == "dummy":
198 line = self.src.readline().strip(" \r\n")
201 tokens = line.split(" ")
202 types = map(lambda e : e.strip("[] ,'"), tokens[2:4])
203 for i in range(len(types)):
204 if types[i][0] == "?":
207 agent_str += tokens[0] + " " + tokens[1] + " " + str(types) + " ".join(tokens[4:]) + "\n"
208 line = self.src.readline().strip(" \r\n")
210 for p in self.players:
211 p.reset_board(agent_str)
214 self.board.reset_board(self_str)
220 line = self.src.readline().strip(" \r\n")
221 while line != "# EOF":
233 line = self.src.readline().strip(" \r\n")
236 tokens = line.split(" ")
237 if tokens[0] == "white" or tokens[0] == "black":
238 self.reset_board(line)
240 line = self.src.readline().strip(" \r\n")
243 move = line.split(":")
244 move = move[len(move)-1].strip(" \r\n")
245 tokens = move.split(" ")
249 [x,y] = map(int, tokens[0:2])
257 target = self.board.grid[x][y]
259 if target.colour == "white":
260 self.state["turn"] = self.players[0]
262 self.state["turn"] = self.players[1]
264 move_piece = (tokens[2] == "->")
266 [x2,y2] = map(int, tokens[len(tokens)-2:])
268 if isinstance(graphics, GraphicsThread):
270 graphics.state["select"] = target
273 self.board.update_select(x, y, int(tokens[2]), tokens[len(tokens)-1])
274 if isinstance(graphics, GraphicsThread):
276 if target.current_type != "unknown":
277 graphics.state["moves"] = self.board.possible_moves(target)
279 graphics.state["moves"] = None
280 time.sleep(turn_delay)
282 self.board.update_move(x, y, x2, y2)
283 if isinstance(graphics, GraphicsThread):
285 graphics.state["moves"] = [[x2,y2]]
286 time.sleep(turn_delay)
288 graphics.state["select"] = None
289 graphics.state["moves"] = None
290 graphics.state["dest"] = None
296 for p in self.players:
300 line = self.src.readline().strip(" \r\n")
303 end = self.board.end_condition()
305 self.final_result = end
323 if self.end and isinstance(graphics, GraphicsThread):
325 pass # Let the user stop the display
326 elif not self.end and self.board.end_condition() == None:
328 # Work out the last move
330 t = last_line.split(" ")
331 if t[len(t)-2] == "black":
332 self.players.reverse()
333 elif t[len(t)-2] == "white":
335 elif self.state["turn"] != None and self.state["turn"].colour == "white":
336 self.players.reverse()
339 game = GameThread(self.board, self.players)
346 def opponent(colour):
347 if colour == "white":