Example #1
0
    def setUp(self):
        super(TestGrid, self).setUp()

        grid_size = GridSize.small
        fleet_size_multiplier = EnumConverters.\
            grid_size_to_fleet_size_multiplier_converter(grid_size)
        self.dimensions = grid_size.value
        self.grid = Grid(GridPosition.left, grid_size)
        self.fleet = Fleet(fleet_size_multiplier)
        self.player = Human.default()
Example #2
0
class TestGrid(unittest.TestCase):

    """Used to test the Grid class.

    """
    def setUp(self):
        super(TestGrid, self).setUp()

        grid_size = GridSize.small
        fleet_size_multiplier = EnumConverters.\
            grid_size_to_fleet_size_multiplier_converter(grid_size)
        self.dimensions = grid_size.value
        self.grid = Grid(GridPosition.left, grid_size)
        self.fleet = Fleet(fleet_size_multiplier)
        self.player = Human.default()

    def test_initialize(self):
        squares = self.grid.squares

        # size should equal dimensions to the power of two
        self.assertEqual(squares.size, self.dimensions ** 2)

        for index, value in np.ndenumerate(squares):
            current_square = value

            # square is initially set as "vacant"
            self.assertEqual(current_square.square_state, SquareState.vacant)
            self.assertEqual(current_square.coordinates, Coordinates(*index))

    def test_can_surround_ship_squares(self):
        # aircraft_carrier
        ship = self.fleet.ships[0]
        submatrix = Submatrix(0, 1, 0, 6)

        first_test = self.grid.can_surround_ship_squares(submatrix)
        self.assertTrue(first_test)

        self.fleet.position_ship(self.grid,
                                 ship,
                                 ShipOrientation.horizontal,
                                 Coordinates(0, 0))

        second_test = self.grid.can_surround_ship_squares(submatrix)
        self.assertFalse(second_test)

    def test_get_available_starting_squares(self):
        # aircraft_carrier
        ship = self.fleet.ships[0]

        # the aircraft carrier fits on exactly 50 starting squares
        # (horizontally, on the small-sized grid)
        first_test = self.grid.get_available_starting_squares(
            ship,
            ShipOrientation.horizontal
        )
        self.assertEqual(len(first_test), 50)

        self.fleet.position_ship(self.grid,
                                 ship,
                                 ShipOrientation.horizontal,
                                 Coordinates(0, 0))

        second_test = self.grid.get_available_starting_squares(
            ship,
            ShipOrientation.horizontal
        )
        self.assertLess(len(second_test), 50)

    def test_get_complete_occupiable_squares(self):
        # aircraft_carrier
        ship = self.fleet.ships[0]

        # the aircraft carrier (or any ship for that matter) can initially be
        # found on all of the squares
        first_test = self.grid.get_complete_occupiable_squares(
            ship,
            ShipOrientation.horizontal
        )
        self.assertEqual(len(first_test), 100)

        self.player.shoot(self.grid, Coordinates(0, 0))

        second_test = self.grid.get_complete_occupiable_squares(
            ship,
            ShipOrientation.horizontal
        )
        self.assertLess(len(second_test), 100)

    def test_get_non_hit_squares_coordinates(self):
        ships = self.fleet.ships

        # initially, the ships can be found anywhere on the grid
        first_test = self.grid.get_non_hit_squares_coordinates(ships)
        self.assertEqual(len(first_test), 100)

        self.player.shoot(self.grid, Coordinates(0, 0))

        second_test = self.grid.get_non_hit_squares_coordinates(ships)
        self.assertLess(len(second_test), 100)

    def test_get_surrounding_squares_coordinates(self):
        # square at this position should be surrounded by exactly
        # four vacant squares
        first_test = self.grid.get_surrounding_squares_coordinates(
            Coordinates(5, 5),
            lambda square_state: square_state == SquareState.vacant
        )
        self.assertEqual(len(first_test), 4)

        self.player.shoot(self.grid, Coordinates(4, 5))

        second_test = self.grid.get_surrounding_squares_coordinates(
            Coordinates(5, 5),
            lambda square_state: square_state == SquareState.vacant
        )
        self.assertLess(len(second_test), 4)

    def test_get_ship_submatrix(self):
        # aircraft_carrier
        ship = self.fleet.ships[0]
        submatrix = Submatrix(0, 1, 0, 6)

        test = self.grid.get_ship_submatrix(ship,
                                            ShipOrientation.horizontal,
                                            Coordinates(0, 0))
        self.assertEqual(test, submatrix)

    def test_is_submatrix_within_bounds(self):
        submatrix_one = Submatrix(0, 1, 0, 6)
        submatrix_two = Submatrix(4, 5, 4, 10)

        first_test = self.grid.is_submatrix_within_bounds(
            submatrix_one,
            ShipOrientation.horizontal
        )
        self.assertTrue(first_test)

        second_test = self.grid.is_submatrix_within_bounds(
            submatrix_two,
            ShipOrientation.horizontal
        )
        self.assertFalse(second_test)

    def test_can_fit(self):
        # aircraft_carrier
        ship = self.fleet.ships[0]
        submatrix = Submatrix(0, 1, 0, 6)

        first_test = self.grid.can_fit(ship,
                                       ShipOrientation.horizontal,
                                       Coordinates(0, 0),
                                       submatrix)
        self.assertTrue(first_test)

        second_test = self.grid.can_fit(ship,
                                        ShipOrientation.horizontal,
                                        Coordinates(9, 9),
                                        submatrix)
        self.assertFalse(second_test)

    def test_get_occupiable_ship_squares(self):
        # aircraft_carrier
        ship = self.fleet.ships[0]

        test = self.grid.get_occupiable_ship_squares(
            ship,
            ShipOrientation.horizontal,
            Coordinates(0, 0)
        )
        self.assertEqual(len(test), 6)

    def test_resolve_shooting_direction(self):
        shooting_direction = ShootingDirection.right

        # simulate shooting the nearest square to the left
        self.player.shoot(self.grid, Coordinates(5, 4))

        test = self.grid.resolve_shooting_direction(
            self.grid.squares.item((5, 5)),
            ShipOrientation.horizontal
        )
        self.assertEqual(test, shooting_direction)

    def test_get_square_coordinates_directed(self):
        # aircraft_carrier
        ship = self.fleet.ships[0]
        coordinates = Coordinates(0, 2)

        self.fleet.position_ship(self.grid,
                                 ship,
                                 ShipOrientation.horizontal,
                                 Coordinates(0, 0))

        # simulate hitting a ship
        self.player.shoot(self.grid, Coordinates(0, 1))

        test = self.grid.get_square_coordinates_directed(
            Coordinates(0, 1),
            ShootingDirection.right,
            lambda square_state: square_state != SquareState.hit
        )

        self.assertEqual(test, coordinates)