示例#1
0
    def attack(self, coordinate, player):

        if player == 1:
            if len(coordinate) != 2:
                raise CoordinateInvalid()
            Validator.validateCoordinate(coordinate)
            playerCaption = "Player"
            otherPlayer = 2

        else:
            coordinate = random.choice(Board.columns) + str(
                random.randint(0, Board.dimension - 1))
            playerCaption = "Computer"
            otherPlayer = 1

        gameWon = False
        enemyBoard = self.boardRepository.getBoard(otherPlayer, 1)
        targetBoard = self.boardRepository.getBoard(player, 2)
        if enemyBoard.getMark(coordinate) == "+":
            output = "%s hits!" % playerCaption
            targetBoard.markAtCoordinate(coordinate, "X")
            enemyBoard.markAtCoordinate(coordinate, "X")
            if enemyBoard.countShipMarks() == 0:
                gameWon = True
                output = "%s wins!" % playerCaption
        else:
            output = "%s misses!" % playerCaption
            targetBoard.markAtCoordinate(coordinate, "O")
            enemyBoard.markAtCoordinate(coordinate, "O")

        return gameWon, output
示例#2
0
    def addShip(self, coordinates, player: int):
        """
        Adds a ship to the game for the specified player at the given coordinates.
        The rest of the board is unmodified
        If all the ships are places, subsequent calls to this function replace the ships that already exist
        :param coordinates: The coordinates of the ship squares in the form C1L1C2L2C3L3
        :param player: The player that adds the ship
        :raises CoordinateInvalid: If the given string do not represent coordinates or
        the coordinates are out of the board or
        the ship intersects with another ship
        In case an exception is thrown, the board remains unmodified
        """
        if len(coordinates) != 6:
            raise CoordinateInvalid()

        coordinate1 = coordinates[0:2]
        coordinate2 = coordinates[2:4]
        coordinate3 = coordinates[4:6]

        shipCoordinates = [coordinate1, coordinate2, coordinate3]

        Validator.validateCoordinate(coordinate1)
        Validator.validateCoordinate(coordinate2)
        Validator.validateCoordinate(coordinate3)
        Validator.validateShip(coordinate1, coordinate2, coordinate3)

        board = self.boardRepository.getBoard(player, 1)

        bonusShip = None
        if self.shipRepository.length(player) == 2:
            # remove ship to be replaced
            bonusShip = self.shipRepository.getShip(
                self.shipRepository.shipIndex[player], player)
            for coordinate in bonusShip.coordinates:
                board.markAtCoordinate(coordinate, ".")

        for coordinate in shipCoordinates:
            if board.getMark(coordinate) != ".":
                if bonusShip is not None:
                    #  put the bonus ship back
                    for bonusShipCoordinates in bonusShip.coordinates:
                        board.markAtCoordinate(bonusShipCoordinates, "+")

                raise CoordinateInvalid()

        for coordinate in shipCoordinates:
            board.markAtCoordinate(coordinate, "+")

        ship = Ship(coordinate1, coordinate2, coordinate3)

        if self.shipRepository.length(player) != 2:
            self.shipRepository.addShip(ship, player)
        else:
            self.shipRepository.putShip(ship,
                                        self.shipRepository.shipIndex[player],
                                        player)
示例#3
0
 def testCoordinateValidation(self):
     Validator.validateCoordinate("A1")
     Validator.validateCoordinate("B5")
     with self.assertRaises(CoordinateInvalid):
         Validator.validateCoordinate("a0")
     with self.assertRaises(CoordinateInvalid):
         Validator.validateCoordinate("f6")
     with self.assertRaises(CoordinateInvalid):
         Validator.validateCoordinate("123")
     with self.assertRaises(CoordinateInvalid):
         Validator.validateCoordinate("r4")