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 if self.board.king["black"] == None:
106 if self.board.king["white"] == None:
108 self.final_result = self.state["turn"].colour + " DRAW"
111 self.final_result = "white"
113 elif self.board.king["white"] == None:
115 self.final_result = "black"
123 for p2 in self.players:
124 p2.quit(self.final_result)
126 log(self.final_result)
128 if isinstance(graphics, GraphicsThread):
132 # A thread that replays a log file
133 class ReplayThread(GameThread):
134 def __init__(self, players, src, end=False,max_lines=None):
135 self.board = Board(style="empty")
136 GameThread.__init__(self, self.board, players)
138 self.max_lines = max_lines
142 self.reset_board(self.src.readline())
144 def reset_board(self, line):
145 pieces = {"white" : [], "black" : []}
146 king = {"white" : None, "black" : None}
149 self.board.grid[x][y] = None
150 while line != "# Start game":
151 tokens = line.split(" ")
152 [x, y] = map(int, tokens[len(tokens)-1].split(","))
153 current_type = tokens[1]
154 types = map(lambda e : e.strip("'[], "), tokens[2].split(","))
156 target = Piece(tokens[0], x, y, current_type)
158 target.choice = types.index(current_type)
162 pieces[token[0]].append(target)
163 if target.current_type == "king":
164 king[token[0]] = target
166 line = self.src.readline().strip(" \r\n")
168 self.board.pieces = pieces
169 self.board.king = king
173 line = self.src.readline().strip(" \r\n")
174 while line != "# EOF":
181 line = self.src.readline().strip(" \r\n")
184 tokens = line.split(" ")
185 if tokens[0] == "white" or tokens[0] == "black":
186 self.reset_board(line)
187 line = self.src.readline().strip(" \r\n")
190 move = line.split(":")[1]
191 tokens = move.split(" ")
193 [x,y] = map(int, tokens[0:2])
198 target = self.board.grid[x][y]
200 move_piece = (tokens[2] == "->")
203 [x2,y2] = map(int, tokens[len(tokens)-2:])
206 self.board.update(move)
207 for p in self.players:
210 if isinstance(graphics, GraphicsThread):
212 if target.colour == "white":
213 self.state["turn"] = self.players[0]
215 self.state["turn"] = self.players[1]
218 graphics.state["select"] = target
220 graphics.state["moves"] = [[x2, y2]]
221 elif target.current_type != "unknown":
222 graphics.state["moves"] = self.board.possible_moves(target)
231 if self.max_lines != None and self.max_lines > count:
232 sys.stderr.write(sys.argv[0] + " : Replaying from file; stopping at last line (" + str(count) + ")\n")
233 sys.stderr.write(sys.argv[0] + " : (You requested line " + str(self.max_lines) + ")\n")
235 if self.end and isinstance(graphics, GraphicsThread):
237 pass # Let the user stop the display
240 game = GameThread(self.board, self.players)
246 def opponent(colour):
247 if colour == "white":