[ Foro de Python ]

Algoritmo Minamax tres en raya

17-Jan-2022 01:06
Daniel Gargallo Leal
0 Respuestas

Hola,
Estoy haciendo un algoritmo Minamax de tres en raya en Python a partir del siguiente video: https://www.youtube.com/watch?v=2Tr8LkyU78c. El código que tengo es el siguiente:


possible_choices = ["a1", "a2","a3","b1", "b2","b3","c1","c2","c3"]
init = {"a1" : 0, "a2" : 0, "a3":0, 
        "b1": 0, "b2": 0, "b3":0,
        "c1" : 0, "c2": 0, "c3": 0}

def jugadahumano():
    jugada = str(input("inserte aqui su jugada del tipo : a1"))
    if (jugada in init.keys()) and (jugada in possible_choices):
        init[jugada]=1
        possible_choices.remove(jugada)
    else:
        print("su jugada es incorrecta o bien ya hay una ficha sobre esa casilla")
        jugadahumano()


def jugadarobi():
    bestScore = -1000
    bestMove = 0
    for key in init.keys():
        if (init[key]==0):
            init[key]=2
            score = minimax(init,False)
            init[key]=0
            if (score > bestScore):
                bestScore = score
                bestMove = key

    init[bestMove]= 2



def minimax(board, isMaximizing):
    if (whichmarkwon(2)):
        return 100
    elif (whichmarkwon(1)):
        return -100
    elif (checkDraw()):
        return 0
    elif (isMaximizing==True):
        bestScore = -800
        for key in init.keys():
            if (init[key]== 0):
                board[key]=2
                score = minimax(init,False)
                board[key]=0
                if (score>bestScore):
                    bestScore = score
        return bestScore
    elif (isMaximizing==False):
        bestScore = 800
        for key in init.keys():
            if (init[key]== 0):
                init[key]==1
                score = minimax(init,True)
                init[key]==0
                if(score<bestScore):
                    bestScore = score
        return bestScore



def checkDraw():
    for key in init:
        if init[key]==0:
            return False
            
    return True


def whowin():
    
    if (init["a1"] == init["a2"] ==init["a3"]== 1) or (init["b1"] == init["b2"] == init["b3"]== 1) or (init["c1"] ==init["c2"] ==init["c3"]== 1) or (init["a1"] == init["b2"] == init["c3"]== 1) or (init["c1"] == init["b2"] == init["a3"]== 1) or (init["a1"] == init["b1"] == init["c1"]== 1) or (init["a2"] == init["b2"] == init["c2"]== 1) or (init["a3"] == init["b3"] == init["c3"]== 1):
        print("el ganador de la partida es el humano")
        return True 
    elif (init["a1"] == init["a2"] ==init["a3"]== 2) or (init["b1"] == init["b2"] == init["b3"]== 2) or (init["c1"] ==init["c2"] ==init["c3"]== 2) or (init["a1"] == init["b2"] == init["c3"]== 2) or (init["c1"] == init["b2"] == init["a3"]== 2) or (init["a1"] == init["b1"] == init["c1"]== 2) or (init["a2"] == init["b2"] == init["c2"]== 2) or (init["a3"] == init["b3"] == init["c3"]== 2):
        print("el ganador es el robot")
        return True
    elif checkDraw() == True:
        print("Ha habido empate.")
        return True
    else:
        return False

def whichmarkwon(mark):
    if (init["a1"] == init["a2"] ==init["a3"]== mark) or (init["b1"] == init["b2"] == init["b3"]== mark) or (init["c1"] ==init["c2"] ==init["c3"]== mark) or (init["a1"] == init["b2"] == init["c3"]== mark) or (init["c1"] == init["b2"] == init["a3"]== mark) or (init["a1"] == init["b1"] == init["c1"]== mark) or (init["a2"] == init["b2"] == init["c2"]== mark) or (init["a3"] == init["b3"] == init["c3"]== mark):
        return True
    else:
        return False







print("esto es el juego del tres en ralla, primero juegan los unos unos despues los doses")

while (whowin()== False)or (checkDraw==False):     
    whowin()
    jugadarobi()

    print(init.get("a1"), init.get("a2"), init.get("a3"))
    print(init.get("b1"), init.get("b2"), init.get("b3"))
    print(init.get("c1"), init.get("c2"), init.get("c3"))
    whowin()
    jugadahumano()


El bot no tendría que perder nunca, sin embargo, en la secuencia a1-b2-a2-a3-b1-c1, el jugador humano gana. He estado bastante tiempo intentando encontrar el problema pero no se donde esta.
Toda ayuda será muy agradecida,
Daniel G




(No se puede continuar esta discusión porque tiene más de dos meses de antigüedad. Si tienes dudas parecidas, abre un nuevo hilo.)