Está en la página 1de 4

# %load tic-tac-toe.

py
# tres en raya con algoritmo minimax

import sys
from operator import itemgetter

MAX = 1
MIN = -1

global jugada_maquina

def evaluarJuego(tablero, jugador):


tableroAux = tablero[:]
tableroAux2 = tablero[:]
centro = 0
esquina = 0
cruz = 0
jugadas= 0
posibilidadesComputador = [0,0,0]
posibilidadesJugador = [0,0,0]
Resultados = [0,0,0]

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 Resultado(tablero, jugador):


puntuacion = 0
if tablero[0] == jugador and tablero[1] == jugador and tablero[2] == jugador:
puntuacion = puntuacion + 1
if tablero[3] == jugador and tablero[4] == jugador and tablero[5] == jugador:
puntuacion = puntuacion + 1
if tablero[6] == jugador and tablero[7] == jugador and tablero[8] == jugador:
puntuacion = puntuacion + 1
if tablero[1] == jugador and tablero[4] == jugador and tablero[7] == jugador:
puntuacion = puntuacion + 1
if tablero[2] == jugador and tablero[5] == jugador and tablero[8] == jugador:
puntuacion = puntuacion + 1
if tablero[0] == jugador and tablero[3] == jugador and tablero[6] == jugador:
puntuacion = puntuacion + 1
if tablero[0] == jugador and tablero[4] == jugador and tablero[8] == jugador:
puntuacion = puntuacion + 1
if tablero[6] == jugador and tablero[4] == jugador and tablero[2] == jugador:
puntuacion = puntuacion + 1
return puntuacion

# metodo de minmax para generar las distintas opciones y seleccionar la mejor


jugada
def minimax(tablero, jugador):
NuevoTablero = evaluarJuego(tablero, jugador)
return NuevoTablero

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

# metodo que imprime el trablero de juego


def ver_tablero(tablero):

board = list(map(str, tablero))


for i in range(0, len(tablero)):
if tablero[i] == MAX:
board[i] = 'X'
elif tablero[i] == MIN:
board[i] = 'O'
else:
board[i] = ' '
print(' | |')
print(' ' + board[0] + ' | ' + board[1] + ' | ' + board[2])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[3] + ' | ' + board[4] + ' | ' + board[5])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[6] + ' | ' + board[7] + ' | ' + board[8])
print(' | |')

# metodo encargado de procesar el movimiento del jugador humano


def juega_humano(tablero):
ok= False
while not ok:
casilla = input("Casilla?")
# obtenemos la posici�n de la casilla de 1-9 y comparamos con su respectivo
indice en la lista
if str(casilla) in '0123456789' and len(str(casilla)) == 1 and
tablero[int(casilla)-1] == 0:
# asignamos a la casilla del jugador un valor de -1
tablero[int(casilla)-1] = MIN
ok = True
if casilla == "exit":
sys.exit(0)
return tablero

# metodo del ordenador donde se aplica min-max con la propagaci�n y selecci�n de la


mejor opci�n de la jugada teniendo la funci�n de evaluaci�n
def juega_ordenador(tablero):
tableroNuevo = minimax(tablero, MAX)
return tableroNuevo

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"

print("Ganador: " + gana)

También podría gustarte