X-Git-Url: https://git.ucc.asn.au/?p=progcomp2013.git;a=blobdiff_plain;f=qchess%2Fsrc%2Fgame.py;fp=qchess%2Fsrc%2Fgame.py;h=934c043d5cbd8f63ecefadc59e887a7f24db46e8;hp=0000000000000000000000000000000000000000;hb=444244d5c7698bb7861cdb7c0ec6bfb0e8cebfb7;hpb=707e794d26062516eb4188d1cd2902929613c46b diff --git a/qchess/src/game.py b/qchess/src/game.py new file mode 100644 index 0000000..934c043 --- /dev/null +++ b/qchess/src/game.py @@ -0,0 +1,122 @@ + +# A thread that runs the game +class GameThread(StoppableThread): + def __init__(self, board, players): + StoppableThread.__init__(self) + self.board = board + self.players = players + self.state = {"turn" : None} # The game state + self.error = 0 # Whether the thread exits with an error + self.lock = threading.RLock() #lock for access of self.state + self.cond = threading.Condition() # conditional for some reason, I forgot + self.final_result = "" + + # Run the game (run in new thread with start(), run in current thread with run()) + def run(self): + result = "" + while not self.stopped(): + + 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: + [x,y] = p.select() # Player selects a square + if self.stopped(): + break + + + + + result = self.board.select(x, y, colour = p.colour) + for p2 in self.players: + p2.update(result) # Inform players of what happened + + + + target = self.board.grid[x][y] + if isinstance(graphics, GraphicsThread): + with graphics.lock: + graphics.state["moves"] = self.board.possible_moves(target) + graphics.state["select"] = target + + time.sleep(turn_delay) + + + if len(self.board.possible_moves(target)) == 0: + #print "Piece cannot move" + target.deselect() + if isinstance(graphics, GraphicsThread): + with graphics.lock: + graphics.state["moves"] = None + graphics.state["select"] = None + graphics.state["dest"] = None + continue + + try: + [x2,y2] = p.get_move() # Player selects a destination + except: + self.stop() + + if self.stopped(): + break + + result = self.board.update_move(x, y, x2, y2) + for p2 in self.players: + p2.update(str(x) + " " + str(y) + " -> " + str(x2) + " " + str(y2)) # Inform players of what happened + + if isinstance(graphics, GraphicsThread): + with graphics.lock: + graphics.state["moves"] = [[x2,y2]] + + time.sleep(turn_delay) + + if isinstance(graphics, GraphicsThread): + with graphics.lock: + graphics.state["select"] = None + 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: + 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: + with self.lock: + self.final_result = "black" + self.stop() + + + if self.stopped(): + break + + + for p2 in self.players: + p2.quit(self.final_result) + + graphics.stop() + + + + +def opponent(colour): + if colour == "white": + return "black" + else: + return "white"