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
135 # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere
136 # except Exception,e:
138 # #sys.stderr.write(result + "\n")
142 # self.final_result = self.state["turn"].colour + " " + e.message
144 end = self.board.end_condition()
148 self.final_result = self.state["turn"].colour + " " + end
150 self.final_result = end
157 for p2 in self.players:
158 p2.quit(self.final_result)
160 log(self.final_result)
162 if isinstance(graphics, GraphicsThread):
166 # A thread that replays a log file
167 class ReplayThread(GameThread):
168 def __init__(self, players, src, end=False,max_moves=None):
169 self.board = Board(style="empty")
170 self.board.max_moves = max_moves
171 GameThread.__init__(self, self.board, players)
175 self.reset_board(self.src.readline())
177 def reset_board(self, line):
180 while line != "# Start game" and line != "# EOF":
183 line = self.src.readline().strip(" \r\n")
187 line = self.src.readline().strip(" \r\n")
190 self_str += line + "\n"
192 if self.players[0].name == "dummy" and self.players[1].name == "dummy":
193 line = self.src.readline().strip(" \r\n")
196 tokens = line.split(" ")
197 types = map(lambda e : e.strip("[] ,'"), tokens[2:4])
198 for i in range(len(types)):
199 if types[i][0] == "?":
202 agent_str += tokens[0] + " " + tokens[1] + " " + str(types) + " ".join(tokens[4:]) + "\n"
203 line = self.src.readline().strip(" \r\n")
205 for p in self.players:
206 p.reset_board(agent_str)
209 self.board.reset_board(self_str)
215 line = self.src.readline().strip(" \r\n")
216 while line != "# EOF":
228 line = self.src.readline().strip(" \r\n")
231 tokens = line.split(" ")
232 if tokens[0] == "white" or tokens[0] == "black":
233 self.reset_board(line)
235 line = self.src.readline().strip(" \r\n")
238 move = line.split(":")
239 move = move[len(move)-1].strip(" \r\n")
240 tokens = move.split(" ")
244 [x,y] = map(int, tokens[0:2])
252 target = self.board.grid[x][y]
254 if target.colour == "white":
255 self.state["turn"] = self.players[0]
257 self.state["turn"] = self.players[1]
259 move_piece = (tokens[2] == "->")
261 [x2,y2] = map(int, tokens[len(tokens)-2:])
263 if isinstance(graphics, GraphicsThread):
265 graphics.state["select"] = target
268 self.board.update_select(x, y, int(tokens[2]), tokens[len(tokens)-1])
269 if isinstance(graphics, GraphicsThread):
271 if target.current_type != "unknown":
272 graphics.state["moves"] = self.board.possible_moves(target)
274 graphics.state["moves"] = None
275 time.sleep(turn_delay)
277 self.board.update_move(x, y, x2, y2)
278 if isinstance(graphics, GraphicsThread):
280 graphics.state["moves"] = [[x2,y2]]
281 time.sleep(turn_delay)
283 graphics.state["select"] = None
284 graphics.state["moves"] = None
285 graphics.state["dest"] = None
291 for p in self.players:
295 line = self.src.readline().strip(" \r\n")
298 end = self.board.end_condition()
300 self.final_result = end
318 if self.end and isinstance(graphics, GraphicsThread):
320 pass # Let the user stop the display
321 elif not self.end and self.board.end_condition() == None:
323 # Work out the last move
325 t = last_line.split(" ")
326 if t[len(t)-2] == "black":
327 self.players.reverse()
328 elif t[len(t)-2] == "white":
330 elif self.state["turn"] != None and self.state["turn"].colour == "white":
331 self.players.reverse()
334 game = GameThread(self.board, self.players)
341 def opponent(colour):
342 if colour == "white":