def handler(x, y, g, p1, p2, b, dif, eve):
    """
    handles player's clicks and manage turns of the players
    
    :param x: the x-coordinate of the move
    :type x: int
    :param y: the y-coordinate of the move
    :type y: int
    :param g: the current situation
    :type g: situation
    :param p1: the first player
    :type p1: player
    :param p2: the second player
    :type p2: player
    :param b: buttons list
    :type b: list
    :param dif: the difficulty of the game(the depth of the IA recursivity)
    :type dif: int
    :param eve: the event
    :type eve: event
    """
    global current
    if not othello.isFinished(g):
        if Player.get_name(current) != 'computer' and othello.playerCanPlay(
                g, g, current):
            g = othello.clean(g)
            if othello.isValidMove(g, current, x, y):
                valid = (othello.validMoves(g, current, x, y))
                for k in valid:
                    x1, y1 = k[0], k[1]
                    g[x1][y1] = (Player.get_spec(current))
                g[x][y] = Player.get_spec(current)
                current = switch_player(current, p1, p2)
        elif not othello.playerCanPlay(g, g, current):
            current = switch_player(current, p1, p2)
        if Player.get_name(current) == 'computer' and othello.playerCanPlay(
                g, g, current):
            g = othello.clean(g)
            g = AlphaBeta.IA(g, current, switch_player(current, p1, p2), dif,
                             othello)
            current = switch_player(current, p1, p2)
    hints(g, b)
    __redraw(g, b)
    othello.clean(g)
    if othello.isFinished(g):
        winner = checkwinner(g, g, current, p1, p2, othello)
        _disable(g, b)
        msg = finalState(winner)
        msgbox.showinfo("finished", msg)
def play(name):
    """
    """
    assert name in LIST_GAME, 'g must be in LIST_GAME'
    if name=='othello':
        import othello as Game
    elif name=='tic_tac_toe':
        import tictactoe as Game
    elif name=='nim_game':
        import nim_game as Game
    game = init_game(name)
    situation= Game.initSituation(game)
    turn_passed=0
    current_player=first_player(game)
    Game.displaySituation(situation)
    while not Game.isFinished(situation) and turn_passed<2:
        if Game.playerCanPlay(game, situation, current_player):
            turn_passed=0
            if Player.name(current_player)!='minMAX':
                situation = Game.humanPlayerPlays(game,current_player,situation)
            else:
                print("computer's playing")
                situation = minmax.main(game,current_player,situation)
            Game.displaySituation(situation)
        else:
            turn_passed +=1
        current_player = next_player(current_player,game)
    winner=Game.getWinner(game,situation,current_player)
    if winner == None:
        print("It's a draw")
    elif Player.name(winner)!="minMAX":
        print("Well done {:s} you win".format(Player.name(winner)))
    else:
        print("Sorry, you loose")
def minMax(situation,player,game,depth):
    name=game['name']
    if name == 'othello':
        import othello as Game
    elif name == 'tic_tac_toe':
        import tictactoe as Game
    elif name =='nim_game':
        import nim_game as Game
    if Player.name(player)==COMPUTER_NAME:
        coeff=1
    else:
        coeff=-1
    next_player=changePlayer(player,game)
    if Game.isFinished(situation) or depth==0:
        #décomenter pour vérifier le résultat de la fonction d'évaluation
        #print(Game.evalFunction(situation,player,game)*coeff) 
        return Game.evalFunction(situation,player,game)*coeff,situation
    else:
        next_situation=Game.nextSituations(game, situation, player)
        res=None
        if Player.name(player)==COMPUTER_NAME:
            for sit in next_situation:
                temp =minMax(sit,next_player,game,depth-1)
                if res==None or temp!=None and res[0]<temp[0]:
                   res=temp
        else:
            for sit in next_situation:
                temp =minMax(sit,next_player,game,depth-1)
                if res==None or temp!=None and res[0]>temp[0]:
                    res=temp
        try:
            return res[0],sit
        except:
            print (res)
def minMaxFinal(situation,player,game):
    name=game['name']
    if name == 'othello':
        import othello as Game
    elif name == 'tic_tac_toe':
        import tictactoe as Game
    elif name =='nim_game':
        import nim_game as Game
    next_player=changePlayer(player,game)
    if Game.isFinished(situation):
        return (Game.evalFunction(situation,player),situation)
    else:
        next_situation=Game.nextSituations(game, situation, player)
        res=None
        if Player.name(player)==COMPUTER_NAME:
            for sit in next_situation:
                temp =minMaxFinal(sit,next_player,game)
                if res==None or temp!=None and res[0]<temp[0]:
                   res=temp
        else:
            for sit in next_situation:
                temp =minMaxFinal(sit,next_player,game)
                if res==None or temp!=None and res[0]>temp[0]:
                    res=temp
        try:
            return res[0],sit
        except:
            pass