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, NetworkSender):
28 self.state["turn"] = p.base_player # "turn" contains the player who's turn it is
30 self.state["turn"] = p
33 [x,y] = p.select() # Player selects a square
40 result = self.board.select(x, y, colour = p.colour)
41 for p2 in self.players:
42 p2.update(result) # Inform players of what happened
47 target = self.board.grid[x][y]
48 if isinstance(graphics, GraphicsThread):
50 graphics.state["moves"] = self.board.possible_moves(target)
51 graphics.state["select"] = target
53 time.sleep(turn_delay)
56 if len(self.board.possible_moves(target)) == 0:
57 #print "Piece cannot move"
59 if isinstance(graphics, GraphicsThread):
61 graphics.state["moves"] = None
62 graphics.state["select"] = None
63 graphics.state["dest"] = None
67 [x2,y2] = p.get_move() # Player selects a destination
74 result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
77 self.board.update_move(x, y, x2, y2)
79 for p2 in self.players:
80 p2.update(result) # Inform players of what happened
84 if isinstance(graphics, GraphicsThread):
86 graphics.state["moves"] = [[x2,y2]]
88 time.sleep(turn_delay)
90 if isinstance(graphics, GraphicsThread):
92 graphics.state["select"] = None
93 graphics.state["dest"] = None
94 graphics.state["moves"] = None
96 # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere
99 # #sys.stderr.write(result + "\n")
103 # self.final_result = self.state["turn"].colour + " " + e.message
105 end = self.board.end_condition()
109 self.final_result = self.state["turn"].colour + " " + end
111 self.final_result = end
118 for p2 in self.players:
119 p2.quit(self.final_result)
121 log(self.final_result)
123 if isinstance(graphics, GraphicsThread):
127 # A thread that replays a log file
128 class ReplayThread(GameThread):
129 def __init__(self, players, src, end=False,max_moves=None):
130 self.board = Board(style="empty")
131 self.board.max_moves = max_moves
132 GameThread.__init__(self, self.board, players)
136 self.reset_board(self.src.readline())
138 def reset_board(self, line):
141 while line != "# Start game" and line != "# EOF":
144 line = self.src.readline().strip(" \r\n")
148 line = self.src.readline().strip(" \r\n")
151 self_str += line + "\n"
153 if self.players[0].name == "dummy" and self.players[1].name == "dummy":
154 line = self.src.readline().strip(" \r\n")
157 tokens = line.split(" ")
158 types = map(lambda e : e.strip("[] ,'"), tokens[2:4])
159 for i in range(len(types)):
160 if types[i][0] == "?":
163 agent_str += tokens[0] + " " + tokens[1] + " " + str(types) + " ".join(tokens[4:]) + "\n"
164 line = self.src.readline().strip(" \r\n")
166 for p in self.players:
167 p.reset_board(agent_str)
170 self.board.reset_board(self_str)
176 line = self.src.readline().strip(" \r\n")
177 while line != "# EOF":
189 line = self.src.readline().strip(" \r\n")
192 tokens = line.split(" ")
193 if tokens[0] == "white" or tokens[0] == "black":
194 self.reset_board(line)
196 line = self.src.readline().strip(" \r\n")
199 move = line.split(":")
200 move = move[len(move)-1].strip(" \r\n")
201 tokens = move.split(" ")
205 [x,y] = map(int, tokens[0:2])
213 target = self.board.grid[x][y]
215 if target.colour == "white":
216 self.state["turn"] = self.players[0]
218 self.state["turn"] = self.players[1]
220 move_piece = (tokens[2] == "->")
222 [x2,y2] = map(int, tokens[len(tokens)-2:])
224 if isinstance(graphics, GraphicsThread):
226 graphics.state["select"] = target
229 self.board.update_select(x, y, int(tokens[2]), tokens[len(tokens)-1])
230 if isinstance(graphics, GraphicsThread):
232 if target.current_type != "unknown":
233 graphics.state["moves"] = self.board.possible_moves(target)
235 graphics.state["moves"] = None
236 time.sleep(turn_delay)
238 self.board.update_move(x, y, x2, y2)
239 if isinstance(graphics, GraphicsThread):
241 graphics.state["moves"] = [[x2,y2]]
242 time.sleep(turn_delay)
244 graphics.state["select"] = None
245 graphics.state["moves"] = None
246 graphics.state["dest"] = None
252 for p in self.players:
256 line = self.src.readline().strip(" \r\n")
259 end = self.board.end_condition()
261 self.final_result = end
279 if self.end and isinstance(graphics, GraphicsThread):
281 pass # Let the user stop the display
282 elif not self.end and self.board.end_condition() == None:
284 # Work out the last move
286 t = last_line.split(" ")
287 if t[len(t)-2] == "black":
288 self.players.reverse()
289 elif t[len(t)-2] == "white":
291 elif self.state["turn"] != None and self.state["turn"].colour == "white":
292 self.players.reverse()
295 game = GameThread(self.board, self.players)
302 def opponent(colour):
303 if colour == "white":