Пример #1
0
 def __init__(self):
     self.map_sea = MapSea(10)
     self.map_sea2 = MapSea(10)
     self.nb_ship_rest = 5
     self.nb_ship_rest2 = 5
     self.list_ship = [Ship(i) for i in range(5, 1, -1)]
     self.list_ship.insert(2, Ship(3))
     self.list_ship2 = [Ship(i) for i in range(5, 1, -1)]
     self.list_ship2.insert(2, Ship(3))
     self.player1 = None
     self.player2 = None
Пример #2
0
 def setUp(self):
     self.map_sea_test = MapSea(5)
     self.cell_test = Cell()
Пример #3
0
class test_map_sea(unittest.TestCase):

    def setUp(self):
        self.map_sea_test = MapSea(5)
        self.cell_test = Cell()

    def test_map_init(self):
        self.assertEqual(self.map_sea_test.size,
                         len(self.map_sea_test.map_cell))
        self.assertEqual(self.map_sea_test.size,
                         len(self.map_sea_test.map_cell[1]))
        for ligne in self.map_sea_test.map_cell:
            for member in ligne:
                self.assertIsInstance(member, Cell)

    def test_place_ship(self):
        ship1 = Ship(5)
        self.map_sea_test.place_ship(ship1, 0, 0, dir.HORI)
        for i in range(5):
            self.assertTrue(self.map_sea_test.map_cell[0][i].is_ship())
            self.assertTrue(self.map_sea_test.map_cell[0][i].ship_cell.lenght)
        ship2 = Ship(2)
        self.map_sea_test.place_ship(ship2, 1, 1, dir.VERTI)
        for i in range(2):
            self.assertTrue(self.map_sea_test.map_cell[i + 1][1].is_ship())
            self.assertTrue(self.map_sea_test.map_cell[i + 1][1].ship_cell.lenght)
        nb_cell_with_ship = 0
        for ligne in self.map_sea_test.map_cell:
            for cell_m in ligne:
                if cell_m.is_ship():
                    nb_cell_with_ship += 1
        self.assertEqual(7, nb_cell_with_ship)

    def test_peut_placer(self):
        ship1 = Ship(5)
        # test quand il y a rien sur la map
        for i in range(self.map_sea_test.size):
            self.assertTrue(self.map_sea_test.peut_placer(ship1, i, 0,
                                                          dir.HORI))

        for i in range(self.map_sea_test.size):
            self.assertTrue(self.map_sea_test.peut_placer(ship1, 0, i,
                                                          dir.VERTI))

        for i in range(self.map_sea_test.size):
            for j in range(1, self.map_sea_test.size):
                self.assertFalse(self.map_sea_test.peut_placer(ship1, i, j,
                                                               dir.HORI))

        for i in range(1, self.map_sea_test.size):
            for j in range(self.map_sea_test.size):
                self.assertFalse(self.map_sea_test.peut_placer(ship1, i, j,
                                                               dir.VERTI))

        self.map_sea_test.place_ship(ship1, 2, 0, dir.HORI)
        ship2 = Ship(3)
        # test quand il y a un bateau sur la map
        for i in range(self.map_sea_test.size):
            for j in range(self.map_sea_test.size):
                self.assertFalse(self.map_sea_test.peut_placer(ship2, i, j,
                                                               dir.VERTI))

        for i in range(self.map_sea_test.size):
            self.assertFalse(self.map_sea_test.peut_placer(ship2, 0, i,
                                                           dir.VERTI))

        for i in range(3, self.map_sea_test.size):
            self.assertTrue(self.map_sea_test.peut_placer(ship2, i, 0,
                                                          dir.HORI))

    def test_see_cell(self):
        ship2 = Ship(2)
        self.map_sea_test.place_ship(ship2, 1, 0, dir.HORI)
        self.assertEqual(action.NOTHING, self.map_sea_test.see_cell(0, 0))
        self.assertEqual(action.TOUCHE, self.map_sea_test.see_cell(1, 0))
        self.assertEqual(action.COULER, self.map_sea_test.see_cell(1, 1))

    def test_reset_map(self):
        ship1 = Ship(5)
        ship2 = Ship(2)
        self.map_sea_test.place_ship(ship1, 0, 0, dir.HORI)
        self.map_sea_test.place_ship(ship2, 1, 0, dir.VERTI)
        nb_cell_with_ship = 0
        for ligne in self.map_sea_test.map_cell:
            for cell_m in ligne:
                if cell_m.is_ship():
                    nb_cell_with_ship += 1
        self.assertEqual(7, nb_cell_with_ship)
        for ligne in self.map_sea_test.map_cell:
            ligne[0].change_view()
        self.map_sea_test.reset_map()
        nb_cell_with_ship = 0
        for ligne in self.map_sea_test.map_cell:
            for cell_m in ligne:
                if cell_m.is_ship():
                    nb_cell_with_ship += 1
        self.assertEqual(0, nb_cell_with_ship)
        for ligne in self.map_sea_test.map_cell:
            self.assertTrue(ligne[0].is_hide())
Пример #4
0
 def setUp(self):
     self.test_map = MapSea(10)
     self.ia_test = IaHunterUltime(10, self.test_map)
Пример #5
0
class test_IaHunter(unittest.TestCase):
    def setUp(self):
        self.test_map = MapSea(10)
        self.ia_test = IaHunter(10, self.test_map)

    def test_init_ia_hunter(self):
        self.assertIsInstance(self.ia_test.pile_coup, Pile)
        self.assertListEqual(self.ia_test.coup_jouer, [])
        self.assertFalse(self.ia_test.track)
        self.assertEqual(self.ia_test.size_map, self.test_map.size)
        self.assertEqual(100, len(self.ia_test.coup_possible))

    def test_croix_hunt(self):
        self.assertListEqual([(0, 1), (2, 1), (1, 2), (1, 0)],
                             self.ia_test.croix_hunt(1, 1))
        self.assertListEqual([(1, 0), (0, 1)], self.ia_test.croix_hunt(0, 0))
        self.assertListEqual([(1, 1), (0, 2), (0, 0)],
                             self.ia_test.croix_hunt(0, 1))
        self.assertListEqual([(1, 9), (0, 8)], self.ia_test.croix_hunt(0, 9))
        self.assertListEqual([(0, 9), (2, 9), (1, 8)],
                             self.ia_test.croix_hunt(1, 9))
        self.assertListEqual([(8, 9), (9, 8)], self.ia_test.croix_hunt(9, 9))
        self.assertListEqual([(8, 1), (9, 2), (9, 0)],
                             self.ia_test.croix_hunt(9, 1))
        self.assertListEqual([(8, 0), (9, 1)], self.ia_test.croix_hunt(9, 0))
        self.assertListEqual([(0, 0), (2, 0), (1, 1)],
                             self.ia_test.croix_hunt(1, 0))
        self.test_map.see_cell(0, 1)
        self.test_map.see_cell(1, 1)
        self.test_map.see_cell(8, 1)
        self.test_map.see_cell(2, 9)
        self.assertListEqual([(2, 1), (1, 2), (1, 0)],
                             self.ia_test.croix_hunt(1, 1))
        self.assertListEqual([(1, 0)], self.ia_test.croix_hunt(0, 0))
        self.assertListEqual([(0, 2), (0, 0)], self.ia_test.croix_hunt(0, 1))
        self.assertListEqual([(1, 9), (0, 8)], self.ia_test.croix_hunt(0, 9))
        self.assertListEqual([(0, 9), (1, 8)], self.ia_test.croix_hunt(1, 9))
        self.assertListEqual([(8, 9), (9, 8)], self.ia_test.croix_hunt(9, 9))
        self.assertListEqual([(9, 2), (9, 0)], self.ia_test.croix_hunt(9, 1))
        self.assertListEqual([(8, 0), (9, 1)], self.ia_test.croix_hunt(9, 0))
        self.assertListEqual([(0, 0), (2, 0)], self.ia_test.croix_hunt(1, 0))
        # test avec des case entre les adjacant
        ship3 = Ship(3)
        self.test_map.place_ship(ship3, 5, 5, dir.HORI)
        self.test_map.see_cell(5, 6)
        self.assertListEqual([(4, 5), (6, 5), (5, 7), (5, 4)],
                             self.ia_test.croix_hunt(5, 5))
        self.assertListEqual([(4, 7), (6, 7), (5, 8), (5, 5)],
                             self.ia_test.croix_hunt(5, 7))
        self.assertListEqual([(3, 6), (6, 6), (4, 7), (4, 5)],
                             self.ia_test.croix_hunt(4, 6))
        self.assertListEqual([(4, 6), (7, 6), (6, 7), (6, 5)],
                             self.ia_test.croix_hunt(6, 6))

    def test_next_coup(self):
        self.ia_test.origine_point = (2, 2)
        self.assertTupleEqual((0, 2), self.ia_test.next_coup(1, 2))
        self.assertTupleEqual((4, 2), self.ia_test.next_coup(3, 2))
        self.assertTupleEqual((2, 4), self.ia_test.next_coup(2, 3))
        self.assertTupleEqual((2, 0), self.ia_test.next_coup(2, 1))
        self.ia_test.origine_point = (1, 1)
        self.assertIs(None, self.ia_test.next_coup(0, 1))
        self.assertTupleEqual((3, 1), self.ia_test.next_coup(2, 1))
        self.assertIs(None, self.ia_test.next_coup(1, 0))
        self.assertTupleEqual((1, 3), self.ia_test.next_coup(1, 2))
        self.ia_test.origine_point = (8, 8)
        self.assertTupleEqual((6, 8), self.ia_test.next_coup(7, 8))
        self.assertIs(None, self.ia_test.next_coup(9, 8))
        self.assertTupleEqual((8, 6), self.ia_test.next_coup(8, 7))
        self.assertIs(None, self.ia_test.next_coup(8, 9))
        self.ia_test.origine_point = (2, 2)
        self.ia_test.grille.see_cell(0, 2)
        self.ia_test.grille.see_cell(4, 2)
        self.ia_test.grille.see_cell(2, 0)
        self.ia_test.grille.see_cell(2, 4)
        self.assertIs(None, self.ia_test.next_coup(1, 2))
        self.assertIs(None, self.ia_test.next_coup(3, 2))
        self.assertIs(None, self.ia_test.next_coup(2, 1))
        self.assertIs(None, self.ia_test.next_coup(2, 3))

    def test_choice_coup(self):
        random.seed(a=0)
        self.ia_test.choice_coup()
        self.assertEqual(99, len(self.ia_test.coup_possible))
        self.assertEqual(1, len(self.ia_test.coup_jouer))
        self.ia_test.track = True
        self.ia_test.origine_point = (5, 5)
        self.ia_test.pile_coup.stack += self.ia_test.croix_hunt(5, 5)
        coup = self.ia_test.choice_coup()
        self.assertEqual(3, len(self.ia_test.pile_coup.stack))
        self.assertEqual(98, len(self.ia_test.coup_possible))
        self.assertNotIn(coup, self.ia_test.coup_possible)
        self.assertIn(coup, self.ia_test.coup_jouer)

    def test_play_one_tour_iaHunter(self):
        random.seed(a=0)
        self.ia_test.reset_coup_possible()
        ship1 = Ship(2)
        self.test_map.place_ship(ship1, 8, 0, dir.HORI)
        ship2 = Ship(3)
        self.test_map.place_ship(ship2, 1, 0, dir.HORI)
        ship3 = Ship(3)
        self.test_map.place_ship(ship3, 7, 4, dir.VERTI)
        ship4 = Ship(3)
        self.test_map.place_ship(ship4, 6, 5, dir.VERTI)
        ship5 = Ship(4)
        self.test_map.place_ship(ship5, 1, 9, dir.VERTI)
        self.assertEqual(action.NOTHING, self.ia_test.play_one_tour())
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.COULER, self.ia_test.play_one_tour())
        # premier bateau
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.COULER, self.ia_test.play_one_tour())
        # test bateau juste poser l'un contre l'autre
        self.assertEqual(action.NOTHING, self.ia_test.play_one_tour())
        # premier touche
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.NOTHING, self.ia_test.play_one_tour())
        # correspond au 11eme coup
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.NOTHING, self.ia_test.play_one_tour())
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.COULER, self.ia_test.play_one_tour())
        self.assertEqual(action.NOTHING, self.ia_test.play_one_tour())
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.COULER, self.ia_test.play_one_tour())

    def test_play_one_tour_iaHunter_hard(self):
        random.seed(a=0)
        self.ia_test.reset_coup_possible()
        ship1 = Ship(2)
        self.test_map.place_ship(ship1, 8, 0, dir.HORI)
        ship2 = Ship(3)
        self.test_map.place_ship(ship2, 1, 0, dir.HORI)
        ship3 = Ship(3)
        self.test_map.place_ship(ship3, 7, 4, dir.VERTI)
        ship4 = Ship(3)
        self.test_map.place_ship(ship4, 6, 5, dir.VERTI)
        ship5 = Ship(4)
        self.test_map.place_ship(ship5, 1, 9, dir.VERTI)
        self.assertEqual(action.NOTHING, self.ia_test.play_one_tour())
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.COULER, self.ia_test.play_one_tour())
        # premier bateau
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.COULER, self.ia_test.play_one_tour())
        # test bateau juste poser l'un contre l'autre
        self.assertEqual(action.NOTHING, self.ia_test.play_one_tour())
        # premier touche
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.NOTHING, self.ia_test.play_one_tour())
        # correspond au 11eme coup
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.NOTHING, self.ia_test.play_one_tour())
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.COULER, self.ia_test.play_one_tour())
        self.assertEqual(action.NOTHING, self.ia_test.play_one_tour())
        self.assertEqual(action.TOUCHE, self.ia_test.play_one_tour())
        self.assertEqual(action.COULER, self.ia_test.play_one_tour())
