コード例 #1
0
    def test_get_suitable_cells(self):
        base = SeaField(3, 3)
        SeaPlayground.put_ship(base, 0, 0, 1)
        assert SeaPlayground.get_suitable_cells(base, 3) == [(2, 0, True), (0, 2, False)]
        assert SeaPlayground.get_suitable_cells(base, 2) == [(2, 0, True), (2, 1, True), (0, 2, False), (1, 2, False)]

        assert SeaPlayground.get_suitable_cells(base, 1) == [(2, 0, True), (2, 0, False), (2, 1, True), (2, 1, False),
                                                             (0, 2, True), (0, 2, False), (1, 2, True), (1, 2, False),
                                                             (2, 2, True), (2, 2, False)]
コード例 #2
0
    def test_is_killed_ship_injured(self):
        base = SeaField(5, 5)
        base.set(1, 2, Cell.HIT)
        base.set(1, 3, Cell.HIT)
        base.set(1, 4, Cell.SHIP)
        assert SeaPlayground._get_killed_ship(base, 1, 3) == []

        base.set(1, 2, Cell.HIT)
        base.set(1, 3, Cell.SHIP)
        base.set(1, 4, Cell.HIT)
        assert SeaPlayground._get_killed_ship(base, 1, 3) == []
コード例 #3
0
    def test_suitable_cell(self):
        base = SeaField(5, 5)
        assert base.is_cell_suitable(1, 1, 1)
        assert base.is_cell_suitable(1, 1, 3)
        assert base.is_cell_suitable(1, 1, 3, True)

        assert not base.is_cell_suitable(-1, 1, 1)
        assert not base.is_cell_suitable(5, 1, 1)
        assert not base.is_cell_suitable(1, 1, 11)

        SeaPlayground.put_ship(base, 1, 1, 3)

        assert not base.is_cell_suitable(0, 0, 1)
        assert not base.is_cell_suitable(0, 2, 2, True)
コード例 #4
0
    def test_put_ship(self):
        base = SeaField(5, 5)
        SeaPlayground.put_ship(base, 2, 1, 3, True)
        ship = [(2, 1), (2, 2), (2, 3)]
        border = [(1, 0), (1, 1), (1, 2), (1, 3), (1, 4),
                  (2, 0), (2, 4),
                  (3, 0), (3, 1), (3, 2), (3, 3), (3, 4)]

        for cell in base._cells:
            if (cell.x, cell.y) in ship:
                assert cell.value == Cell.SHIP
            elif (cell.x, cell.y) in border:
                assert cell.value == Cell.BORDER
            else:
                assert cell.value == Cell.EMPTY
コード例 #5
0
 def test_income_shoot(self):
     base = SeaField()
     SeaPlayground.put_ship(base, 2, 2, 3)
     assert SeaPlayground.income_shoot_to(base, 3, 0) == dict(signal=SIGNALS.MISS, cells=[(3, 0)])
     assert SeaPlayground.income_shoot_to(base, 3, 1) == dict(signal=SIGNALS.MISS, cells=[(3, 1)])
     assert SeaPlayground.income_shoot_to(base, 3, 2) == dict(signal=SIGNALS.HITTING, cells=[(3, 2)])
     assert SeaPlayground.income_shoot_to(base, 3, 3) == dict(signal=SIGNALS.MISS, cells=[(3, 3)])
     assert SeaPlayground.income_shoot_to(base, 3, 4) == dict(signal=SIGNALS.MISS, cells=[(3, 4)])
コード例 #6
0
 def test_incorrect_placement(self):
     base = SeaField(5, 5)
     SeaPlayground.put_ship(base, 1, 1, 3)
     with self.assertRaises(IncorrectCoordinate):
         SeaPlayground.put_ship(base, -1, 2, 2, True)
     with self.assertRaises(IncorrectShipPosition):
         SeaPlayground.put_ship(base, 0, 2, 2, True)
コード例 #7
0
 def test_answet_target(self):
     base = SeaField(4, 4)
     SeaPlayground.handle_shoot_answer(base, SIGNALS.HITTING, [(0, 1)])
     SeaPlayground.handle_shoot_answer(base, SIGNALS.MISS, [(1, 1)])
     SeaPlayground.handle_shoot_answer(base, SIGNALS.MISS, [(2, 1)])
     SeaPlayground.handle_shoot_answer(base, SIGNALS.KILLED, [(3, 1)])
     for cell in base._cells:
         if cell.x in (1, 2, 3) and cell.y in (0, 2):
             assert cell.value == Cell.BORDER
         elif (cell.x, cell.y) in ((1, 1), (2, 1)):
             assert cell.value == Cell.MISSED
         elif (cell.x, cell.y) in ((0, 1), (3, 1)):
             assert cell.value == Cell.HIT
         else:
             assert cell.value == Cell.EMPTY
