X-Git-Url: https://git.ucc.asn.au/?p=progcomp2013.git;a=blobdiff_plain;f=qchess%2Fsrc%2Fgame.py;h=15c53b1315086129fcc8dd21497b1ad7b3393bd9;hp=16f26d72a8699d78f170aa87ac0f5191829f80d4;hb=538a32cb30f21a1a48653d7b639ab0c82db44d83;hpb=3decbfd61b59ee2611700e7fa96368e02f643d4d diff --git a/qchess/src/game.py b/qchess/src/game.py index 16f26d7..15c53b1 100644 --- a/qchess/src/game.py +++ b/qchess/src/game.py @@ -5,7 +5,7 @@ # 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 @@ -14,8 +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): @@ -24,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) @@ -63,21 +79,43 @@ 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 - - result = str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2) - log(result) - - self.board.update_move(x, y, x2, y2) + 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 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) + @@ -93,26 +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 - - end = self.board.end_condition() - if end != None: - with self.lock: - if end == "DRAW": - self.final_result = self.state["turn"].colour + " " + end - else: - self.final_result = end - self.stop() + + end = self.board.end_condition() + if end != None: + with self.lock: + if end == "DRAW": + self.final_result = self.state["turn"].colour + " " + end + else: + self.final_result = end + self.stop() - if self.stopped(): - break + 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 = self.state["turn"].colour + " " + e.message + break + for p2 in self.players: @@ -180,6 +224,8 @@ class ReplayThread(GameThread): if self.stopped(): break + if len(line) <= 0: + continue if line[0] == '#': @@ -227,7 +273,10 @@ class ReplayThread(GameThread): 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) + if target.current_type != "unknown": + graphics.state["moves"] = self.board.possible_moves(target) + else: + graphics.state["moves"] = None time.sleep(turn_delay) else: self.board.update_move(x, y, x2, y2)