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 self.board.update_move(x, y, x2, y2)
75 result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
76 for p2 in self.players:
77 p2.update(result) # Inform players of what happened
81 if isinstance(graphics, GraphicsThread):
83 graphics.state["moves"] = [[x2,y2]]
85 time.sleep(turn_delay)
87 if isinstance(graphics, GraphicsThread):
89 graphics.state["select"] = None
90 graphics.state["dest"] = None
91 graphics.state["moves"] = None
93 # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere
96 # #sys.stderr.write(result + "\n")
100 # self.final_result = self.state["turn"].colour + " " + e.message
102 if self.board.king["black"] == None:
103 if self.board.king["white"] == None:
105 self.final_result = self.state["turn"].colour + " DRAW"
108 self.final_result = "white"
110 elif self.board.king["white"] == None:
112 self.final_result = "black"
120 for p2 in self.players:
121 p2.quit(self.final_result)
123 log(self.final_result)
128 # A thread that replays a log file
129 class ReplayThread(GameThread):
130 def __init__(self, players, src, end=False,max_lines=None):
131 self.board = Board(style="empty")
132 GameThread.__init__(self, self.board, players)
134 self.max_lines = max_lines
142 while self.src.readline().strip(" \r\n") != "# Initial board":
143 self.line_number += 1
145 line = self.src.readline().strip(" \r\n")
147 while line != "# Start game":
148 #print "Reading line " + str(line)
149 self.line_number += 1
150 [x,y] = map(int, line.split("at")[1].strip(" \r\n").split(","))
151 colour = line.split(" ")[0]
152 current_type = line.split(" ")[1]
153 types = map(lambda e : e.strip(" [],'"), line.split(" ")[2:4])
154 p = Piece(colour, x, y, types)
155 if current_type != "unknown":
156 p.current_type = current_type
157 p.choice = types.index(current_type)
159 self.board.pieces[colour].append(p)
160 self.board.grid[x][y] = p
161 if current_type == "king":
162 self.board.king[colour] = p
164 line = self.src.readline().strip(" \r\n")
166 #except Exception, e:
167 # raise Exception("FILE line: " + str(self.line_number) + " \""+str(line)+"\"") #\n" + e.message)
173 line = self.src.readline().strip(" \r\n")
174 while line != "# EOF":
176 if self.max_lines != None and count > self.max_lines:
183 self.state["turn"] = self.players[i]
185 line = line.split(":")
186 result = line[len(line)-1].strip(" \r\n")
190 self.board.update(result)
192 self.final_result = result
196 [x,y] = map(int, result.split(" ")[0:2])
197 target = self.board.grid[x][y]
199 if isinstance(graphics, GraphicsThread):
202 graphics.state["moves"] = self.board.possible_moves(target)
203 graphics.state["select"] = target
206 time.sleep(turn_delay)
209 [x2,y2] = map(int, result.split(" ")[3:5])
211 graphics.state["moves"] = [[x2,y2]]
214 time.sleep(turn_delay)
217 graphics.state["select"] = None
218 graphics.state["dest"] = None
219 graphics.state["moves"] = None
225 for p in self.players:
228 phase = (phase + 1) % 2
232 line = self.src.readline().strip(" \r\n")
234 if self.max_lines != None and self.max_lines > count:
235 sys.stderr.write(sys.argv[0] + " : Replaying from file; stopping at last line (" + str(count) + ")\n")
236 sys.stderr.write(sys.argv[0] + " : (You requested line " + str(self.max_lines) + ")\n")
238 if self.end and isinstance(graphics, GraphicsThread):
240 pass # Let the user stop the display
243 game = GameThread(self.board, self.players)
249 def opponent(colour):
250 if colour == "white":