Buenas Tardes,
Para un "trabajo-proyecto" del instituto tengo que hacer un Bot que juegue al tres en raya. Para hacerlo he visto un video en YouTube: https://www.youtube.com/watch?v=2Tr8LkyU78c para ver como hacerlo. Apliqué el cosas que dice al código del tres en raya que ya había hecho pero no acaba de funcionar bien, ya que no tendría que perder nunca y, sin embargo, pierde. Dado que el bot juega y se puede llevar a cabo una partida, supongo que el error está en el algoritmo.
Toda ayuda será muy agradecida.
Daniel G
Para un "trabajo-proyecto" del instituto tengo que hacer un Bot que juegue al tres en raya. Para hacerlo he visto un video en YouTube: https://www.youtube.com/watch?v=2Tr8LkyU78c para ver como hacerlo. Apliqué el cosas que dice al código del tres en raya que ya había hecho pero no acaba de funcionar bien, ya que no tendría que perder nunca y, sin embargo, pierde. Dado que el bot juega y se puede llevar a cabo una partida, supongo que el error está en el algoritmo.
Toda ayuda será muy agradecida.
Daniel G
Código:
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()