コード例 #8
0
 def test_answer_target_incorrect_cell(self):
     base = SeaField(4, 4)
     with self.assertRaises(IncorrectCoordinate):
         SeaPlayground.handle_shoot_answer(base, SIGNALS.HITTING, [(-1, 0)])
     with self.assertRaises(IncorrectCoordinate):
         SeaPlayground.handle_shoot_answer(base, SIGNALS.HITTING, [(1, 11110)])
     with self.assertRaises(IncorrectCoordinate):
         SeaPlayground.handle_shoot_answer(base, SIGNALS.HITTING, [(i, i) for i in range(10)])
コード例 #9
0
 def test_answer_target_mark_border(self):
     base = SeaField(5, 5)
     base.set(2, 2, Cell.MISSED)
     SeaPlayground._shoot_answer_mark_border(base, SIGNALS.KILLED, [(0, 0)])
     SeaPlayground._shoot_answer_mark_border(base, SIGNALS.HITTING, [(3, 3)])
     SeaPlayground._shoot_answer_mark_border(base, SIGNALS.MISS, [(0, 4)])
     SeaPlayground._shoot_answer_mark_border(base, SIGNALS.MISS, [(4, 0)])
     for cell in base._cells:
         if (cell.x, cell.y) in ((0, 1), (1, 0), (1, 1),
                                 (2, 4), (4, 2), (4, 4)):
             assert cell.value == Cell.BORDER
         elif cell.x == 2 and cell.y == 2:
             assert cell.value == Cell.MISSED
         else:
             assert cell.value == Cell.EMPTY
コード例 #10
0
    def test_make_shoots(self):
        enemy_field = SeaField()
        comp = ComputerPlayer()

        SeaPlayground.put_ship(enemy_field, 1, 3, 3)
        SeaPlayground.put_ship(enemy_field, 7, 7, 1)

        comp.target_field.set(0, 3, Cell.MISSED)
        comp.target_field.set(1, 2, Cell.MISSED)
        comp.target_field.set(1, 4, Cell.MISSED)
        comp.target_field.set(1, 3, Cell.PROBABLY_SHIP)

        assert SeaPlayground.make_shoot_by_computer(comp, enemy_field) == {'signal': SIGNALS.HITTING, 'cells': [(1, 3)]}
        assert SeaPlayground.make_shoot_by_computer(comp, enemy_field) == {'signal': SIGNALS.HITTING, 'cells': [(2, 3)]}
        assert SeaPlayground.make_shoot_by_computer(comp, enemy_field) == {'signal': SIGNALS.KILLED, 'cells': [(1, 3), (2, 3), (3, 3)]}

        comp.target_field.set(7, 7, Cell.PROBABLY_SHIP)
        assert SeaPlayground.make_shoot_by_computer(comp, enemy_field) == {'signal': SIGNALS.WIN, 'cells': [(7, 7)]}
コード例 #11
0
 def test_target_anwer_mark_cell(self):
     base = SeaField(5, 5)
     SeaPlayground._shoot_answer_mark_cell(base, SIGNALS.MISS, [(1, 1)])
     SeaPlayground._shoot_answer_mark_cell(base, SIGNALS.HITTING, [(2, 2)])
     SeaPlayground._shoot_answer_mark_cell(base, SIGNALS.MISS, [(3, 3)])
     for cell in base._cells:
         if (cell.x, cell.y) in ((1, 1), (3, 3)):
             assert cell.value == Cell.MISSED
         elif cell.x == 2 and cell.y == 2:
             assert cell.value == Cell.HIT
         else:
             assert cell.value == Cell.EMPTY
コード例 #12
0
 def test_is_killed_ship_killed(self):
     base = SeaField(5, 5)
     base.set(1, 2, Cell.HIT)
     base.set(1, 3, Cell.HIT)
     base.set(1, 4, Cell.HIT)
     assert SeaPlayground._get_killed_ship(base, 1, 3) == {(1, 2), (1, 3), (1, 4)}
コード例 #13
0
 def test_put_random_ship(self):
     base = SeaField(4, 4)
     SeaPlayground._put_ship_random(base, 3)
     with self.assertRaises(NoSpaceLeft):
         SeaPlayground._put_ship_random(base, 3)
         SeaPlayground._put_ship_random(base, 3)
コード例 #14
0
 def test_find_ship_vertical(self):
     base = SeaField(5, 5)
     SeaPlayground.put_ship(base, 1, 1, 3, True)
     assert base.find_ship_by_cells(1, 2) == {(1, 1), (1, 2), (1, 3)}
コード例 #15
0
 def test_find_ship(self):
     base = SeaField(5, 5)
     SeaPlayground.put_ship(base, 1, 1, 3)
     assert base.find_ship_by_cells(2, 1) == {(1, 1), (2, 1), (3, 1)}
コード例 #16
0
 def test_incorrect_income_shoot(self):
     base = SeaField()
     with self.assertRaises(IncorrectCoordinate):
         SeaPlayground.income_shoot_to(base, -3, 0)
     with self.assertRaises(IncorrectCoordinate):
         SeaPlayground.income_shoot_to(base, 11, 0)
コード例 #17
0
 def test_put_random_many(self):
     base = SeaField()
     SeaPlayground.put_ships_random(base)
     assert len([cell for cell in base._cells if cell.value == Cell.SHIP]) == 20