Пример #6
0
 def setUp(self):
     self.test_map = MapSea(5)
     self.ia_test = IaDumb(5, self.test_map)
Пример #7
0
class BattleShip:

    def __init__(self):
        self.map_sea = MapSea(10)
        self.map_sea2 = MapSea(10)
        self.nb_ship_rest = 5
        self.nb_ship_rest2 = 5
        self.list_ship = [Ship(i) for i in range(5, 1, -1)]
        self.list_ship.insert(2, Ship(3))
        self.list_ship2 = [Ship(i) for i in range(5, 1, -1)]
        self.list_ship2.insert(2, Ship(3))
        self.player1 = None
        self.player2 = None

    def party_init(self):
        for sh in self.list_ship:
            sh.reset_ship()
        self.map_sea.reset_map()
        self.nb_ship_rest = 5
        self.generate_grille(self.list_ship, self.map_sea)

    def party_init2(self):
        for sh in self.list_ship2:
            sh.reset_ship()
        self.map_sea2.reset_map()
        self.nb_ship_rest2 = 5
        self.generate_grille(self.list_ship2, self.map_sea2)

    def is_finish(self):
        return self.nb_ship_rest == 0 or self.nb_ship_rest2 == 0

    def generate_grille(self, liste_ship, sea):
        for sh in liste_ship:
            x = random.randint(0, 9)
            y = random.randint(0, 9)
            direct = dir.VERTI if 1 == random.randint(1, 2) else dir.HORI
            while(not sea.peut_placer(sh, x, y, direct)):
                x = random.randint(0, 9)
                y = random.randint(0, 9)
                direct = dir.VERTI if 1 == random.randint(1, 2) else dir.HORI
            sea.place_ship(sh, x, y, direct)
        return

    def find_player(self, joueur, grille):
        if joueur is player.USER:
            return User(grille)
        elif joueur is player.DUMB:
            return IaDumb(10, grille)
        elif joueur is player.HUNTER:
            return IaHunter(10, grille)
        else:
            return IaHunterUltime(10, grille)

    def play_nb_game(self, joueur, nb_game):
        """
        docstring
        """
        self.player1 = self.find_player(joueur, self.map_sea)
        self.party_init()
        nb_coup = 0
        for _ in range(nb_game):
            while(not self.is_finish()):
                if self.player1.play_one_tour() == action.COULER:
                    self.nb_ship_rest -= 1
                nb_coup += 1
            self.party_init()
            self.player1.grille = self.map_sea
            self.player1.reset_coup_possible()
        return nb_coup

    def play_one_game(self, joueur, verbose=False, graph=False):
        self.player1 = self.find_player(joueur, self.map_sea)
        self.party_init()
        while(not self.is_finish()):
            if graph:
                clear()
                print_map_cache(self.map_sea)
                print('\n')
            if verbose:
                time.sleep(1)
            if self.player1.play_one_tour() == action.COULER:
                self.nb_ship_rest -= 1
        return (joueur, len(self.player1.coup_jouer))

    def play_one_versus(self, joueur1, joueur2, verbose=False, graph=False):
        self.player1 = self.find_player(joueur1, self.map_sea)
        self.player2 = self.find_player(joueur2, self.map_sea2)
        self.party_init()
        self.party_init2()
        while(not self.is_finish()):
            if graph:
                clear()
                print("tableau de l'adversaire")
                print_map_cache(self.map_sea)
                print("votre tableau")
                print_map_not_cache(self.map_sea2)
            if self.player1.play_one_tour() == action.COULER:
                self.nb_ship_rest -= 1
            if self.player2.play_one_tour() == action.COULER:
                self.nb_ship_rest2 -= 1
        if self.nb_ship_rest == 0:
            us = joueur1
            nb_coup = len(self.player1.coup_jouer)
        else:
            us = joueur2
            nb_coup = len(self.player2.coup_jouer)
        if verbose:
            print(f"le joueur {us} a gagné avec {nb_coup} coup")
        self.nb_ship_rest2 = 5
        return (us, nb_coup)

    def play_user(self, mode):
        if mode == 'alone':
            back = self.play_one_game(player.USER, graph=True)
            print(f"vous avez trouvé en {back[1]}")
            return
        elif mode == 'facile':
            back = self.play_one_versus(player.USER, player.DUMB, graph=True)
            if back[0] == player.DUMB:
                print(f"Vous avez perdu contre l'IA facile après {back[1]}"
                      " coups")
            else:
                print(f"Vous avez perdu contre l'IA facile après {back[1]}"
                      " coups")
                return
        elif mode == 'moyen':
            back = self.play_one_versus(player.USER, player.HUNTER, graph=True)
            if back[0] == player.HUNTER:
                print(f"Vous avez perdu contre l'IA moyen après {back[1]}"
                      " coups")
            else:
                print(f"Vous avez perdu contre l'IA moyen après {back[1]}"
                      " coups")
                return
        elif mode == 'difficile':
            back = self.play_one_versus(player.USER, player.ULTIMATE,
                                        graph=True)
            if back[0] == player.ULTIMATE:
                print(f"Vous avez perdu contre l'IA difficile après {back[1]}"
                      " coups")
            else:
                print(f"Vous avez perdu contre l'IA difficile après {back[1]}"
                      " coups")
                return
        else:
            print(f"le mode ({mode} n\'est pas correct, veillez choisi parmi"
                  " ces propositions:\n -alone\n-facile\n-moyen\n-difficile\n)")
            return None

    def play_versus_nb_party(self, joueur1, joueur2, nb_party):
        self.player1 = self.find_player(joueur1, self.map_sea)
        self.player2 = self.find_player(joueur2, self.map_sea2)
        self.party_init()
        self.party_init2()
        liste_victoire = []
        for _ in range(nb_party):
            while(not self.is_finish()):
                if self.player1.play_one_tour() == action.COULER:
                    self.nb_ship_rest -= 1
                if self.player2.play_one_tour() == action.COULER:
                    self.nb_ship_rest2 -= 1
            if self.nb_ship_rest == 0:
                liste_victoire.append(1)
            else:
                liste_victoire.append(0)
            self.party_init()
            self.party_init2()
            self.player1.grille = self.map_sea
            self.player2.grille = self.map_sea2
            self.player1.reset_coup_possible()
            self.player2.reset_coup_possible()
        return liste_victoire