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
38 if isinstance(p, NetworkPlayer):
39 if p.network.server == True:
40 result = self.board.select(x, y, colour = p.colour)
45 result = self.board.select(x, y, colour = p.colour)
47 result = p.update(result)
48 for p2 in self.players:
50 p2.update(result) # Inform players of what happened
55 target = self.board.grid[x][y]
56 if isinstance(graphics, GraphicsThread):
58 graphics.state["moves"] = self.board.possible_moves(target)
59 graphics.state["select"] = target
61 time.sleep(turn_delay)
64 if len(self.board.possible_moves(target)) == 0:
65 #print "Piece cannot move"
67 if isinstance(graphics, GraphicsThread):
69 graphics.state["moves"] = None
70 graphics.state["select"] = None
71 graphics.state["dest"] = None
75 [x2,y2] = p.get_move() # Player selects a destination
82 if isinstance(p, NetworkPlayer):
83 if p.network.server == True:
84 result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
85 self.board.update_move(x, y, x2, y2)
90 result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
91 self.board.update_move(x, y, x2, y2)
93 result = p.update(result)
94 for p2 in self.players:
96 p2.update(result) # Inform players of what happened
103 if isinstance(graphics, GraphicsThread):
105 graphics.state["moves"] = [[x2,y2]]
107 time.sleep(turn_delay)
109 if isinstance(graphics, GraphicsThread):
111 graphics.state["select"] = None
112 graphics.state["dest"] = None
113 graphics.state["moves"] = None
115 # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere
116 # except Exception,e:
118 # #sys.stderr.write(result + "\n")
122 # self.final_result = self.state["turn"].colour + " " + e.message
124 end = self.board.end_condition()
128 self.final_result = self.state["turn"].colour + " " + end
130 self.final_result = end
137 for p2 in self.players:
138 p2.quit(self.final_result)
140 log(self.final_result)
142 if isinstance(graphics, GraphicsThread):
146 # A thread that replays a log file
147 class ReplayThread(GameThread):
148 def __init__(self, players, src, end=False,max_moves=None):
149 self.board = Board(style="empty")
150 self.board.max_moves = max_moves
151 GameThread.__init__(self, self.board, players)
155 self.reset_board(self.src.readline())
157 def reset_board(self, line):
160 while line != "# Start game" and line != "# EOF":
163 line = self.src.readline().strip(" \r\n")
167 line = self.src.readline().strip(" \r\n")
170 self_str += line + "\n"
172 if self.players[0].name == "dummy" and self.players[1].name == "dummy":
173 line = self.src.readline().strip(" \r\n")
176 tokens = line.split(" ")
177 types = map(lambda e : e.strip("[] ,'"), tokens[2:4])
178 for i in range(len(types)):
179 if types[i][0] == "?":
182 agent_str += tokens[0] + " " + tokens[1] + " " + str(types) + " ".join(tokens[4:]) + "\n"
183 line = self.src.readline().strip(" \r\n")
185 for p in self.players:
186 p.reset_board(agent_str)
189 self.board.reset_board(self_str)
195 line = self.src.readline().strip(" \r\n")
196 while line != "# EOF":
208 line = self.src.readline().strip(" \r\n")
211 tokens = line.split(" ")
212 if tokens[0] == "white" or tokens[0] == "black":
213 self.reset_board(line)
215 line = self.src.readline().strip(" \r\n")
218 move = line.split(":")
219 move = move[len(move)-1].strip(" \r\n")
220 tokens = move.split(" ")
224 [x,y] = map(int, tokens[0:2])
232 target = self.board.grid[x][y]
234 if target.colour == "white":
235 self.state["turn"] = self.players[0]
237 self.state["turn"] = self.players[1]
239 move_piece = (tokens[2] == "->")
241 [x2,y2] = map(int, tokens[len(tokens)-2:])
243 if isinstance(graphics, GraphicsThread):
245 graphics.state["select"] = target
248 self.board.update_select(x, y, int(tokens[2]), tokens[len(tokens)-1])
249 if isinstance(graphics, GraphicsThread):
251 if target.current_type != "unknown":
252 graphics.state["moves"] = self.board.possible_moves(target)
254 graphics.state["moves"] = None
255 time.sleep(turn_delay)
257 self.board.update_move(x, y, x2, y2)
258 if isinstance(graphics, GraphicsThread):
260 graphics.state["moves"] = [[x2,y2]]
261 time.sleep(turn_delay)
263 graphics.state["select"] = None
264 graphics.state["moves"] = None
265 graphics.state["dest"] = None
271 for p in self.players:
275 line = self.src.readline().strip(" \r\n")
278 end = self.board.end_condition()
280 self.final_result = end
298 if self.end and isinstance(graphics, GraphicsThread):
300 pass # Let the user stop the display
301 elif not self.end and self.board.end_condition() == None:
303 # Work out the last move
305 t = last_line.split(" ")
306 if t[len(t)-2] == "black":
307 self.players.reverse()
308 elif t[len(t)-2] == "white":
310 elif self.state["turn"] != None and self.state["turn"].colour == "white":
311 self.players.reverse()
314 game = GameThread(self.board, self.players)
321 def opponent(colour):
322 if colour == "white":