예제 #1
0
def test_shoot_valid():
    board = Board(BOARD_SIZE)

    board.shoot(Point(7, 4))
    board.shoot(Point(0, BOARD_SIZE - 1))

    assert board.shot_locations == {Point(7, 4), Point(0, BOARD_SIZE - 1)}
예제 #2
0
def test_shoot_invalid_already_shot():
    board = Board(BOARD_SIZE)

    board.shoot(Point(1, 1))
    board.shoot(Point(5, 2))

    with pytest.raises(InvalidShotException):
        board.shoot(Point(1, 1))
예제 #3
0
def test_valid_place_ship():
    board = Board(BOARD_SIZE)
    board.place_ship(Battleship(), Point(4, 5), Orientation.Horizontal)

    assert board.all_ship_locations == {
        Point(4, 5), Point(5, 5),
        Point(6, 5), Point(7, 5)
    }
예제 #4
0
 def __init__(self):
     super().__init__()
     self.horizontal_offsets = [
         Point(0, 0),
         Point(1, 0),
         Point(2, 0),
         Point(3, 0),
         Point(4, 0),
     ]
     self.vertical_offsets = [
         Point(0, 0),
         Point(0, 1),
         Point(0, 2),
         Point(0, 3),
         Point(0, 4),
     ]
예제 #5
0
 def get_points(self, point: Point,
                orientation: Orientation) -> FrozenSet[Point]:
     """ Returns a set of points corresponding to the positions the ship will occupy on the game board """
     if orientation == Orientation.Horizontal:
         return frozenset([
             Point(*map(operator.add, point, p))
             for p in self.horizontal_offsets
         ])
     elif orientation == Orientation.Vertical:
         return frozenset([
             Point(*map(operator.add, point, p))
             for p in self.vertical_offsets
         ])
     else:
         raise InvalidShipPlacementException(
             f"{orientation} is not a valid Orientation type. ")
예제 #6
0
    def point_in_board(point: Point):
        """
        Checks to see if 'point' is within the board

        :param point: Tuple
        :return: bool
        """
        return point in frozenset([
            Point(x, y) for x in range(BOARD_SIZE) for y in range(BOARD_SIZE)
        ])
예제 #7
0
def test_game_is_won():
    board = Board(BOARD_SIZE)

    assert board.is_board_lost() is False

    board.place_ship(Destroyer(), Point(5, 1), Orientation.Horizontal)

    assert board.is_board_lost() is False

    board.shoot(Point(5, 1))

    assert board.is_board_lost() is False

    board.shoot(Point(5, 2))

    assert board.is_board_lost() is True

    board.shoot(Point(BOARD_SIZE - 1, BOARD_SIZE - 1))

    assert board.is_board_lost() is True
예제 #8
0
    def is_valid_ship_placement(
            placements: List[Tuple[Ship, Point, Orientation]]) -> bool:
        """
        A static helper function that checks to see if ship placements are valid

        :param placements:
        :return:
        """

        all_points = set()

        for ship, point, orientation in placements:
            all_points.update(ship.get_points(point, orientation))

        # Check there are no overlapping placements
        if not len(all_points) == sum([len(s[0]) for s in placements]):
            return False

        # Check all points are within the board
        return all_points.issubset(
            set([
                Point(x, y) for x in range(BOARD_SIZE)
                for y in range(BOARD_SIZE)
            ]))
예제 #9
0
 def board(self):
     return frozenset([
         Point(x, y) for x in range(self.board_size)
         for y in range(self.board_size)
     ])
예제 #10
0
 def __init__(self):
     super().__init__()
     self.horizontal_offsets = [Point(0, 0), Point(0, 1)]
     self.vertical_offsets = [Point(0, 0), Point(1, 0)]
예제 #11
0
def test_shoot_invalid_off_board():
    board = Board(BOARD_SIZE)

    with pytest.raises(InvalidShotException):
        board.shoot(Point(BOARD_SIZE, BOARD_SIZE))
예제 #12
0
def test_invalid_place_ship():
    board = Board(BOARD_SIZE)

    with pytest.raises(InvalidShipPlacementException):
        board.place_ship(Battleship(), Point(BOARD_SIZE - 1, BOARD_SIZE - 1),
                         Orientation.Vertical)