Exemplo n.º 1
0
 def test_is_dead(self):
     grid = Grid()
     grid.add_ship(Ship((0, 0), EAST, SUBMARINE))
     self.assertFalse(grid.is_dead())
     grid.attack(0, 0)
     grid.attack(1, 0)
     grid.attack(2, 0)
     self.assertTrue(grid.is_dead())
Exemplo n.º 2
0
 def test_peg_exists(self):
     grid = Grid()
     grid.add_ship(Ship((0, 0), EAST, SUBMARINE))
     grid.attack(0, 0)
     with self.assertRaises(PegExistsError):
         grid.attack(0, 0)
     grid.attack(5, 5)
     with self.assertRaises(PegExistsError):
         grid.attack(5, 5)
Exemplo n.º 3
0
    def test_attack(self):
        grid = Grid()
        grid.add_ship(Ship((0, 0), EAST, SUBMARINE))
        grid.add_ship(Ship((9, 9), WEST, BATTLESHIP))

        self.assertEqual(grid.attack(0, 0), (SUBMARINE, False))
        self.assertEqual(grid.attack(1, 0), (SUBMARINE, False))
        self.assertEqual(grid.attack(2, 0), (SUBMARINE, True))
        self.assertEqual(grid.attack(3, 0), None)
        self.assertEqual(grid.attack(0, 1), None)
        self.assertEqual(grid.attack(9, 9), (BATTLESHIP, False))
        self.assertEqual(grid.attack(8, 9), (BATTLESHIP, False))
        self.assertEqual(grid.attack(7, 9), (BATTLESHIP, False))
        self.assertEqual(grid.attack(6, 9), (BATTLESHIP, True))
        self.assertEqual(grid.attack(5, 9), None)
        self.assertEqual(grid.attack(5, 5), None)
        self.assertEqual(grid.attack(7, 5), None)

        self.assertEqual(
            grid._pegs,
            [Peg(0, 0, True),
             Peg(1, 0, True),
             Peg(2, 0, True),
             Peg(3, 0, False),
             Peg(0, 1, False),
             Peg(9, 9, True),
             Peg(8, 9, True),
             Peg(7, 9, True),
             Peg(6, 9, True),
             Peg(5, 9, False),
             Peg(5, 5, False),
             Peg(7, 5, False)]
        )
Exemplo n.º 4
0
 def test_target_grid_str(self):
     grid = Grid()
     grid.add_ship(Ship((0, 0), EAST, SUBMARINE))
     grid.add_ship(Ship((8, 1), SOUTH, CARRIER))
     grid.attack(0, 0)
     grid.attack(5, 5)
     grid.attack(7, 2)
     grid.attack(8, 3)
     self.assertEqual(
         grid.target_grid_str(),
         '  1 2 3 4 5 6 7 8 9 10\n'
         'A x . . . . . . . . . \n'
         'B . . . . . . . . . . \n'
         'C . . . . . . . o . . \n'
         'D . . . . . . . . x . \n'
         'E . . . . . . . . . . \n'
         'F . . . . . o . . . . \n'
         'G . . . . . . . . . . \n'
         'H . . . . . . . . . . \n'
         'I . . . . . . . . . . \n'
         'J . . . . . . . . . . \n'
     )
Exemplo n.º 5
0
    def test_total_sunk(self):
        grid = Grid()
        grid.add_ship(Ship((0, 0), EAST, SUBMARINE))
        grid.add_ship(Ship((9, 9), WEST, BATTLESHIP))

        self.assertEqual(grid.total_sunk(), 0)
        grid.attack(0, 0)
        grid.attack(1, 0)
        self.assertEqual(grid.total_sunk(), 0)
        grid.attack(2, 0)
        self.assertEqual(grid.total_sunk(), 1)
        grid.attack(9, 9)
        grid.attack(8, 9)
        self.assertEqual(grid.total_sunk(), 1)
        grid.attack(7, 9)
        grid.attack(6, 9)
        self.assertEqual(grid.total_sunk(), 2)