Exemple #1
0
    def test_get_ship_outrange(self):
        f = Field(3, 3)
        with self.assertRaises(CoordOutOfRange):
            ShipService.get_ship_if_killed(f, -1, 2)

        with self.assertRaises(CoordOutOfRange):
            ShipService.get_ship_if_killed(f, 0, 32)
Exemple #2
0
 def test_is_fleet_killed(self):
     f = Field(4, 4)
     self.assertTrue(ShipService.is_fleet_killed(f))
     f.get(2, 2).mark_ship()
     self.assertFalse(ShipService.is_fleet_killed(f))
     f.get(2, 2).shoot()
     self.assertTrue(ShipService.is_fleet_killed(f))
Exemple #3
0
    def test_income_shoot_outrange(self):
        f = Field(4, 4)
        with self.assertRaises(CoordOutOfRange):
            ShipService.shoot_to(f, 22, 2)

        with self.assertRaises(CoordOutOfRange):
            ShipService.shoot_to(f, 2, -2)
Exemple #4
0
 def test_pit_ships_random(self):
     f = Field(5, 5)
     ShipService.put_ships_random(f, fleet=[5, 1])
     ships = set()
     for c in f.cells:
         if c.is_ship:
             ships.add(tuple(ShipService.get_ship_by_cell(f, c.x, c.y)))
     self.assertEqual(2, len(ships))
     self.assertEqual(sum([len(i) for i in ships]), 6)
Exemple #5
0
 def test_put_ship(self):
     f = Field(3, 3)
     ShipService.put_ship(f, 0, 0, 2, True)
     for cell in f.cells:
         if (cell.x, cell.y) in ((0, 0), (0, 1)):
             self.assertTrue(cell.is_ship)
         elif (cell.x, cell.y) in ((1, 0), (1, 1), (1, 2), (0, 2)):
             self.assertTrue(cell.is_border)
         else:
             self.assertTrue(cell.is_empty)
Exemple #6
0
 def test_income_shoot_hit(self):
     f = Field(4, 4)
     cell = f.get(2, 2)
     cell.mark_ship()
     self.assertTrue(ShipService.shoot_to(f, 2, 2))
     self.assertTrue(cell.is_shooted)
     self.assertTrue(cell.is_ship)
Exemple #7
0
 def test_income_shoot_border(self):
     f = Field(4, 4)
     cell = f.get(2, 2)
     cell.mark_border()
     self.assertFalse(ShipService.shoot_to(f, 2, 2))
     self.assertTrue(cell.is_shooted)
     self.assertTrue(cell.is_border)
Exemple #8
0
    def test_get_available_vectors(self):
        f = Field(3, 3)
        f.get(0, 0).mark_ship()
        f.get(0, 1).mark_border()
        f.get(1, 0).mark_border()
        f.get(1, 1).mark_border()

        self.assertEqual(ShipService.get_available_vectors(f, 3),
                         [(2, 0, 3, True), (0, 2, 3, False)])
        self.assertEqual(ShipService.get_available_vectors(f, 2),
                         [(2, 0, 2, True), (2, 1, 2, True), (0, 2, 2, False),
                          (1, 2, 2, False)])
        self.assertEqual(ShipService.get_available_vectors(f, 1),
                         [(2, 0, 1, True), (2, 0, 1, False), (2, 1, 1, True),
                          (2, 1, 1, False), (0, 2, 1, True), (0, 2, 1, False),
                          (1, 2, 1, True), (1, 2, 1, False), (2, 2, 1, True),
                          (2, 2, 1, False)])
Exemple #9
0
 def test_get_ship_by_cell_surrounded(self):
     f = Field()
     f.get(3, 3).mark_ship()
     f.get(1, 3).mark_ship()
     f.get(3, 1).mark_ship()
     f.get(5, 3).mark_ship()
     f.get(3, 5).mark_ship()
     self.assertEqual(ShipService.get_ship_by_cell(f, 3, 3), [(3, 3)])
Exemple #10
0
 def test_get_ship_by_cell_v(self):
     f = Field(5, 5)
     f.get(2, 1).mark_ship()
     f.get(2, 2).mark_ship()
     f.get(2, 3).mark_ship()
     self.assertFalse(
         set(ShipService.get_ship_by_cell(f, 2, 2)).difference([(2, 1),
                                                                (2, 2),
                                                                (2, 3)]))
Exemple #11
0
    def test_get_ship_if_killed(self):
        f = Field(5, 5)
        f.get(0, 1).mark_ship()
        f.get(0, 0).mark_ship()

        self.assertEqual(ShipService.get_ship_if_killed(f, 0, 1), {})

        f.get(0, 1).shoot()
        self.assertEqual(ShipService.get_ship_if_killed(f, 0, 0), {})

        f.get(0, 0).shoot()
        resp = ShipService.get_ship_if_killed(f, 0, 1)

        self.assertEqual(len(resp['ship']), 2)
        self.assertEqual(len(resp['border']), 4)

        self.assertFalse(set(resp['ship']).difference([(0, 0), (0, 1)]))
        self.assertFalse(
            set(resp['border']).difference([(1, 0), (1, 1), (1, 2), (0, 2)]))
Exemple #12
0
 def test_income_shoot_missed(self):
     f = Field(4, 4)
     cell = f.get(2, 2)
     self.assertFalse(ShipService.shoot_to(f, 2, 2))
     self.assertTrue(cell.is_shooted)
     self.assertTrue(cell.is_empty)