Documentos de Académico
Documentos de Profesional
Documentos de Cultura
py
# tres en raya con algoritmo minimax
import sys
from operator import itemgetter
MAX = 1
MIN = -1
global jugada_maquina
if tablero[4] == 0:
centro = 4
if tablero[0] == 0:
esquina = 0
jugadas = jugadas + 1
if tablero[2] == 0:
esquina = 2
jugadas = jugadas + 1
if tablero[6] == 0:
esquina = 6
jugadas = jugadas + 1
if tablero[8] == 0:
esquina = 8
jugadas = jugadas + 1
if tablero[1] == 0:
cruz = 1
jugadas = jugadas + 1
if tablero[3] == 0:
jugadas = jugadas + 1
cruz = 3
if tablero[5] == 0:
cruz = 5
jugadas = jugadas + 1
if tablero[7] == 0:
cruz = 7
jugadas = jugadas + 1
if centro != 0:
tableroAux[centro] = MAX
tableroAux2[centro] = MIN
for i in range(0,len(tableroAux)):
if tableroAux[i] == 0:
tableroAux[i] = MAX
tableroAux2[i] = MIN
posibilidadPC = Resultado(tableroAux, MAX)
posibilidadJugador = Resultado(tableroAux2, MIN)
posibilidadesComputador[0] = posibilidadPC
posibilidadesJugador[0] = posibilidadesJugador
tableroAux[centro] = 0
tableroAux2[centro] = 0
if esquina != 0:
tableroAux[esquina] = MAX
tableroAux2[esquina] = MIN
for i in range(0,len(tableroAux)):
if tableroAux[i] == 0:
tableroAux[i] = MAX
tableroAux2[i] = MIN
posibilidadPC = Resultado(tableroAux, MAX)
posibilidadJugador = Resultado(tableroAux2, MIN)
posibilidadesComputador[1] = posibilidadPC
posibilidadesJugador[1] = posibilidadesJugador
tableroAux[esquina] = 0
tableroAux2[esquina] = 0
if cruz != 0:
tableroAux[cruz] = MAX
tableroAux2[cruz] = MIN
for i in range(0,len(tableroAux)):
if tableroAux[i] == 0:
tableroAux[i] = MAX
tableroAux2[i] = MIN
posibilidadPC = Resultado(tableroAux, MAX)
posibilidadJugador = Resultado(tableroAux2, MIN)
posibilidadesComputador[2] = posibilidadPC
posibilidadesJugador[2] = posibilidadesJugador
tableroAux[cruz] = 0
tableroAux2[cruz] = 0
for i in range(0, len(Resultados):
Resultados[i] = posibilidadesComputador[i] -posibilidadesJugador[i]
if jugador == MAX:
Casilla = max(Resultados)
else:
Casilla = min(Resultados)
Posicion = 0
for i in range(0, len(Resultados):
if Posicion == Casilla:
Posicion = i
if Posicion == 0
tablero[centro] = MAX
if Posicion == 1
tablero[esquina] = MAX
else:
tablero[cruz] = MAX
return tablero
def game_over(tablero):
# hay tablas?
no_tablas = False
for i in range(0, len(tablero)):
if tablero[i] == 0:
no_tablas = True
# hay ganador?
if ganador(tablero) == 0 and no_tablas:
return False
else:
return True
def ganador(tablero):
# combinaciones de estados de ganadores
lineas = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5,8], [0,
4, 8], [2, 4, 6]]
ganador = 0
for linea in lineas:
if tablero[linea[0]] == tablero[linea[1]] and tablero[linea[0]] ==
tablero[linea[2]] and tablero[linea[0]] != 0:
ganador = tablero[linea[0]]
return ganador
if __name__ == "__main__":
print("Introduce casilla o exit para terminar")
tablero = [0,0,0,0,0,0,0,0,0]
while(True):
ver_tablero(tablero)
tablero = juega_ordenador(tablero)
if game_over(tablero):
break
tablero = juega_humano(tablero)
if game_over(tablero):
break
ver_tablero(tablero)
g = ganador(tablero)
if g == 0:
gana = "Tablas"
elif g == MIN:
gana = "Jugador"
else:
gana = "Ordenador"