X-Git-Url: https://git.ucc.asn.au/?p=progcomp2013.git;a=blobdiff_plain;f=qchess%2Fsrc%2Fgame.py;h=16f26d72a8699d78f170aa87ac0f5191829f80d4;hp=a62e280a6efc46a268d2926850ecc7f22270b133;hb=3decbfd61b59ee2611700e7fa96368e02f643d4d;hpb=0325af7e6f9c20b549efa84754b4a4a206fe66cc diff --git a/qchess/src/game.py b/qchess/src/game.py index a62e280..16f26d7 100644 --- a/qchess/src/game.py +++ b/qchess/src/game.py @@ -71,15 +71,15 @@ class GameThread(StoppableThread): if self.stopped(): break - if isinstance(log_file, HttpLog): - log_file.prelog() + result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2) + log(result) self.board.update_move(x, y, x2, y2) - result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2) + for p2 in self.players: p2.update(result) # Inform players of what happened - log(result) + if isinstance(graphics, GraphicsThread): with graphics.lock: @@ -102,20 +102,15 @@ class GameThread(StoppableThread): # with self.lock: # self.final_result = self.state["turn"].colour + " " + e.message - if self.board.king["black"] == None: - if self.board.king["white"] == None: - with self.lock: - self.final_result = self.state["turn"].colour + " DRAW" - else: - with self.lock: - self.final_result = "white" - self.stop() - elif self.board.king["white"] == None: + end = self.board.end_condition() + if end != None: with self.lock: - self.final_result = "black" + if end == "DRAW": + self.final_result = self.state["turn"].colour + " " + end + else: + self.final_result = end self.stop() - - + if self.stopped(): break @@ -131,129 +126,171 @@ class GameThread(StoppableThread): # A thread that replays a log file class ReplayThread(GameThread): - def __init__(self, players, src, end=False,max_lines=None): + def __init__(self, players, src, end=False,max_moves=None): self.board = Board(style="empty") + self.board.max_moves = max_moves GameThread.__init__(self, self.board, players) self.src = src - self.max_lines = max_lines - self.line_number = 0 self.end = end - self.setup() + self.reset_board(self.src.readline()) - def setup(self): - sys.stderr.write("setup called for ReplayThread\n") - if True: - while self.src.readline().strip(" \r\n") != "# Initial board": - self.line_number += 1 - - line = self.src.readline().strip(" \r\n") + def reset_board(self, line): + agent_str = "" + self_str = "" + while line != "# Start game" and line != "# EOF": - while line != "# Start game": - #print "Reading line " + str(line) - self.line_number += 1 - [x,y] = map(int, line.split("at")[1].strip(" \r\n").split(",")) - colour = line.split(" ")[0] - current_type = line.split(" ")[1] - types = map(lambda e : e.strip(" [],'"), line.split(" ")[2:4]) - p = Piece(colour, x, y, types) - if current_type != "unknown": - p.current_type = current_type - p.choice = types.index(current_type) - - self.board.pieces[colour].append(p) - self.board.grid[x][y] = p - if current_type == "king": - self.board.king[colour] = p + while line == "": + line = self.src.readline().strip(" \r\n") + continue + if line[0] == '#': line = self.src.readline().strip(" \r\n") - - #except Exception, e: - # raise Exception("FILE line: " + str(self.line_number) + " \""+str(line)+"\"") #\n" + e.message) + continue + + self_str += line + "\n" + + if self.players[0].name == "dummy" and self.players[1].name == "dummy": + line = self.src.readline().strip(" \r\n") + continue + + tokens = line.split(" ") + types = map(lambda e : e.strip("[] ,'"), tokens[2:4]) + for i in range(len(types)): + if types[i][0] == "?": + types[i] = "unknown" + + agent_str += tokens[0] + " " + tokens[1] + " " + str(types) + " ".join(tokens[4:]) + "\n" + line = self.src.readline().strip(" \r\n") + + for p in self.players: + p.reset_board(agent_str) + + + self.board.reset_board(self_str) + def run(self): - i = 0 - phase = 0 - count = 0 + move_count = 0 + last_line = "" line = self.src.readline().strip(" \r\n") while line != "# EOF": - sys.stderr.write(sys.argv[0] + " : " + str(self.__class__.__name__) + " read: " + str(line) + "\n") - count += 1 - if self.max_lines != None and count > self.max_lines: - self.stop() + if self.stopped(): break + + - with self.lock: - self.state["turn"] = self.players[i] + if line[0] == '#': + last_line = line + line = self.src.readline().strip(" \r\n") + continue - line = line.split(":") - result = line[len(line)-1].strip(" \r\n") - + tokens = line.split(" ") + if tokens[0] == "white" or tokens[0] == "black": + self.reset_board(line) + last_line = line + line = self.src.readline().strip(" \r\n") + continue + move = line.split(":") + move = move[len(move)-1].strip(" \r\n") + tokens = move.split(" ") + + try: - self.board.update(result) - except Exception, e: - sys.stderr.write("Exception! " + str(e.message) + "\n") - self.final_result = result + [x,y] = map(int, tokens[0:2]) + except: + last_line = line self.stop() break - log(result) + log(move) - [x,y] = map(int, result.split(" ")[0:2]) target = self.board.grid[x][y] + with self.lock: + if target.colour == "white": + self.state["turn"] = self.players[0] + else: + self.state["turn"] = self.players[1] + + move_piece = (tokens[2] == "->") + if move_piece: + [x2,y2] = map(int, tokens[len(tokens)-2:]) if isinstance(graphics, GraphicsThread): - if phase == 0: + with graphics.lock: + graphics.state["select"] = target + + if not move_piece: + self.board.update_select(x, y, int(tokens[2]), tokens[len(tokens)-1]) + if isinstance(graphics, GraphicsThread): with graphics.lock: graphics.state["moves"] = self.board.possible_moves(target) - graphics.state["select"] = target - - if self.end: - time.sleep(turn_delay) - - elif phase == 1: - [x2,y2] = map(int, result.split(" ")[3:5]) + time.sleep(turn_delay) + else: + self.board.update_move(x, y, x2, y2) + if isinstance(graphics, GraphicsThread): with graphics.lock: graphics.state["moves"] = [[x2,y2]] - - if self.end: - time.sleep(turn_delay) - + time.sleep(turn_delay) with graphics.lock: graphics.state["select"] = None - graphics.state["dest"] = None graphics.state["moves"] = None - + graphics.state["dest"] = None + + + + + for p in self.players: + p.update(move) + last_line = line + line = self.src.readline().strip(" \r\n") + + end = self.board.end_condition() + if end != None: + self.final_result = end + self.stop() + break + + + - for p in self.players: - p.update(result) - phase = (phase + 1) % 2 - if phase == 0: - i = (i + 1) % 2 + + + - line = self.src.readline().strip(" \r\n") - sys.stderr.write(sys.argv[0] + " : " + str(self.__class__.__name__) + " finished...\n") + + - if self.max_lines != None and self.max_lines > count: - sys.stderr.write(sys.argv[0] + " : Replaying from file; stopping at last line (" + str(count) + ")\n") - sys.stderr.write(sys.argv[0] + " : (You requested line " + str(self.max_lines) + ")\n") + if self.end and isinstance(graphics, GraphicsThread): #graphics.stop() pass # Let the user stop the display - elif not self.end: + elif not self.end and self.board.end_condition() == None: global game + # Work out the last move + + t = last_line.split(" ") + if t[len(t)-2] == "black": + self.players.reverse() + elif t[len(t)-2] == "white": + pass + elif self.state["turn"] != None and self.state["turn"].colour == "white": + self.players.reverse() + + game = GameThread(self.board, self.players) game.run() - + else: + pass