8 log_file.write(str(datetime.datetime.now()) + " : " + s + "\n")
10 def log_init(board, players):
13 log_file.write("# Log starts " + str(datetime.datetime.now()) + "\n")
15 log_file.write("# " + p.colour + " : " + p.name + "\n")
17 log_file.write("# Initial board\n")
20 if board.grid[x][y] != None:
21 log_file.write(str(board.grid[x][y]) + "\n")
23 log_file.write("# Start game\n")
26 # A thread that runs the game
27 class GameThread(StoppableThread):
28 def __init__(self, board, players):
29 StoppableThread.__init__(self)
31 self.players = players
32 self.state = {"turn" : None} # The game state
33 self.error = 0 # Whether the thread exits with an error
34 self.lock = threading.RLock() #lock for access of self.state
35 self.cond = threading.Condition() # conditional for some reason, I forgot
36 self.final_result = ""
40 # Run the game (run in new thread with start(), run in current thread with run())
43 while not self.stopped():
45 for p in self.players:
47 if isinstance(p, NetworkSender):
48 self.state["turn"] = p.base_player # "turn" contains the player who's turn it is
50 self.state["turn"] = p
53 [x,y] = p.select() # Player selects a square
60 result = self.board.select(x, y, colour = p.colour)
61 for p2 in self.players:
62 p2.update(result) # Inform players of what happened
67 target = self.board.grid[x][y]
68 if isinstance(graphics, GraphicsThread):
70 graphics.state["moves"] = self.board.possible_moves(target)
71 graphics.state["select"] = target
73 time.sleep(turn_delay)
76 if len(self.board.possible_moves(target)) == 0:
77 #print "Piece cannot move"
79 if isinstance(graphics, GraphicsThread):
81 graphics.state["moves"] = None
82 graphics.state["select"] = None
83 graphics.state["dest"] = None
87 [x2,y2] = p.get_move() # Player selects a destination
94 self.board.update_move(x, y, x2, y2)
95 result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
96 for p2 in self.players:
97 p2.update(result) # Inform players of what happened
101 if isinstance(graphics, GraphicsThread):
103 graphics.state["moves"] = [[x2,y2]]
105 time.sleep(turn_delay)
107 if isinstance(graphics, GraphicsThread):
109 graphics.state["select"] = None
110 graphics.state["dest"] = None
111 graphics.state["moves"] = None
113 # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere
114 # except Exception,e:
116 # #sys.stderr.write(result + "\n")
120 # self.final_result = self.state["turn"].colour + " " + e.message
122 if self.board.king["black"] == None:
123 if self.board.king["white"] == None:
125 self.final_result = self.state["turn"].colour + " DRAW"
128 self.final_result = "white"
130 elif self.board.king["white"] == None:
132 self.final_result = "black"
140 for p2 in self.players:
141 p2.quit(self.final_result)
143 log(self.final_result)
148 # A thread that replays a log file
149 class ReplayThread(GameThread):
150 def __init__(self, players, src, end=False,max_lines=None):
151 self.board = Board(style="empty")
152 GameThread.__init__(self, self.board, players)
154 self.max_lines = max_lines
159 while self.src.readline().strip(" \r\n") != "# Initial board":
160 self.line_number += 1
162 line = self.src.readline().strip(" \r\n")
164 while line != "# Start game":
165 #print "Reading line " + str(line)
166 self.line_number += 1
167 [x,y] = map(int, line.split("at")[1].strip(" \r\n").split(","))
168 colour = line.split(" ")[0]
169 current_type = line.split(" ")[1]
170 types = map(lambda e : e.strip(" [],'"), line.split(" ")[2:4])
171 p = Piece(colour, x, y, types)
172 if current_type != "unknown":
173 p.current_type = current_type
174 p.choice = types.index(current_type)
176 self.board.pieces[colour].append(p)
177 self.board.grid[x][y] = p
178 if current_type == "king":
179 self.board.king[colour] = p
181 line = self.src.readline().strip(" \r\n")
184 raise Exception("FILE line: " + str(self.line_number) + " \""+str(line)+"\"") #\n" + e.message)
190 line = self.src.readline().strip(" \r\n")
191 while line != "# EOF":
193 if self.max_lines != None and count > self.max_lines:
200 self.state["turn"] = self.players[i]
202 line = line.split(":")
203 result = line[len(line)-1].strip(" \r\n")
207 self.board.update(result)
209 self.final_result = result
213 [x,y] = map(int, result.split(" ")[0:2])
214 target = self.board.grid[x][y]
216 if isinstance(graphics, GraphicsThread):
219 graphics.state["moves"] = self.board.possible_moves(target)
220 graphics.state["select"] = target
223 time.sleep(turn_delay)
226 [x2,y2] = map(int, result.split(" ")[3:5])
228 graphics.state["moves"] = [[x2,y2]]
231 time.sleep(turn_delay)
234 graphics.state["select"] = None
235 graphics.state["dest"] = None
236 graphics.state["moves"] = None
242 for p in self.players:
245 phase = (phase + 1) % 2
249 line = self.src.readline().strip(" \r\n")
251 if self.max_lines != None and self.max_lines > count:
252 sys.stderr.write(sys.argv[0] + " : Replaying from file; stopping at last line (" + str(count) + ")\n")
253 sys.stderr.write(sys.argv[0] + " : (You requested line " + str(self.max_lines) + ")\n")
255 if self.end and isinstance(graphics, GraphicsThread):
257 pass # Let the user stop the display
260 game = GameThread(self.board, self.players)
266 def opponent(colour):
267 if colour == "white":