6 # A thread that runs the game
7 class GameThread(StoppableThread):
8 def __init__(self, board, players):
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 = ""
20 # Run the game (run in new thread with start(), run in current thread with run())
23 while not self.stopped():
25 for p in self.players:
27 if isinstance(p, Network) and p.baseplayer != None:
28 self.state["turn"] = p.baseplayer # "turn" contains the player who's turn it is
30 self.state["turn"] = p
33 [x,y] = p.select() # Player selects a square
37 if isinstance(p, Network) == False or p.server == True:
38 result = self.board.select(x, y, colour = p.colour)
40 result = p.get_response()
41 self.board.update(result)
43 for p2 in self.players:
44 p2.update(result) # Inform players of what happened
49 target = self.board.grid[x][y]
50 if isinstance(graphics, GraphicsThread):
52 graphics.state["moves"] = self.board.possible_moves(target)
53 graphics.state["select"] = target
55 time.sleep(turn_delay)
58 if len(self.board.possible_moves(target)) == 0:
59 #print "Piece cannot move"
61 if isinstance(graphics, GraphicsThread):
63 graphics.state["moves"] = None
64 graphics.state["select"] = None
65 graphics.state["dest"] = None
69 [x2,y2] = p.get_move() # Player selects a destination
76 result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
79 self.board.update_move(x, y, x2, y2)
81 for p2 in self.players:
82 p2.update(result) # Inform players of what happened
86 if isinstance(graphics, GraphicsThread):
88 graphics.state["moves"] = [[x2,y2]]
90 time.sleep(turn_delay)
92 if isinstance(graphics, GraphicsThread):
94 graphics.state["select"] = None
95 graphics.state["dest"] = None
96 graphics.state["moves"] = None
98 # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere
101 # #sys.stderr.write(result + "\n")
105 # self.final_result = self.state["turn"].colour + " " + e.message
107 end = self.board.end_condition()
111 self.final_result = self.state["turn"].colour + " " + end
113 self.final_result = end
120 for p2 in self.players:
121 p2.quit(self.final_result)
123 log(self.final_result)
125 if isinstance(graphics, GraphicsThread):
129 # A thread that replays a log file
130 class ReplayThread(GameThread):
131 def __init__(self, players, src, end=False,max_moves=None):
132 self.board = Board(style="empty")
133 self.board.max_moves = max_moves
134 GameThread.__init__(self, self.board, players)
138 self.reset_board(self.src.readline())
140 def reset_board(self, line):
143 while line != "# Start game" and line != "# EOF":
146 line = self.src.readline().strip(" \r\n")
150 line = self.src.readline().strip(" \r\n")
153 self_str += line + "\n"
155 if self.players[0].name == "dummy" and self.players[1].name == "dummy":
156 line = self.src.readline().strip(" \r\n")
159 tokens = line.split(" ")
160 types = map(lambda e : e.strip("[] ,'"), tokens[2:4])
161 for i in range(len(types)):
162 if types[i][0] == "?":
165 agent_str += tokens[0] + " " + tokens[1] + " " + str(types) + " ".join(tokens[4:]) + "\n"
166 line = self.src.readline().strip(" \r\n")
168 for p in self.players:
169 p.reset_board(agent_str)
172 self.board.reset_board(self_str)
178 line = self.src.readline().strip(" \r\n")
179 while line != "# EOF":
191 line = self.src.readline().strip(" \r\n")
194 tokens = line.split(" ")
195 if tokens[0] == "white" or tokens[0] == "black":
196 self.reset_board(line)
198 line = self.src.readline().strip(" \r\n")
201 move = line.split(":")
202 move = move[len(move)-1].strip(" \r\n")
203 tokens = move.split(" ")
207 [x,y] = map(int, tokens[0:2])
215 target = self.board.grid[x][y]
217 if target.colour == "white":
218 self.state["turn"] = self.players[0]
220 self.state["turn"] = self.players[1]
222 move_piece = (tokens[2] == "->")
224 [x2,y2] = map(int, tokens[len(tokens)-2:])
226 if isinstance(graphics, GraphicsThread):
228 graphics.state["select"] = target
231 self.board.update_select(x, y, int(tokens[2]), tokens[len(tokens)-1])
232 if isinstance(graphics, GraphicsThread):
234 if target.current_type != "unknown":
235 graphics.state["moves"] = self.board.possible_moves(target)
237 graphics.state["moves"] = None
238 time.sleep(turn_delay)
240 self.board.update_move(x, y, x2, y2)
241 if isinstance(graphics, GraphicsThread):
243 graphics.state["moves"] = [[x2,y2]]
244 time.sleep(turn_delay)
246 graphics.state["select"] = None
247 graphics.state["moves"] = None
248 graphics.state["dest"] = None
254 for p in self.players:
258 line = self.src.readline().strip(" \r\n")
261 end = self.board.end_condition()
263 self.final_result = end
281 if self.end and isinstance(graphics, GraphicsThread):
283 pass # Let the user stop the display
284 elif not self.end and self.board.end_condition() == None:
286 # Work out the last move
288 t = last_line.split(" ")
289 if t[len(t)-2] == "black":
290 self.players.reverse()
291 elif t[len(t)-2] == "white":
293 elif self.state["turn"] != None and self.state["turn"].colour == "white":
294 self.players.reverse()
297 game = GameThread(self.board, self.players)
304 def opponent(colour):
305 if colour == "white":