Beispiel #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())
Beispiel #2
0
    def test_attack_player(self, AI):
        from colorama import Fore
        from battleship import Grid

        AI_attr = {'grid': Grid()}
        AI.configure_mock(**AI_attr)
        AI.grid.grid['G2'], AI.grid.grid['G3'] = (Fore.WHITE + 'P|', ) * 2
        self.player.enemy = AI.grid
        self.player.attack()
Beispiel #3
0
 def test_add_ships(self):
     grid = Grid()
     ships = [
         Ship((0, 0), EAST, SUBMARINE),
         Ship((5, 5), WEST, SUBMARINE),
         Ship((9, 9), WEST, SUBMARINE)
     ]
     grid.add_ship(ships[0])
     self.assertEqual(grid._ships, [ships[0]])
     grid.add_ship(ships[1])
     self.assertEqual(grid._ships, [ships[0], ships[1]])
     grid.add_ship(ships[2])
     self.assertEqual(grid._ships, [ships[0], ships[1], ships[2]])
Beispiel #4
0
 def setUpClass(cls):
     from battleship import Grid
     from colorama import Fore
     cls.grid = Grid()
Beispiel #5
0
    def test_no_ships_overlap(self):
        grid = Grid()
        grid.add_ship(Ship((2, 5), EAST, SUBMARINE))

        with self.assertRaises(ShipsOverlapError):
            grid.add_ship(Ship((0, 5), EAST, SUBMARINE))

        with self.assertRaises(ShipsOverlapError):
            grid.add_ship(Ship((4, 5), WEST, SUBMARINE))

        with self.assertRaises(ShipsOverlapError):
            grid.add_ship(Ship((2, 7), NORTH, SUBMARINE))

        with self.assertRaises(ShipsOverlapError):
            grid.add_ship(Ship((2, 3), SOUTH, SUBMARINE))
Beispiel #6
0
 def test_create_grid(self):
     grid = Grid()
     self.assertEqual(grid._ships, [])
Beispiel #7
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'
     )
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
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)]
        )