Example #1
0
 def test_can_place_ship_on(self):
     field = Field()
     field.reset()
     self.assertFalse(field.can_place_ship_on(None, (0, 0)))
     field.try_place_new_ship((0, 0))
     self.assertFalse(field.can_place_ship_on(Ship(1), (-1, 0)))
     self.assertFalse(field.can_place_ship_on(Ship(1), (0, 0)))
     self.assertFalse(field.can_place_ship_on(Ship(1), (1, 1)))
     self.assertTrue(field.can_place_ship_on(Ship(1), (1, 2)))
Example #2
0
    def test_shot(self):
        field = Field()
        field.reset()
        self.assertTrue(field.shot((0, 0))[0])
        self.assertFalse(field.shot((0, 0))[1])
        self.assertFalse(field.shot((0, 0))[0])
        self.assertFalse(field.shot((0, 0))[1])

        field.try_place_new_ship((4, 4))
        shot_result = field.shot((4, 4))
        self.assertTrue(shot_result[0])
        self.assertTrue(shot_result[1])
Example #3
0
    def test_try_remove_ship(self):
        field = Field()
        field.reset()

        self.assertFalse(field.try_remove_ship((0, 0)))
        field.try_place_new_ship((0, 0))
        self.assertTrue(field.try_remove_ship((0, 0)))
Example #4
0
    def test_get_cell_type(self):
        field = Field()
        point1 = (0, 0)

        point2 = (2, 0)
        ship = Ship(1)
        ship.placed_location = point2
        field.ships.append(ship)
        ship.on_shot(point2)

        point3 = (4, 0)
        ship = Ship(1)
        ship.placed_location = point3
        field.ships.append(ship)

        point4 = (6, 0)
        ship = Ship(2)
        ship.rotate()
        ship.placed_location = point4
        field.ships.append(ship)
        ship.on_shot(point4)

        point5 = (8, 0)
        field.shot(point5)

        self.assertEqual(field.get_cell_type(point1, True), Cell.Empty)
        self.assertEqual(field.get_cell_type(point2, True), Cell.FullDeadShip)
        self.assertEqual(field.get_cell_type(point3, True), Cell.ShipPeace)
        self.assertEqual(field.get_cell_type(point4, True), Cell.DeadShipPeace)
        self.assertEqual(field.get_cell_type(point5, True), Cell.Shot)
        self.assertEqual(field.get_cell_type(point5, False), Cell.Empty)
        self.assertEqual(field.get_cell_type(point2, False), Cell.ShipPeace)
Example #5
0
    def test_get_ship_on(self):
        field = Field()
        field.reset()
        field.try_place_new_ship((0, 0))

        self.assertFalse(field.get_ship_on((0, 0)) is None)
        self.assertTrue(field.get_ship_on((2, 3)) is None)
Example #6
0
    def test_extract_to_render(self):
        game = Game()
        game.bot_field = Field(Field.get_arrange_ships())
        game.player_field = Field(Field.get_arrange_ships())
        buffer = BufferToGameLogic()
        buffer_to_render = BufferToRender()
        game.extract_to_render(BufferToGameLogic(), buffer_to_render)

        self.assertEqual(len(buffer_to_render.player_cells), 100)
        self.assertEqual(len(buffer_to_render.bot_cells), 100)
        self.assertEqual(len(buffer_to_render.pre_show_cell), 0)

        game = Game()
        buffer.pre_show_cell_index = (1, 1)
        buffer_to_render = BufferToRender()
        game.extract_to_render(buffer, buffer_to_render)
        self.assertEqual(len(buffer_to_render.pre_show_cell), 1)
Example #7
0
    def test_get_next(self):
        field = Field()
        field.reset()

        self.assertFalse(field.get_next_ship_to_place() is None)
        field.ships_to_place = []
        self.assertTrue(field.get_next_ship_to_place() is None)
Example #8
0
 def test_generate_random_shot(self):
     for _ in range(100):
         field = Field()
         field.make_random_shot()
         self.assertEqual(len(field.shots), 1)
         field.make_random_shot()
         self.assertEqual(len(field.shots), 2)
Example #9
0
    def test_is_game_completed(self):
        game = Game()

        game_result = game.is_game_completed()
        game.bot_field = Field()
        self.assertFalse(game_result[0])
        self.assertFalse(game_result[1])
        game.bot_field = Field(Field.get_arrange_ships())
        game.player_field = Field(Field.get_arrange_ships())

        game_result = game.is_game_completed()
        self.assertFalse(game_result[0])
        self.assertTrue(game_result[1])

        for _ in range(100):
            game.bot_field = Field(Field.get_arrange_ships())

        game_result = game.is_game_completed()
        self.assertTrue(game_result[0])
        self.assertTrue(game_result[1])

        game.bot_field = Field(Field.get_arrange_ships())
        for _ in range(100):
            game.player_field = Field(Field.get_arrange_ships())

        game_result = game.is_game_completed()
        self.assertTrue(game_result[0])
        self.assertFalse(game_result[1])
Example #10
0
 def test_has_all_ships_die(self):
     field = Field()
     field.ships.append(Ship(1))
     self.assertFalse(field.has_all_ships_die())
     field.ships[0].on_shot((0, 0))
     self.assertTrue(field.has_all_ships_die())
Example #11
0
 def test_reset(self):
     field = Field()
     field.reset()
     self.assertEqual(len(field.ships), 0)
     self.assertEqual(len(field.shots), 0)
     self.assertEqual(len(field.ships_to_place), 10)
Example #12
0
 def test_arrange_ships_automatically(self):
     for _ in range(100):
         self.assertEqual(len(Field.get_arrange_ships()), 10)