예제 #1
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)])
예제 #2
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)]
예제 #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_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)]}
예제 #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_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)}
예제 #8
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)}