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
37 if not (isinstance(p, Network) and p.server == False):
38 result = self.board.select(x, y, colour = p.colour)
40 #debug(str(self) + " don't update local board")
43 result = p.update(result)
44 for p2 in self.players:
46 result = p2.update(result) # Inform players of what happened
51 target = self.board.grid[x][y]
52 if isinstance(graphics, GraphicsThread):
54 graphics.state["moves"] = self.board.possible_moves(target)
55 graphics.state["select"] = target
57 time.sleep(turn_delay)
60 if len(self.board.possible_moves(target)) == 0:
61 #print "Piece cannot move"
63 if isinstance(graphics, GraphicsThread):
65 graphics.state["moves"] = None
66 graphics.state["select"] = None
67 graphics.state["dest"] = None
71 [x2,y2] = p.get_move() # Player selects a destination
78 result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
81 self.board.update_move(x, y, x2, y2)
83 for p2 in self.players:
84 p2.update(result) # Inform players of what happened
88 if isinstance(graphics, GraphicsThread):
90 graphics.state["moves"] = [[x2,y2]]
92 time.sleep(turn_delay)
94 if isinstance(graphics, GraphicsThread):
96 graphics.state["select"] = None
97 graphics.state["dest"] = None
98 graphics.state["moves"] = None
100 # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere
101 # except Exception,e:
103 # #sys.stderr.write(result + "\n")
107 # self.final_result = self.state["turn"].colour + " " + e.message
109 end = self.board.end_condition()
113 self.final_result = self.state["turn"].colour + " " + end
115 self.final_result = end
122 for p2 in self.players:
123 p2.quit(self.final_result)
125 log(self.final_result)
127 if isinstance(graphics, GraphicsThread):
131 # A thread that replays a log file
132 class ReplayThread(GameThread):
133 def __init__(self, players, src, end=False,max_moves=None):
134 self.board = Board(style="empty")
135 self.board.max_moves = max_moves
136 GameThread.__init__(self, self.board, players)
140 self.reset_board(self.src.readline())
142 def reset_board(self, line):
145 while line != "# Start game" and line != "# EOF":
148 line = self.src.readline().strip(" \r\n")
152 line = self.src.readline().strip(" \r\n")
155 self_str += line + "\n"
157 if self.players[0].name == "dummy" and self.players[1].name == "dummy":
158 line = self.src.readline().strip(" \r\n")
161 tokens = line.split(" ")
162 types = map(lambda e : e.strip("[] ,'"), tokens[2:4])
163 for i in range(len(types)):
164 if types[i][0] == "?":
167 agent_str += tokens[0] + " " + tokens[1] + " " + str(types) + " ".join(tokens[4:]) + "\n"
168 line = self.src.readline().strip(" \r\n")
170 for p in self.players:
171 p.reset_board(agent_str)
174 self.board.reset_board(self_str)
180 line = self.src.readline().strip(" \r\n")
181 while line != "# EOF":
193 line = self.src.readline().strip(" \r\n")
196 tokens = line.split(" ")
197 if tokens[0] == "white" or tokens[0] == "black":
198 self.reset_board(line)
200 line = self.src.readline().strip(" \r\n")
203 move = line.split(":")
204 move = move[len(move)-1].strip(" \r\n")
205 tokens = move.split(" ")
209 [x,y] = map(int, tokens[0:2])
217 target = self.board.grid[x][y]
219 if target.colour == "white":
220 self.state["turn"] = self.players[0]
222 self.state["turn"] = self.players[1]
224 move_piece = (tokens[2] == "->")
226 [x2,y2] = map(int, tokens[len(tokens)-2:])
228 if isinstance(graphics, GraphicsThread):
230 graphics.state["select"] = target
233 self.board.update_select(x, y, int(tokens[2]), tokens[len(tokens)-1])
234 if isinstance(graphics, GraphicsThread):
236 if target.current_type != "unknown":
237 graphics.state["moves"] = self.board.possible_moves(target)
239 graphics.state["moves"] = None
240 time.sleep(turn_delay)
242 self.board.update_move(x, y, x2, y2)
243 if isinstance(graphics, GraphicsThread):
245 graphics.state["moves"] = [[x2,y2]]
246 time.sleep(turn_delay)
248 graphics.state["select"] = None
249 graphics.state["moves"] = None
250 graphics.state["dest"] = None
256 for p in self.players:
260 line = self.src.readline().strip(" \r\n")
263 end = self.board.end_condition()
265 self.final_result = end
283 if self.end and isinstance(graphics, GraphicsThread):
285 pass # Let the user stop the display
286 elif not self.end and self.board.end_condition() == None:
288 # Work out the last move
290 t = last_line.split(" ")
291 if t[len(t)-2] == "black":
292 self.players.reverse()
293 elif t[len(t)-2] == "white":
295 elif self.state["turn"] != None and self.state["turn"].colour == "white":
296 self.players.reverse()
299 game = GameThread(self.board, self.players)
306 def opponent(colour):
307 if colour == "white":