def test_should_return_false_when_theres_a_not_destroyed_ship_on_board(): board, ship = Board2D(4, 4), Ship("destroyer", 3) ship.hits_taken = 2 # Ship almost destroyed board.ships.append(ship) # Append ship to the board assert not has_all_ships_destroyed(board)
def test_shouldnt_bomb_an_already_bombed_position(): board, position = Board2D(4, 4), Position(0, 0) board.chart[position.x][position.y] = BoardPosition(PositionStatus.BOMBED) with pytest.raises(CannotBombPosition): bomb_position(board, position)
def test_should_return_true_when_all_ships_from_a_board_was_destroyed(): board, ship = Board2D(4, 4), Ship("destroyer", 3) ship.hits_taken = 3 # Ship destroyed board.ships.append(ship) # Append ship to the board assert has_all_ships_destroyed(board)
def test_should_bomb_a_free_position(): board, position = Board2D(4, 4), Position(0, 0) outcome = bomb_position(board, position) assert board.chart[0][0].status == PositionStatus.BOMBED assert not outcome.has_hit_something assert not outcome.has_destroyed_a_ship
def test_should_return_true_when_position_is_bombed_on_board(): board = Board2D(4, 4) board.chart[0][0] = BoardPosition(PositionStatus.BOMBED) assert cannot_occupy_board_in_the_positions( board, [Position(i, j) for i, j in itertools.product(range(4), range(4))])
def __init__(self, name: str, *, game_option: GameOption, length: int = 10, width: int = 10): self.name = name self.board = Board2D(length, width) self.game_option = deepcopy(game_option)
def test_should_raise_exception_that_cannot_occupy_board_position(): board = Board2D(4, 4) # A new board completely free board.chart[1][1] = BoardPosition( PositionStatus.BOMBED) # Occupy position (1,1) with pytest.raises(CannotOccupyPositions): occupy_board_positions_with_ship(board, [Position(1, 1)], Ship("destroyer", 3))
def test_should_occupy_free_positions_from_board_with_ship(): board = Board2D(4, 4) # A new board completely free ship = Ship("destroyer", 3) occupy_board_positions_with_ship(board, [Position(1, 1)], ship) assert board.chart[1][1].status == PositionStatus.OCCUPIED assert board.chart[1][1].ship == ship assert len(board.ships) == 1
def test_should_create_a_2d_board_with_the_given_length_and_width( length: int, width: int): board = Board2D(length, width) assert board.length == length assert board.width == width expected_chart = [[BoardPosition() for _ in range(width)] for _ in range(length)] assert board.chart == expected_chart
def test_should_place_ship_vertically(): board, ship, front_position = Board2D(4, 4), Ship("destroyer", 3), Position(0, 0) place_ship_on_board(ship, board, front_position, ShipDirection.V) expected_board_position = BoardPosition(PositionStatus.OCCUPIED, ship) assert board.chart[0][0] == expected_board_position assert board.chart[1][0] == expected_board_position assert board.chart[2][0] == expected_board_position
def test_should_bomb_and_destroy_an_occupied_position(): board, position, ship = Board2D(4, 4), Position(0, 0), Ship("destroyer", 1) board.chart[position.x][position.y] = BoardPosition( PositionStatus.OCCUPIED, ship) outcome = bomb_position(board, position) assert ship.hits_taken == 1 assert board.chart[0][0].status == PositionStatus.BOMBED assert outcome.has_hit_something assert outcome.has_destroyed_a_ship
def test_shouldnt_bomb_a_position_outside_the_board(): board, position = Board2D(4, 4), Position(5, 5) with pytest.raises(CannotBombPosition): bomb_position(board, position)
def is_position_occuppied(board: Board2D, position: Position) -> bool: return board.status_at(position) == PositionStatus.OCCUPIED
def cannot_occupy_board_in_the_positions( board: Board2D, positions: Sequence[Position]) -> bool: return any(not is_position_inside_the_board(board, position) or board.status_at(position) != PositionStatus.FREE for position in positions)
def has_destroyed_ship_on_position(board: Board2D, position: Position) -> bool: affected_ship = board.ship_at(position) increase_ship_hits_taken(affected_ship) return is_ship_destroyed(affected_ship)
def test_should_return_false_when_board_has_all_positions_free(): board = Board2D(4, 4) # A new board completely free assert not cannot_occupy_board_in_the_positions( board, [Position(i, j) for i, j in itertools.product(range(4), range(4))])
def test_should_return_true_when_given_position_isnt_inside_the_board(): board = Board2D(4, 4) assert cannot_occupy_board_in_the_positions(board, [Position(10, 2)])
def is_position_bombed(board: Board2D, position: Position) -> bool: return board.status_at(position) == PositionStatus.BOMBED