Advertisement
Geovanny

Gato en Python

Dec 31st, 2012
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from Tkinter import *
  3. v0=Tk()
  4. v0.title("Gato")
  5. v0.resizable(0,0)
  6.  
  7. color,nl,matriz,ganador=["red"],[],[0,0,0,0,0,0,0,0,0],[0]
  8.  
  9. def imprimir(t): print t
  10. def doit(f): v0.after(100,f) # Evento por tiempo
  11.  
  12. # Crea un gato
  13. def gato():
  14.     ind=0
  15.     c1,c2=0,0
  16.     while ind < 9:
  17.         nl.append(Button(v0,text="",width=10,height=5,bg="white"))
  18.         nl[ind].grid(row=c2,column=c1)
  19.         ind+=1
  20.         if c1==2: c1,c2=0,c2+1
  21.         else: c1+=1
  22.     nl[0].config(command=lambda: jugar(0)), nl[1].config(command=lambda: jugar(1))
  23.     nl[2].config(command=lambda: jugar(2)), nl[3].config(command=lambda: jugar(3))
  24.     nl[4].config(command=lambda: jugar(4)), nl[5].config(command=lambda: jugar(5))
  25.     nl[6].config(command=lambda: jugar(6)), nl[7].config(command=lambda: jugar(7))
  26.     nl[8].config(command=lambda: jugar(8))
  27.  
  28. def juego_finalizado(): # Revisa si el gato está lleno. Si lo está devuelve True
  29.     ind,largo=0,len(matriz)
  30.     while ind < largo:
  31.         if matriz[ind] == 0:
  32.             return False
  33.             break
  34.         ind+=1
  35.     return True
  36.  
  37. def tres_linea(lista):
  38.     if matriz[lista[0]] != 0 and matriz[lista[1]] != 0 and matriz[lista[2]] != 0:
  39.         if matriz[lista[0]] == matriz[lista[1]] == matriz[lista[2]]:
  40.             ganador[0]=matriz[lista[0]]
  41.             return True
  42.     return False
  43.  
  44. def gana():
  45.     if tres_linea([0,1,2]) or tres_linea([3,4,5]) or tres_linea([6,7,8]) or tres_linea([0,3,6]) or tres_linea([1,4,7]) or tres_linea([2,5,8]) or tres_linea([0,4,8]) or tres_linea([2,4,6]):
  46.         return True
  47.  
  48. def limpiar_botones():
  49.     color="white"
  50.     ind,largo=0,len(nl)
  51.     while ind < largo:
  52.         nl[ind].config(bg=color)
  53.         ind+=1
  54.     matriz[0],matriz[1],matriz[2],matriz[3],matriz[4],matriz[5],matriz[6],matriz[7],matriz[8]=0,0,0,0,0,0,0,0,0
  55.  
  56. def declarar_ganador():
  57.     v0.after(200,declarar_ganador)
  58.     if gana():
  59.         limpiar_botones()
  60.         v0.withdraw()
  61.         v1=Toplevel(v0)
  62.         if ganador[0] == 1:
  63.             l1=Label(v1,text="Ganador: Jugador 1 (Azul)",font=(16))
  64.             color[0]="red"
  65.         if ganador[0] == 2:
  66.             l1=Label(v1,text="Ganador: Jugador 2 (Rojo)",font=(16))
  67.             color[0]="blue"
  68.         l1.pack()
  69.         b1=Button(v1,text="OK",command=lambda: v1.withdraw() or v0.deiconify(),font=(16))
  70.         b1.pack()
  71.         v1.focus_force()
  72.  
  73. def reiniciar_juego():
  74.     v0.after(200,reiniciar_juego)
  75.     if juego_finalizado():
  76.         v0.withdraw()
  77.         v1=Toplevel(v0)
  78.         v1.title("Finalizado")
  79.         v1.resizable(0,0)
  80.         l1=Label(v1,text="Nadie Ganó.",font=(16))
  81.         l1.pack()
  82.         b1=Button(v1,text="OK",command=lambda: v1.withdraw() or v0.deiconify(),font=(16))
  83.         b1.pack()
  84.         doit(limpiar_botones())
  85.  
  86. def jugar(posicion):
  87.     if matriz[posicion] != 0:
  88.         print "Ya se jugó en esta posición"
  89.     else:
  90.         if color[0]=="red":
  91.             matriz[posicion]=1
  92.             color[0]="blue"
  93.             nl[posicion].config(bg=color)
  94.         elif color[0]=="blue":
  95.             matriz[posicion]=2
  96.             color[0]="red"
  97.             nl[posicion].config(bg=color)
  98.         print "ESTADO DE LA MATRIZ:",matriz
  99.  
  100. gato()
  101. declarar_ganador()
  102. reiniciar_juego()
  103. v0.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement