Está en la página 1de 5

# -*- coding: utf-8 -*-

"""triqui 1.2

Automatically generated by Colaboratory.

Original file is located at


https://colab.research.google.com/drive/1y2qc7Hqkd9lcuJTFQM3d0l2mzFcoi5or
"""

import random
import time
import os

def presentacion():
print("seleccione su ficha X / O")

ficha = ""
while ficha != "O" and ficha != "X":
ficha = input(" ").upper()
if ficha == "X":
humano = "X"
ordenador = "O"
elif ficha == "O":
humano = "O"
ordenador = "X"
return humano,ordenador

def mostrar_tablero(tablero):

print(" inicio del juego ")


print()
print(" | | ")
print(" 1 {} |2 {} |3 {} ".format(tablero[0],tablero[1],tablero[2]))
print(" | | ")
print("----------------------- ")
print(" | | ")
print(" 4 {} |5 {} |6 {} ".format(tablero[3],tablero[4],tablero[5]))
print(" | | ")
print("----------------------- ")
print(" | | ")
print(" 7 {} |8 {} |9 {} ".format(tablero[6],tablero[7],tablero[8]))
print(" | | ")

def seguir_jugando():

respuesta = imput("quiere seguir jugando? si es haci introduzca la letra s:


").upper()
if respuesta == "S" :
return True
else:
return False

def ganador(tablero, jugador):


if tablero[0] == tablero[1] == tablero[2] == jugador or \
tablero[3] == tablero[4] == tablero[5] == jugador or \
tablero[6] == tablero[7] == tablero[8] == jugador or \
tablero[0] == tablero[3] == tablero[6] == jugador or \
tablero[1] == tablero[4] == tablero[7] == jugador or \
tablero[2] == tablero[5] == tablero[8] == jugador or \
tablero[0] == tablero[4] == tablero[8] == jugador or \
tablero[2] == tablero[4] == tablero[6] == jugador:

return True
else:
return False

def tablero_lleno(tablero):

for i in tablero:
if i == " ":
return False
else:
return True

def casilla_libre(tablero,casilla):

return tablero[casilla] == " "

def movimiento_jugador(tablero):

posiciones = ["1","2","3","4","5","6","7","8","9"]
posicion = None
while True:
if posicion not in posiciones:
posicion = input(" te toca (1/9): ")
else:
posicion = int (posicion)
if not casilla_libre(tablero,posicion-1):
print(" casilla no disponible ")
else:
return posicion-1

def movimiento_ordenador(tablero,jugador):

if tablero[0] == tablero[1] == jugador and tablero[2] == " ":


casilla = 2

elif tablero[0] == tablero[2] == jugador and tablero[1] == " ":


casilla = 1

elif tablero[1] == tablero[2] == jugador and tablero[0] == " ":


casilla = 0

elif tablero[3] == tablero[4] == jugador and tablero[5] == " ":


casilla = 5

elif tablero[3] == tablero[5] == jugador and tablero[4] == " ":


casilla = 4

elif tablero[4] == tablero[5] == jugador and tablero[3] == " ":


casilla = 3

elif tablero[6] == tablero[7] == jugador and tablero[8] == " ":


casilla = 8
elif tablero[6] == tablero[8] == jugador and tablero[7] == " ":
casilla = 7

elif tablero[7] == tablero[8] == jugador and tablero[6] == " ":


casilla = 6

elif tablero[0] == tablero[2] == jugador and tablero[1] == " ":


casilla = 1

elif tablero[0] == tablero[3] == jugador and tablero[6] == " ":


casilla = 6

elif tablero[0] == tablero[6] == jugador and tablero[3] == " ":


casilla = 3

elif tablero[3] == tablero[6] == jugador and tablero[0] == " ":


casilla = 0

elif tablero[1] == tablero[4] == jugador and tablero[7] == " ":


casilla = 7

elif tablero[1] == tablero[7] == jugador and tablero[4] == " ":


casilla = 4

elif tablero[4] == tablero[7] == jugador and tablero[1] == " ":


casilla = 1

elif tablero[1] == tablero[4] == jugador and tablero[7] == " ":


casilla = 7

elif tablero[1] == tablero[7] == jugador and tablero[4] == " ":


casilla = 4

elif tablero[4] == tablero[7] == jugador and tablero[1] == " ":


casilla = 1

elif tablero[2] == tablero[5] == jugador and tablero[8] == " ":


casilla = 8

elif tablero[2] == tablero[8] == jugador and tablero[5] == " ":


casilla = 5

elif tablero[5] == tablero[8] == jugador and tablero[2] == " ":


casilla = 2

elif tablero[0] == tablero[4] == jugador and tablero[8] == " ":


casilla = 8

elif tablero[0] == tablero[8] == jugador and tablero[4] == " ":


casilla = 4

elif tablero[4] == tablero[8] == jugador and tablero[0] == " ":


casilla = 0

elif tablero[2] == tablero[4] == jugador and tablero[6] == " ":


casilla = 6

elif tablero[2] == tablero[6] == jugador and tablero[4] == " ":


casilla = 4

elif tablero[6] == tablero[4] == jugador and tablero[2] == " ":


casilla = 2

else:
while True:
casilla = random.randint(0,8)
if tablero[casilla] == " ":
break

return casilla

jugando = True

while jugando:
tablero = [" "] * 9

os.system("cls")

humano, ordenador = presentacion()


os.system("cls")

mostrar_tablero(tablero)

if humano == "O":
turno = "humano"
else:
turno = "ordenador"

partida = True

while partida:
if tablero_lleno(tablero):
print("empate")
partida = False

elif turno == "humano":


casilla = movimiento_jugador(tablero)
tablero[casilla] = humano
turno = "ordenador"
os.system("cls")
mostrar_tablero(tablero)
if ganador(tablero, humano):
print(" has ganado ")
partida = False
elif turno == "ordenador":
print("el ordenador esta pensando ...")
time.sleep(2)
casilla = movimiento_ordenador(tablero,humano)
os.system("cls")
if ganador(tablero,ordenador):
print("has perdido")
partida = False

jugando = seguir_jugando()'''

También podría gustarte