Advertisement
Guest User

Buscaminas

a guest
Dec 15th, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | None | 0 0
  1. import tkinter as tk
  2. import random
  3.  
  4. class Pieza():
  5.     def __init__(self,valor,hide):
  6.         self.valor = valor
  7.         self.hide = hide
  8.  
  9.     def getValor(self):
  10.         return self.valor
  11.  
  12.     def setValor(self,valor):
  13.         self.valor = valor
  14.  
  15.     def getHide(self):
  16.         return self.hide
  17.  
  18.     def sethide(self,hide):
  19.         self.hide = hide
  20.  
  21. def crearTabla(x,y):
  22.     temp = [[0 for i in range(x)] for i in range(y)]
  23.     for i in range(0,x):
  24.         for j in range(0,y):
  25.             if random.randrange(10) < 2:
  26.                 temp[i][j] = -1
  27.             else:
  28.                 temp[i][j] = 0
  29.     return temp
  30.  
  31. def addVecinos(tabla,i,j):
  32.     for x in range(0,i):
  33.         for y in range(0,j):
  34.             if tabla[x][y] == -1:
  35.                 if x>=0:
  36.                     if tabla[x-1][y]>=0:
  37.                         tabla[x-1][y] = tabla[x-1][y] + 1
  38.                     if y-1>=0:
  39.                         if tabla[x-1][y-1]>=0:
  40.                             tabla[x-1][y-1] = tabla[x-1][y-1] + 1
  41.                        
  42.                     if y+1<j:
  43.                         if tabla[x-1][y+1]>=0:
  44.                             tabla[x-1][y+1] = tabla[x-1][y+1] + 1
  45.                 if y>=0:
  46.                     if tabla[x][y-1]>=0:
  47.                         tabla[x][y-1] = tabla[x][y-1] + 1
  48.                 if y+1<j:
  49.                     if tabla[x][y+1]>=0:
  50.                         tabla[x][y+1] = tabla[x][y+1] + 1
  51.                 if x+1<i:
  52.                     if tabla[x+1][y]>=0:
  53.                         tabla[x+1][y] = tabla[x+1][y] + 1
  54.                     if y-1>=0:
  55.                         if tabla[x+1][y-1]>=0:
  56.                             tabla[x+1][y-1] = tabla[x+1][y-1] + 1  
  57.                     if y+1<j:                
  58.                         if tabla[x+1][y+1]>=0:
  59.                             tabla[x+1][y+1] = tabla[x+1][y+1] + 1
  60.  
  61. def tableToList(tabla,x,y):
  62.     temp = [[0 for i in range(x)] for i in range(y)]
  63.     for i in range(0,x):
  64.         for j in range(0,y):
  65.             temp[i][j] = Pieza(tabla[i][j],True)
  66.     return temp
  67.  
  68. def test(x,y,piezas):
  69.     piezas[x][y].sethide(False)
  70.     print(piezas[x][y].getValor())
  71.  
  72. def toggle(x,y):
  73.     buttonTable[x][y]["text"]=tabla[x][y]
  74.     buttonTable[x][y]["relief"]="sunken"
  75.  
  76. if __name__ == "__main__":
  77.     buttonTable = [[0 for i in range(10)] for i in range(10)]
  78.     tabla = crearTabla(10,10)
  79.     addVecinos(tabla,10,10)
  80.     piezas = tableToList(tabla,10,10)
  81.     root = tk.Tk()
  82.     root.title("Buscaminas")
  83.     inicio = tk.Button(root,text =":)")
  84.     inicio.grid(row=0,column=5)
  85.     for x in range(0,10):
  86.         for y in range(0,10):
  87.             cmd=lambda:toggle(x,y)
  88.             buttonTable[x][y] = tk.Button(root,text="",relief="raised",width=5,command=cmd)
  89.             buttonTable[x][y].grid(row=x+1,column=y)
  90.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement