Python - Problema algoritmo minamax en python

 
Vista:

Problema algoritmo minamax en python

Publicado por Daniel G (2 intervenciones) el 22/01/2022 14:53:13
Saludos,
Para un "trabajo" del instituto tengo que hacer un bot del tres en ralla que no pierda nunca, solo puedes empatar o perder contra el. Para hacerlo miré como hacerlo en un video de YouTube: https://www.youtube.com/watch?v=2Tr8LkyU78c . Ha habido algunas cosas que he adaptado a la parte del juego que ya tenia hecho. El ordenador juega primero.
El problema viene de que cuando (como mínimo) hay esta sucesión de jugadas, (a1, b2, a2, a3, b1, c1) el jugador humano pierde y no debería ser así. Otro problema es que el código tarda mucho en ejecutarse pero eso no es tan importante.
Toda ayuda será bien agradecida.

Daniel G
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 1
    elif (whichmarkwon(1)):
        return -1
    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()
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder