def IA(situation, comp, player): """ """ n = -1000 situations = Game.nextSituations({"":0},situation, comp) for sit in situations: tmp = Mini(sit[0], comp, player) if tmp >= n : n = tmp valueToPlay = sit[1] situation = Game.changeValue(situation , valueToPlay, comp) return situation
def Mini(situation,comp ,player): """ """ #mini = 1000 if Game.isFinished(situation): return Game.evalFunction(situation,comp) else: situations = Game.nextSituations({"":0},situation, player) for sit in situations: tmp = Maxi (sit[0] ,comp, player) if tmp <= mini : mini = tmp return mini
def __min_max(game_name, game, situation, player, depth): """ The minimax algorithm. :param game: The game :type game: game :param situation: The current situation :type situation: situation: :param player: The player :type player: a player :param depth: The recursivity depth for the minimax algorithm (Upper is the depth, better is the decision) * If depth = -1 , the depth will be based on the difficulty score :type depth: int :return: A tuple with two elements: the first is the best situation score, the second is the best situation :rtype: tuple<int, situation> """ if game_name == "nim": import nim_game as Game elif game_name == "othello": import othello as Game else: import tictactoe as Game if Game.isFinished(situation) or depth == 0: score = Game.evalFunction(situation, player) return score, situation else: nextSituations = Game.nextSituations(situation, player) if Game.coef(player) == 1: return max([(__min_max(game_name, game, nextSit, Game.get_inv_player(player), depth - 1)[0], nextSit) for nextSit in nextSituations]) else: return min([(__min_max(game_name, game, nextSit, Game.get_inv_player(player), depth - 1)[0], nextSit) for nextSit in nextSituations])