Пример #1
0
    def setUp(self):
        # Ship parameters
        length = 5
        orientation = 0
        prow = (0, 0)
        random = True

        # Random positionning
        if random:
            length = rd.choice([3, 5])
            orientation = rd.randint(2)
            prow = (max(rd.randint(10) - length * orientation, 0),
                    max(rd.randint(10) - length * (1 - orientation), 0))

        self.ship = Ship(length=length, orientation=orientation, prow=prow)
        pass
Пример #2
0
    def test_valid_position(self):
        orientation = 0  #horizontal
        length = 5
        prow = (2, 3)
        ship = Ship(length=length, orientation=orientation, prow=prow)
        expPosition = {(2, 3), (2, 4), (2, 5), (2, 6), (2, 7)}

        self.assertEqual(ship.position, expPosition)
        pass
Пример #3
0
    def test_bottom_left_corner(self):
        length = self.length
        orientation = self.orientation
        prow = (0, 0)
        ship = Ship(length=length, orientation=orientation, prow=prow)

        if orientation:
            surrArea = {(k, 1) for k in range(length + 1)}.union({(length, 0)})
            self.assertEqual(ship.surrArea, surrArea)

        else:
            surrArea = {(1, k) for k in range(length + 1)}.union({(0, length)})
            self.assertEqual(ship.surrArea, surrArea)
        pass
Пример #4
0
class TestIsSunk(unittest.TestCase):
    def setUp(self):
        # Ship parameters
        length = 5
        orientation = 0
        prow = (0, 0)
        random = True

        # Random positionning
        if random:
            length = rd.choice([3, 5])
            orientation = rd.randint(2)
            prow = (max(rd.randint(10) - length * orientation, 0),
                    max(rd.randint(10) - length * (1 - orientation), 0))

        self.ship = Ship(length=length, orientation=orientation, prow=prow)
        pass

    def test_hit_all_ship_cells(self):
        hits = self.ship.position

        self.assertTrue(self.ship.is_sunk(hits=hits))
        pass

    def test_hit_only_part_ship_cells(self):
        position = list(self.ship.position)
        hits = {position[0], position[2]}

        self.assertFalse(self.ship.is_sunk(hits=hits))
        pass

    def test_hit_no_cells(self):
        hits = {}

        self.assertFalse(self.ship.is_sunk(hits=hits))
        pass
Пример #5
0
    def test_bottom_right_corner(self):
        length = self.length
        orientation = self.orientation
        prow = (0, 9 - (length - 1) * (1 - orientation))
        ship = Ship(length=length, orientation=orientation, prow=prow)

        if orientation:
            surrArea = {(k, 8) for k in range(length + 1)}.union({(length, 9)})
            self.assertEqual(ship.surrArea, surrArea)

        else:
            surrArea = {(1, 9 - k)
                        for k in range(length + 1)
                        }.union({(0, 10 - (length + 1))})
            self.assertEqual(ship.surrArea, surrArea)
        pass
Пример #6
0
    def test_top_left_corner(self):
        length = self.length
        orientation = self.orientation
        prow = (9 - (length - 1) * orientation, 0)
        ship = Ship(length=length, orientation=orientation, prow=prow)

        if orientation:
            surrArea = {(9 - k, 1)
                        for k in range(length + 1)}.union({(10 - (length + 1),
                                                            0)})
            self.assertEqual(ship.surrArea, surrArea)

        else:
            surrArea = {(8, k) for k in range(length + 1)}.union({(9, length)})
            self.assertEqual(ship.surrArea, surrArea)
        pass