X-Git-Url: https://git.ucc.asn.au/?p=progcomp2013.git;a=blobdiff_plain;f=qchess%2Fsrc%2Fgame.py;h=15c53b1315086129fcc8dd21497b1ad7b3393bd9;hp=e3f1c7cd3bdca0d421c952184836819f9f1b02ac;hb=538a32cb30f21a1a48653d7b639ab0c82db44d83;hpb=877034f05346e24fdf822f6e6149ad50d891f030 diff --git a/qchess/src/game.py b/qchess/src/game.py index e3f1c7c..15c53b1 100644 --- a/qchess/src/game.py +++ b/qchess/src/game.py @@ -1,18 +1,11 @@ -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): + def __init__(self, board, players, server = True): StoppableThread.__init__(self) self.board = board self.players = players @@ -21,6 +14,11 @@ class GameThread(StoppableThread): self.lock = threading.RLock() #lock for access of self.state self.cond = threading.Condition() # conditional for some reason, I forgot self.final_result = "" + self.server = server + self.retry_illegal = False + + + # Run the game (run in new thread with start(), run in current thread with run()) def run(self): @@ -29,22 +27,35 @@ class GameThread(StoppableThread): for p in self.players: with self.lock: - if isinstance(p, NetworkSender): - self.state["turn"] = p.base_player # "turn" contains the player who's turn it is - else: - self.state["turn"] = p - #try: - if True: + self.state["turn"] = p.base_player() + try: + #if True: [x,y] = p.select() # Player selects a square if self.stopped(): + #debug("Quitting in select") break - - - - result = self.board.select(x, y, colour = p.colour) + if isinstance(p, NetworkPlayer): + if p.network.server == True: + result = self.board.select(x, y, colour = p.colour) + else: + result = None + + else: + result = self.board.select(x, y, colour = p.colour) + + result = p.update(result) + if self.stopped(): + break for p2 in self.players: + if p2 == p: + continue p2.update(result) # Inform players of what happened + if self.stopped(): + break + + if self.stopped(): + break log(result) @@ -68,21 +79,46 @@ class GameThread(StoppableThread): graphics.state["dest"] = None continue - try: - [x2,y2] = p.get_move() # Player selects a destination - except: - self.stop() + #try: + [x2,y2] = p.get_move() # Player selects a destination + #except: + # self.stop() + if self.stopped(): + #debug("Quitting in get_move") + break + + if isinstance(p, NetworkPlayer): + if p.network.server == True: + result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2) + self.board.update_move(x, y, x2, y2) + else: + result = None + + else: + result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2) + self.board.update_move(x, y, x2, y2) + + result = p.update(result) if self.stopped(): break - - self.board.update_move(x, y, x2, y2) - result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2) for p2 in self.players: + if p2 == p: + continue p2.update(result) # Inform players of what happened - + if self.stopped(): + break + + if self.stopped(): + break + + + log(result) + + + if isinstance(graphics, GraphicsThread): with graphics.lock: graphics.state["moves"] = [[x2,y2]] @@ -95,31 +131,32 @@ class GameThread(StoppableThread): graphics.state["dest"] = None graphics.state["moves"] = None - # Commented out exception stuff for now, because it makes it impossible to tell if I made an IndentationError somewhere - # except Exception,e: - # result = e.message - # #sys.stderr.write(result + "\n") - # - # self.stop() - # with self.lock: - # self.final_result = self.state["turn"].colour + " " + e.message - - if self.board.king["black"] == None: - if self.board.king["white"] == None: + + end = self.board.end_condition() + if end != None: with self.lock: - self.final_result = self.state["turn"].colour + " DRAW" + if end == "DRAW": + self.final_result = self.state["turn"].colour + " " + end + else: + self.final_result = end + self.stop() + + if self.stopped(): + break + except Exception,e: + #if False: + + + result = e.message + if self.retry_illegal: + self.state["turn"].update(result); else: + sys.stderr.write("qchess.py exception: "+result + "\n") + self.stop() with self.lock: - self.final_result = "white" - self.stop() - elif self.board.king["white"] == None: - with self.lock: - self.final_result = "black" - self.stop() - + self.final_result = self.state["turn"].colour + " " + e.message + break - if self.stopped(): - break for p2 in self.players: @@ -127,76 +164,182 @@ class GameThread(StoppableThread): log(self.final_result) - graphics.stop() + if isinstance(graphics, GraphicsThread): + graphics.stop() # A thread that replays a log file class ReplayThread(GameThread): - def __init__(self, players, src): - self.board = Board(style="agent") + 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.end = end + + self.reset_board(self.src.readline()) + + def reset_board(self, line): + agent_str = "" + self_str = "" + while line != "# Start game" and line != "# EOF": + + while line == "": + line = self.src.readline().strip(" \r\n") + continue + + if line[0] == '#': + line = self.src.readline().strip(" \r\n") + 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) - self.ended = False def run(self): - i = 0 - phase = 0 - for line in self.src: + move_count = 0 + last_line = "" + line = self.src.readline().strip(" \r\n") + while line != "# EOF": + if self.stopped(): - self.ended = True break + + if len(line) <= 0: + continue + - with self.lock: - self.state["turn"] = self.players[i] - - line = line.split(":") - result = line[len(line)-1].strip(" \r\n") - log(result) - + if line[0] == '#': + last_line = line + line = self.src.readline().strip(" \r\n") + continue + + 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) + [x,y] = map(int, tokens[0:2]) except: - self.ended = True - self.final_result = result - if isinstance(graphics, GraphicsThread): - graphics.stop() + last_line = line + self.stop() break - [x,y] = map(int, result.split(" ")[0:2]) + log(move) + 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 target.current_type != "unknown": + graphics.state["moves"] = self.board.possible_moves(target) + else: + graphics.state["moves"] = None time.sleep(turn_delay) - - elif phase == 1: - [x2,y2] = map(int, result.split(" ")[3:5]) + else: + self.board.update_move(x, y, x2, y2) + if isinstance(graphics, GraphicsThread): 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 + 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 + + + + if self.end and isinstance(graphics, GraphicsThread): + #graphics.stop() + pass # Let the user stop the display + 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