8 log_file.write(str(datetime.datetime.now()) + " : " + s + "\n")
13 # A thread that runs the game
14 class GameThread(StoppableThread):
15 def __init__(self, board, players):
16 StoppableThread.__init__(self)
18 self.players = players
19 self.state = {"turn" : None} # The game state
20 self.error = 0 # Whether the thread exits with an error
21 self.lock = threading.RLock() #lock for access of self.state
22 self.cond = threading.Condition() # conditional for some reason, I forgot
23 self.final_result = ""
25 # Run the game (run in new thread with start(), run in current thread with run())
28 while not self.stopped():
30 for p in self.players:
32 if isinstance(p, NetworkSender):
33 self.state["turn"] = p.base_player # "turn" contains the player who's turn it is
35 self.state["turn"] = p
38 [x,y] = p.select() # Player selects a square
45 result = self.board.select(x, y, colour = p.colour)
46 for p2 in self.players:
47 p2.update(result) # Inform players of what happened
52 target = self.board.grid[x][y]
53 if isinstance(graphics, GraphicsThread):
55 graphics.state["moves"] = self.board.possible_moves(target)
56 graphics.state["select"] = target
58 time.sleep(turn_delay)
61 if len(self.board.possible_moves(target)) == 0:
62 #print "Piece cannot move"
64 if isinstance(graphics, GraphicsThread):
66 graphics.state["moves"] = None
67 graphics.state["select"] = None
68 graphics.state["dest"] = None
72 [x2,y2] = p.get_move() # Player selects a destination
79 self.board.update_move(x, y, x2, y2)
80 result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)
81 for p2 in self.players:
82 p2.update(result) # Inform players of what happened
86 if isinstance(graphics, GraphicsThread):
88 graphics.state["moves"] = [[x2,y2]]
90 time.sleep(turn_delay)
92 if isinstance(graphics, GraphicsThread):
94 graphics.state["select"] = None
95 graphics.state["dest"] = None
96 graphics.state["moves"] = None
98 # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere
101 # #sys.stderr.write(result + "\n")
105 # self.final_result = self.state["turn"].colour + " " + e.message
107 if self.board.king["black"] == None:
108 if self.board.king["white"] == None:
110 self.final_result = self.state["turn"].colour + " DRAW"
113 self.final_result = "white"
115 elif self.board.king["white"] == None:
117 self.final_result = "black"
125 for p2 in self.players:
126 p2.quit(self.final_result)
128 log(self.final_result)
133 # A thread that replays a log file
134 class ReplayThread(GameThread):
135 def __init__(self, players, src):
136 self.board = Board(style="agent")
137 GameThread.__init__(self, self.board, players)
145 for line in self.src:
152 self.state["turn"] = self.players[i]
154 line = line.split(":")
155 result = line[len(line)-1].strip(" \r\n")
159 self.board.update(result)
162 self.final_result = result
163 if isinstance(graphics, GraphicsThread):
167 [x,y] = map(int, result.split(" ")[0:2])
168 target = self.board.grid[x][y]
170 if isinstance(graphics, GraphicsThread):
173 graphics.state["moves"] = self.board.possible_moves(target)
174 graphics.state["select"] = target
176 time.sleep(turn_delay)
179 [x2,y2] = map(int, result.split(" ")[3:5])
181 graphics.state["moves"] = [[x2,y2]]
183 time.sleep(turn_delay)
186 graphics.state["select"] = None
187 graphics.state["dest"] = None
188 graphics.state["moves"] = None
194 for p in self.players:
197 phase = (phase + 1) % 2
203 def opponent(colour):
204 if colour == "white":