miércoles, 13 de septiembre de 2017

Código del juego gato

from Tkinter import *
import tkMessageBox
import tkSimpleDialog
def bloq():
    for i in range(0, 9):
        lisb[i].config(state="disable")
def inij():
    for i in range(0, 9):
        lisb[i].config(state="normal")
        lisb[i].config(bg="lightgray")
        lisb[i].config(text="")
        tab[i] = "N"    global nomj1, nomj2  
    nomj1 = tkSimpleDialog.askstring("Jugador", "Escribe el nombre del jugador 1: ")
    nomj2 = tkSimpleDialog.askstring("Jugador", "Escribe el nombre del jugador 2: ")
    turj.set("Turno: " + nomj1)
def cam(num):
    global turno, nomj1, nomj2
    if tab[num] == "N" and turno == 0:
        lisb[num].config(text="X")
        lisb[num].config(bg="white")
        tab[num] = "X"        turno = 1        turj.set("Turno: " + nomj2)
    elif tab[num] == "N" and turno == 1:
        lisb[num].config(text="O")
        lisb[num].config(bg="lightblue")
        tab[num] = "O"        turno = 0        turj.set("Turno: " + nomj1)
    lisb[num].config(state="disable")
    verif()
def verif():
    if (tab[0] == "X" and tab[1] == "X" and tab[2] == "X") or (tab[3] == "X" and 
                tab[4] == "X" and tab[5] == "X") or (
                tab[6] == "X" and tab[7] == "X" and tab[8] == "X"):
        bloq()
        tkMessageBox.showinfo("Ganaste", "Ganaste jugador: " + nomj1)
    elif (tab[0] == "X" and tab[3] == "X" and tab[6] == "X") or (tab[1] == "X" and 
                tab[4] == "X" and tab[7] == "X") or (
                tab[2] == "X" and tab[5] == "X" and tab[8] == "X"):
        bloq()
        tkMessageBox.showinfo("Ganaste", "Ganaste jugador: " + nomj1)
    elif (tab[0] == "X" and tab[4] == "X" and tab[8] == "X") or (tab[2] == "X" and
                tab[4] == "X" and tab[6] == "X"):
        bloq()
        tkMessageBox.showinfo("Ganaste", "Ganaste jugador: " + nomj1)
    elif (tab[0] == "O" and tab[1] == "O" and tab[2] == "O") or (tab[3] == "O" and 
                tab[4] == "O" and tab[5] == "O") or (
                tab[6] == "O" and tab[7] == "O" and tab[8] == "O"):
        bloq()
        tkMessageBox.showinfo("Ganaste", "Ganaste jugador: " + nomj2)
    elif (tab[0] == "O" and tab[3] == "O" and tab[6] == "O") or (tab[1] == "O" and 
                tab[4] == "O" and tab[7] == "O") or (
                tab[2] == "O" and tab[5] == "O" and tab[8] == "O"):
        bloq()
        tkMessageBox.showinfo("Ganaste", "Ganaste jugador: " + nomj2)
    elif (tab[0] == "O" and tab[4] == "O" and tab[8] == "O") or (tab[2] == "O" and 
                tab[4] == "O" and tab[6] == "O"):
        bloq()
        tkMessageBox.showinfo("Ganaste", "Ganaste jugador: " + nomj2)
ven = Tk()
ven.geometry("370x460")
ven.title("Juego del gato")
turno = 0
nomj1 = ""nomj2 = ""
lisb = []
tab = []
turj = StringVar()

for i in range(0, 9):
    tab.append("N")
boton0 = Button(ven, width=9, height=3, relief=SOLID,  command=lambda: cam(0))
lisb.append(boton0)
boton0.place(x=50, y=50)
boton1 = Button(ven, width=9, height=3, relief=SOLID,  command=lambda: cam(1))
lisb.append(boton1)
boton1.place(x=150, y=50)
boton2 = Button(ven, width=9, height=3, relief=SOLID, command=lambda: cam(2))
lisb.append(boton2)
boton2.place(x=250, y=50)
boton3 = Button(ven, width=9, height=3, relief=SOLID,  command=lambda: cam(3))
lisb.append(boton3)
boton3.place(x=50, y=150)
boton4 = Button(ven, width=9, height=3, relief=SOLID,  command=lambda: cam(4))
lisb.append(boton4)
boton4.place(x=150, y=150)
boton5 = Button(ven, width=9, height=3, relief=SOLID,  command=lambda: cam(5))
lisb.append(boton5)
boton5.place(x=250, y=150)
boton6 = Button(ven, width=9, height=3, relief=SOLID,  command=lambda: cam(6))
lisb.append(boton6)
boton6.place(x=50, y=250)
boton7 = Button(ven, width=9, height=3, relief=SOLID,  command=lambda: cam(7))
lisb.append(boton7)
boton7.place(x=150, y=250)
boton8 = Button(ven, width=9, height=3, relief=SOLID,  command=lambda: cam(8))
lisb.append(boton8)
boton8.place(x=250, y=250)
tue = Label(ven, textvariable=turj).place(x=140, y=10)
bini = Button(ven, bg='blue', fg='white', text='Iniciar juego', cursor="sizing",
 width=15, height=3,
              command=inij).place(x=130, y=360)
bloq()

linea = Canvas(ven, width=310, height=10)
linea.place(x=30, y=120)
linea.create_line(310, 0, 0, 0, width=25, fill='black')
l2 = Canvas(ven, width=310, height=10)
l2.place(x=30, y=220)
l2.create_line(310, 0, 0, 0, width=25, fill='black')
l3 = Canvas(ven, width=10, height=310)
l3.place(x=130, y=25)
l3.create_line(0, 310, 0, 0, width=25, fill='black')
l4 = Canvas(ven, width=10, height=310)
l4.place(x=230, y=25)
l4.create_line(0, 310, 0, 0, width=25, fill='black')

ven.mainloop()


No hay comentarios:

Publicar un comentario