def get_arrange_ships() -> list: ships_to_place = [] for _ in range(FOUR_DECK_COUNT): ships_to_place.append(Ship(4)) for _ in range(THREE_DECK_COUNT): ships_to_place.append(Ship(3)) for _ in range(TWO_DECK_COUNT): ships_to_place.append(Ship(2)) for _ in range(SINGLE_DECK_COUNT): ships_to_place.append(Ship(1)) while True: auxiliary_field = Field() for ship in ships_to_place: for _ in range(ATTEMPTS_PLACE_SHIP): point = (random.randint(0, auxiliary_field.width), random.randint(0, auxiliary_field.height)) if random.randint(0, 2) == 1: ship.rotate() if auxiliary_field.can_place_ship_on(ship, point): auxiliary_field.ships.append(ship) ship.placed_location = point break if len(auxiliary_field.ships) == len(ships_to_place): return auxiliary_field.ships
def test_rotate(self): ship = Ship(4) self.placed_location = (0, 0) ship.rotate() cells = ship.get_occupied_cells() self.assertEqual(len(cells), 4) self.assertEqual(cells[0], (0, 0)) self.assertEqual(cells[3], (3, 0))
def test_can_place_ship_on(self): field = Field() field.reset() self.assertFalse(field.can_place_ship_on(None, (0, 0))) field.try_place_new_ship((0, 0)) self.assertFalse(field.can_place_ship_on(Ship(1), (-1, 0))) self.assertFalse(field.can_place_ship_on(Ship(1), (0, 0))) self.assertFalse(field.can_place_ship_on(Ship(1), (1, 1))) self.assertTrue(field.can_place_ship_on(Ship(1), (1, 2)))
def test_get_occupied_cells(self): ship = Ship(4) self.placed_location = (0, 0) cells = ship.get_occupied_cells() self.assertEqual(len(cells), 4) self.assertEqual(cells[0], (0, 0)) self.assertEqual(cells[3], (0, 3)) cells = ship.get_occupied_cells((1, 1)) self.assertEqual(len(cells), 4) self.assertEqual(cells[0], (1, 1)) self.assertEqual(cells[3], (1, 4))
def reset(self): self.ships = [] self.ships_to_place = [] self.shots = [] for _ in range(SINGLE_DECK_COUNT): self.ships_to_place.append(Ship(1)) for _ in range(TWO_DECK_COUNT): self.ships_to_place.append(Ship(2)) for _ in range(THREE_DECK_COUNT): self.ships_to_place.append(Ship(3)) for _ in range(FOUR_DECK_COUNT): self.ships_to_place.append(Ship(4))
def try_remove_ship(self, location: Tuple[int, int]) -> bool: ship_to_remove = self.get_ship_on(location) if ship_to_remove is None: return False self.ships.remove(ship_to_remove) self.ships_to_place.append(Ship(len(ship_to_remove.occupied_cells))) return True
def test___contains__(self): ship = Ship(4) self.placed_location = (0, 0) self.assertTrue((0, 0) in ship) self.assertTrue((0, 1) in ship) self.assertTrue((0, 2) in ship) self.assertTrue((0, 3) in ship) self.assertFalse((0, 4) in ship)
def can_place_ship_on(self, ship: Ship, location: Tuple[int, int]) -> bool: if ship is None: return False cells_to_check = ship.get_occupied_cells(location) for cell in cells_to_check: if not (0 <= cell[0] < self.height and 0 <= cell[1] < self.height): return False for x in range(-1, 2): for y in range(-1, 2): if self.get_ship_on( (cell[0] + x, cell[1] + y)) is not None: return False return True
def test_on_shot(self): ship = Ship(4) self.assertTrue(ship.on_shot((0, 0))) self.assertEqual(len(ship.affected_cells), 1) self.assertFalse(ship.on_shot((0, 0))) self.assertEqual(len(ship.affected_cells), 1)
def test_get_affected_cells(self): ship = Ship(4) self.assertTrue(ship.on_shot((0, 0))) self.assertEqual(len(ship.get_affected_cells()), 1)
def test_has_die(self): ship = Ship(1) self.assertFalse(ship.has_die()) ship.on_shot((0, 0)) self.assertTrue(ship.has_die())
def test_has_all_ships_die(self): field = Field() field.ships.append(Ship(1)) self.assertFalse(field.has_all_ships_die()) field.ships[0].on_shot((0, 0)) self.assertTrue(field.has_all_ships_die())
def test_get_cell_type(self): field = Field() point1 = (0, 0) point2 = (2, 0) ship = Ship(1) ship.placed_location = point2 field.ships.append(ship) ship.on_shot(point2) point3 = (4, 0) ship = Ship(1) ship.placed_location = point3 field.ships.append(ship) point4 = (6, 0) ship = Ship(2) ship.rotate() ship.placed_location = point4 field.ships.append(ship) ship.on_shot(point4) point5 = (8, 0) field.shot(point5) self.assertEqual(field.get_cell_type(point1, True), Cell.Empty) self.assertEqual(field.get_cell_type(point2, True), Cell.FullDeadShip) self.assertEqual(field.get_cell_type(point3, True), Cell.ShipPeace) self.assertEqual(field.get_cell_type(point4, True), Cell.DeadShipPeace) self.assertEqual(field.get_cell_type(point5, True), Cell.Shot) self.assertEqual(field.get_cell_type(point5, False), Cell.Empty) self.assertEqual(field.get_cell_type(point2, False), Cell.ShipPeace)