from heuristiquesOthello import * ################## le nécessaire au décompte des positions calculées ################# nb_situations_examinees = 0 def compter_situations(init): global nb_situations_examinees if init: nb_situations_examinees = 1 else: nb_situations_examinees += 1 ################## Petite fonction utilitaire ################# def tour_joueur_courant(prof_courante): return prof_courante % 2 == 1 ############################ algorithme alpha-beta ########################### def alpha_beta(othello, joueur, prof_courante, prof_max, heuristique, alpha, beta): compter_situations(prof_courante == 1) if prof_courante > prof_max: retour = ([], heuristique(othello, joueur)) else: possibilites = othello.liste_coups() if len(possibilites) == 0: #Si aucun coup n'est jouable, on change de joueur et on continue retour = evaluer_passer_son_tour_alpha_beta(othello, joueur, prof_courante, prof_max, heuristique, alpha, beta) else: #On s'intéresse maintenant aux cas où au moins 1 coup est jouable if tour_joueur_courant(prof_courante): retour = retenir_meilleur_coup_alpha_beta(othello, possibilites, joueur, prof_courante, prof_max, heuristique, alpha, beta) else: retour = retenir_pire_coup_alpha_beta(othello, possibilites, joueur, prof_courante, prof_max, heuristique, alpha, beta) if prof_courante == 1: print("nombre de situations examinées :",nb_situations_examinees) return retour def evaluer_passer_son_tour_alpha_beta(othello, joueur, prof_courante, prof_max, heuristique, alpha, beta): situation = Othello(othello) situation.changer_joueur() enchainement, recompense = alpha_beta(situation, joueur, prof_courante+1, prof_max, heuristique, alpha, beta) retour = (([None] + enchainement), recompense) return retour def retenir_meilleur_coup_alpha_beta(othello, possibilites, joueur, prof_courante, prof_max, heuristique, alpha, beta): coup_retenu = None recompense_retenue = None enchainement_retenu = None for coup in possibilites: nouvelle_situation = othello.tenter_coup(coup) liste_coups, recompense = alpha_beta(nouvelle_situation, joueur, prof_courante+1, prof_max, heuristique, alpha, beta) if recompense_retenue is None or recompense > recompense_retenue: recompense_retenue = recompense enchainement_retenu = liste_coups coup_retenu = coup if recompense_retenue >= beta: break if recompense > alpha: alpha = recompense return [coup_retenu] + enchainement_retenu, recompense_retenue def retenir_pire_coup_alpha_beta(othello, possibilites, joueur, prof_courante, prof_max, heuristique, alpha, beta): coup_retenu = None recompense_retenue = None enchainement_retenu = None for coup in possibilites: nouvelle_situation = othello.tenter_coup(coup) liste_coups, recompense = alpha_beta(nouvelle_situation, joueur, prof_courante+1, prof_max, heuristique, alpha, beta) if recompense_retenue is None or recompense < recompense_retenue: recompense_retenue = recompense enchainement_retenu = liste_coups coup_retenu = coup if recompense_retenue <= alpha: break if recompense < beta: beta = recompense return [coup_retenu] + enchainement_retenu, recompense_retenue