def sp(choix_ai_adversaire, num_tirages_MC = 3, num_descentes_dans_arbre = 7, facteur_uct = 0.0): '''Une partie opposant un joueur humain à une intelligence artificielle''' grid = Grille() ai = AI('O') if choix_ai_adversaire == 2: ai = MC('O', num_tirages_MC) elif choix_ai_adversaire == 3: ai = UCT('O', num_tirages_MC, num_descentes_dans_arbre, facteur_uct) le_joueur1_gagne = False mes_coups_possibles = grid.lookForAllowedSteps() input = [] player = 'X' while(input != 'q' and grid.checkVictory() is False and len(mes_coups_possibles)>0): grid.showGrid() input = raw_input("Number 1 through 7 = drop disc, q = quit. \nYour move:") if (input in ['1','2','3','4','5','6','7']): MonCoup = int(input) if grid.drop(player, MonCoup): le_joueur1_gagne = True mes_coups_possibles = grid.lookForAllowedSteps() if(grid.checkVictory() is False and len(mes_coups_possibles)>0): VotreCoup = ai.ai(grid) grid.drop(ai.player, VotreCoup) le_joueur1_gagne = False mes_coups_possibles = grid.lookForAllowedSteps() il_y_a_un_vainqueur = grid.checkVictory() print "The game ended in the following state:" grid.showGrid() print("Y a-t-il un gagnant ?"), print(il_y_a_un_vainqueur) print("Si oui, est-ce le joueur n 1 (X) ?"), print(le_joueur1_gagne)
def np(choix_ai_joueur, choix_ai_adversaire, num_tirages_MC = 3, num_descentes_dans_arbre = 7, facteur_uct = 0.0, show_grid = False): '''Une partie entre intelligences artificielles''' grid = Grille() ai1 = AI('X') if choix_ai_joueur == 2: ai1 = MC('X', num_tirages_MC) elif choix_ai_joueur == 3: ai1 = UCT('X', num_tirages_MC, num_descentes_dans_arbre, facteur_uct) ai2 = AI('O') if choix_ai_adversaire == 2: ai2 = MC('O') # paramètres par défaut elif choix_ai_adversaire == 3: ai2 = UCT('O') # paramètres par défaut le_joueur1_gagne = False mes_coups_possibles = grid.lookForAllowedSteps() while(grid.checkVictory() is False and len(mes_coups_possibles)>0): MonCoup = ai1.ai(grid) grid.drop(ai1.player, MonCoup) le_joueur1_gagne = True mes_coups_possibles = grid.lookForAllowedSteps() if show_grid: grid.showGrid() sleep(1) if(grid.checkVictory() is False and len(mes_coups_possibles)>0): VotreCoup = ai2.ai(grid) grid.drop(ai2.player, VotreCoup) le_joueur1_gagne = False mes_coups_possibles = grid.lookForAllowedSteps() if show_grid: grid.showGrid() il_y_a_un_vainqueur = grid.checkVictory() print "The game ended in the following state:" grid.showGrid() print("Y a-t-il un gagnant ?"), print(il_y_a_un_vainqueur) print("Si oui, est-ce le joueur 1 (X) ?"), print(le_joueur1_gagne) return (il_y_a_un_vainqueur, le_joueur1_gagne)
def TreeDown(self, position_courante, symbole_dont_c_est_le_tour): '''Descendre dans l'arbre UCT''' # soit N la racine de l'arbre N = self.tree joueur = symbole_dont_c_est_le_tour # boucle : fils non explorés de N grille = Grille() grille.set(N.name) mes_coups_possibles = grille.lookForAllowedSteps() while all([self.compteur_choix_action_dans_etat.has_key((N.name, i)) for i in mes_coups_possibles]): # soit F le fils de N ayant la plus grande valeur UCT if(len(mes_coups_possibles)>0): action = self.choisirActionUCT(grille) nouvel_etat = changerEtatApresTransition(N.name, action, joueur) F = Node(nouvel_etat, N) F.code = action N = F joueur = getOtherSymbol(joueur) grille.set(N.name) mes_coups_possibles = grille.lookForAllowedSteps() else: break # soir F un fils de N tiré au hasard parmi les fils non explorés grille.set(N.name) mes_coups_possibles = grille.lookForAllowedSteps() if(len(mes_coups_possibles)>0): actions_inexplorees = [i for i in mes_coups_possibles if not self.compteur_choix_action_dans_etat.has_key((N.name, i))] tirage = randint(0, len(actions_inexplorees)-1) action = actions_inexplorees[tirage] etat_inexplore = changerEtatApresTransition(N.name, action, joueur) F = Node(etat_inexplore, N) F.code = action joueur = getOtherSymbol(joueur) else: F = N return (F, joueur)