Exemplo n.º 1
0
class Game:
    """
    Class to represent a game instance.
    """
    def __init__(self, whites, blacks, gameIndex):
        self.game_id = gameIndex
        self.validMovesCounter = 0
        self.white = whites
        self.white.game_id = self.game_id
        self.white.game = self
        self.black = blacks
        self.black.game_id = self.game_id
        self.black.game = self
        self.chessBoard = ChessBoard()
        self.lightBoard = LightBoard()
        self.notifyReady()

    def notifyReady(self):
        self.sendMessageToAll({"type": "status", "status": "ready"})
        self.white.state = "PLAYING"
        self.black.state = "PLAYING"

    def move(self, x1, y1, x2, y2, color):
        piece = self.chessBoard.grid[x1][y1]
        if piece is not None and piece.color is not color:
            raise IllegalMove(
                "Trying to move a place that does not belong to the player.")
        self.chessBoard.move(x1, y1, x2, y2, disp=False)
        self.lightBoard.move(x1, y1, x2, y2)
        self.validMovesCounter += 1

    def autoMove(self):
        return self.chessBoard.auto_move()

    def checkEnd(self, color):
        outcome = self.chessBoard.end_game()
        msg = {"type": "chat", "content": outcome}
        self.sendMessageTo(msg, color)

    def updateLightBoardTask(self):
        for col in [0, 1]:
            for i, piece in enumerate(self.chessBoard.pieces[col]):
                if piece is not None:
                    color = piece.color
                    position = piece.position
                    natures = self.chessBoard.all_legal_natures(piece)
                    pieceIndex = i + col * 24
                    self.lightBoard.setPiece(pieceIndex, color, position,
                                             natures)
        msg = {"type": "lightboard", "description": self.lightBoard.wrapUp()}
        self.sendMessageToAll(msg)

    def updateLightBoard(self):
        """ Schedules an update board task. """
        reactor.callLater(0.5, self.updateLightBoardTask)

    def sendMessageToAll(self, msg):
        self.white.sendMessage(msg)
        self.black.sendMessage(msg)

    def sendMessageTo(self, msg, color):
        if color == 0:
            self.white.sendMessage(msg)
        elif color == 1:
            self.black.sendMessage(msg)
        else:
            pass

    def disconnectPlayers(self):
        self.white.disconnect()
        self.black.disconnect()
Exemplo n.º 2
0
class TwoPlayersOnOneBoard(GameEngine):

    def __init__(self, gameEngine):
        """
        Constructor.
        :param gameEngine: The initial game engine.
        """
        self.chessBoard = ChessBoard()
        self.lightBoard = gameEngine.lightBoard
        self.display = gameEngine.display
        self.loopingCall = gameEngine.loopingCall
        self.validMovesCounter = 0

    @inlineCallbacks
    def updateLightBoard(self):
        nb = self.validMovesCounter
        for col in [0, 1]:
            for i, piece in enumerate(self.chessBoard.pieces[col]):
                if nb == self.validMovesCounter:
                    self.updateDeferred = Deferred()
                    self.updateDeferred.addCallback(self.chessBoard.all_legal_natures)
                    self.updateDeferred.addErrback(log.err)  # DEBUG
                    reactor.callLater(0, self.updateDeferred.callback, piece)
                    natures = yield self.updateDeferred
                    if nb == self.validMovesCounter:
                        color = piece.color
                        position = piece.position
                        pieceIndex = i + col * 24
                        self.lightBoard.setPiece(pieceIndex, color, position, natures)
                        if (piece.position is not None) and (piece.position is not False):
                            self.makeDisplayDrawBoard()
                        elif (piece.position is False):
                            self.display.updatePane()

    def moveTask(self, mov):
        x1, y1, x2, y2 = mov[0], mov[1], mov[2], mov[3]
        try:
            self.chessBoard.move(x1, y1, x2, y2, disp=False)
            self.lightBoard.move(x1, y1, x2, y2)
            color = "Whites" if (self.validMovesCounter % 2 == 0) else "Blacks"
            self.display.addMessage(color + " move from ({},{}) to ({},{})".format(x1, y1, x2, y2))
            self.validMovesCounter += 1
            self.display.setLastMove(x1, y1, x2, y2)
            self.makeDisplayDrawBoard()
            self.updateLightBoard()
        except IllegalMove as e:
            self.handleIllegalMove(str(e))

    def move(self, x1, y1, x2, y2):
        d = Deferred()
        d.addCallback(self.moveTask).addErrback(log.err)  # DEBUG
        reactor.callLater(0, d.callback, (x1, y1, x2, y2))

    def autoMove(self):
        try:
            return self.chessBoard.auto_move()
        except IllegalMove as e: # In case the game has ended
            self.handleIllegalMove(str(e))

    def checkEndTask(self):
        outcome = self.chessBoard.end_game()
        self.display.addMessage(outcome)