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 = ""
24 # Run the game (run in new thread with start(), run in current thread with run())
27 while not self.stopped():
29 for p in self.players:
31 self.state["turn"] = p.base_player()
34 [x,y] = p.select() # Player selects a square
36 #debug("Quitting in select")
39 if isinstance(p, NetworkPlayer):
40 if p.network.server == True:
41 result = self.board.select(x, y, colour = p.colour)
46 result = self.board.select(x, y, colour = p.colour)
48 result = p.update(result)
51 for p2 in self.players:
54 p2.update(result) # Inform players of what happened
64 target = self.board.grid[x][y]
65 if isinstance(graphics, GraphicsThread):
67 graphics.state["moves"] = self.board.possible_moves(target)
68 graphics.state["select"] = target
70 time.sleep(turn_delay)
73 if len(self.board.possible_moves(target)) == 0:
74 #print "Piece cannot move"
76 if isinstance(graphics, GraphicsThread):
78 graphics.state["moves"] = None
79 graphics.state["select"] = None
80 graphics.state["dest"] = None
84 [x2,y2] = p.get_move() # Player selects a destination
89 #debug("Quitting in get_move")
92 if isinstance(p, NetworkPlayer):
93 if p.network.server == True:
94 result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
95 self.board.update_move(x, y, x2, y2)
100 result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
101 self.board.update_move(x, y, x2, y2)
103 result = p.update(result)
106 for p2 in self.players:
109 p2.update(result) # Inform players of what happened
123 if isinstance(graphics, GraphicsThread):
125 graphics.state["moves"] = [[x2,y2]]
127 time.sleep(turn_delay)
129 if isinstance(graphics, GraphicsThread):
131 graphics.state["select"] = None
132 graphics.state["dest"] = None
133 graphics.state["moves"] = None
136 end = self.board.end_condition()
140 self.final_result = self.state["turn"].colour + " " + end
142 self.final_result = end
150 #sys.stderr.write(result + "\n")
155 self.final_result = self.state["turn"].colour + " " + e.message
160 for p2 in self.players:
161 p2.quit(self.final_result)
163 log(self.final_result)
165 if isinstance(graphics, GraphicsThread):
169 # A thread that replays a log file
170 class ReplayThread(GameThread):
171 def __init__(self, players, src, end=False,max_moves=None):
172 self.board = Board(style="empty")
173 self.board.max_moves = max_moves
174 GameThread.__init__(self, self.board, players)
178 self.reset_board(self.src.readline())
180 def reset_board(self, line):
183 while line != "# Start game" and line != "# EOF":
186 line = self.src.readline().strip(" \r\n")
190 line = self.src.readline().strip(" \r\n")
193 self_str += line + "\n"
195 if self.players[0].name == "dummy" and self.players[1].name == "dummy":
196 line = self.src.readline().strip(" \r\n")
199 tokens = line.split(" ")
200 types = map(lambda e : e.strip("[] ,'"), tokens[2:4])
201 for i in range(len(types)):
202 if types[i][0] == "?":
205 agent_str += tokens[0] + " " + tokens[1] + " " + str(types) + " ".join(tokens[4:]) + "\n"
206 line = self.src.readline().strip(" \r\n")
208 for p in self.players:
209 p.reset_board(agent_str)
212 self.board.reset_board(self_str)
218 line = self.src.readline().strip(" \r\n")
219 while line != "# EOF":
231 line = self.src.readline().strip(" \r\n")
234 tokens = line.split(" ")
235 if tokens[0] == "white" or tokens[0] == "black":
236 self.reset_board(line)
238 line = self.src.readline().strip(" \r\n")
241 move = line.split(":")
242 move = move[len(move)-1].strip(" \r\n")
243 tokens = move.split(" ")
247 [x,y] = map(int, tokens[0:2])
255 target = self.board.grid[x][y]
257 if target.colour == "white":
258 self.state["turn"] = self.players[0]
260 self.state["turn"] = self.players[1]
262 move_piece = (tokens[2] == "->")
264 [x2,y2] = map(int, tokens[len(tokens)-2:])
266 if isinstance(graphics, GraphicsThread):
268 graphics.state["select"] = target
271 self.board.update_select(x, y, int(tokens[2]), tokens[len(tokens)-1])
272 if isinstance(graphics, GraphicsThread):
274 if target.current_type != "unknown":
275 graphics.state["moves"] = self.board.possible_moves(target)
277 graphics.state["moves"] = None
278 time.sleep(turn_delay)
280 self.board.update_move(x, y, x2, y2)
281 if isinstance(graphics, GraphicsThread):
283 graphics.state["moves"] = [[x2,y2]]
284 time.sleep(turn_delay)
286 graphics.state["select"] = None
287 graphics.state["moves"] = None
288 graphics.state["dest"] = None
294 for p in self.players:
298 line = self.src.readline().strip(" \r\n")
301 end = self.board.end_condition()
303 self.final_result = end
321 if self.end and isinstance(graphics, GraphicsThread):
323 pass # Let the user stop the display
324 elif not self.end and self.board.end_condition() == None:
326 # Work out the last move
328 t = last_line.split(" ")
329 if t[len(t)-2] == "black":
330 self.players.reverse()
331 elif t[len(t)-2] == "white":
333 elif self.state["turn"] != None and self.state["turn"].colour == "white":
334 self.players.reverse()
337 game = GameThread(self.board, self.players)
344 def opponent(colour):
345 if colour == "white":