X-Git-Url: https://git.ucc.asn.au/?p=progcomp2013.git;a=blobdiff_plain;f=qchess%2Fsrc%2Fgame.py;h=e3f1c7cd3bdca0d421c952184836819f9f1b02ac;hp=934c043d5cbd8f63ecefadc59e887a7f24db46e8;hb=877034f05346e24fdf822f6e6149ad50d891f030;hpb=521b6fc0521aa0fb9fef52ec2778474887fad12f diff --git a/qchess/src/game.py b/qchess/src/game.py index 934c043..e3f1c7c 100644 --- a/qchess/src/game.py +++ b/qchess/src/game.py @@ -1,4 +1,15 @@ + +log_file = None + +def log(s): + if log_file != None: + import datetime + log_file.write(str(datetime.datetime.now()) + " : " + s + "\n") + + + + # A thread that runs the game class GameThread(StoppableThread): def __init__(self, board, players): @@ -36,6 +47,7 @@ class GameThread(StoppableThread): p2.update(result) # Inform players of what happened + log(result) target = self.board.grid[x][y] if isinstance(graphics, GraphicsThread): @@ -64,9 +76,12 @@ class GameThread(StoppableThread): if self.stopped(): break - result = self.board.update_move(x, y, x2, y2) + self.board.update_move(x, y, x2, y2) + result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2) for p2 in self.players: - p2.update(str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)) # Inform players of what happened + p2.update(result) # Inform players of what happened + + log(result) if isinstance(graphics, GraphicsThread): with graphics.lock: @@ -110,10 +125,80 @@ class GameThread(StoppableThread): for p2 in self.players: p2.quit(self.final_result) + log(self.final_result) + graphics.stop() +# A thread that replays a log file +class ReplayThread(GameThread): + def __init__(self, players, src): + self.board = Board(style="agent") + GameThread.__init__(self, self.board, players) + self.src = src + + self.ended = False + + def run(self): + i = 0 + phase = 0 + for line in self.src: + + if self.stopped(): + self.ended = True + break + + with self.lock: + self.state["turn"] = self.players[i] + + line = line.split(":") + result = line[len(line)-1].strip(" \r\n") + log(result) + + try: + self.board.update(result) + except: + self.ended = True + self.final_result = result + if isinstance(graphics, GraphicsThread): + graphics.stop() + break + + [x,y] = map(int, result.split(" ")[0:2]) + target = self.board.grid[x][y] + + if isinstance(graphics, GraphicsThread): + if phase == 0: + with graphics.lock: + graphics.state["moves"] = self.board.possible_moves(target) + graphics.state["select"] = target + + time.sleep(turn_delay) + + elif phase == 1: + [x2,y2] = map(int, result.split(" ")[3:5]) + with graphics.lock: + graphics.state["moves"] = [[x2,y2]] + + time.sleep(turn_delay) + + with graphics.lock: + graphics.state["select"] = None + graphics.state["dest"] = None + graphics.state["moves"] = None + + + + + + for p in self.players: + p.update(result) + + phase = (phase + 1) % 2 + if phase == 0: + i = (i + 1) % 2 + def opponent(colour): if colour == "white":