示例#1
0
def play(game_name, difficulty):
    """
    Main game function process

    :param game_name: the game's name
    :type game_name: str
    :return: None
    """
    global currentPlayer
    global mod

    if game_name == "nim":
        import nim_game as Game
        mod = __import__("nim_game")

    elif game_name == "othello":
        import othello as Game
        mod = __import__("othello")

    else:
        import tictactoe as Game
        mod = __import__("tictactoe")
    ask_players_names(Game.color)
    situation = Game.initSituation(game_name)

    while not Game.isFinished(situation):

        if currentPlayer is None:
            currentPlayer = Game.get_player1(Game.game)
        else:
            currentPlayer = swap_player_turn(currentPlayer, mod)

        if not Game.playerCanPlay(Game.game, situation, currentPlayer):
            print(currentPlayer, " can't play")
            currentPlayer = swap_player_turn(currentPlayer, Game)
        Game.displaySituation(situation)
        print(Player.get_name(currentPlayer), " turn")

        if Player.get_name(currentPlayer) == "Minmax":
            situation = IA.min_max(game_name, Game.game, situation, currentPlayer, difficulty)
        else:
            situation = Game.humanPlayerPlays(Game.game, currentPlayer, situation)
        Game.game['nb_plays'] += 1
    winner = Game.getWinner(Game.game, situation, currentPlayer)

    if winner is None:
        Game.displaySituation(situation)
        print("equality !")

    else:
        Game.displaySituation(situation)
        print(Player.get_name(winner), "won")
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
示例#3
0
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])