[Tutor] Minesweeper implementing 20%, weird problem with matrixes
Alberto Troiano
albertito_g at hotmail.com
Wed Jul 13 23:36:44 CEST 2005
Hey tutors
I think I got this a little, but I have a very weird problem
This is the code I'm using:
######################
class buscaminas(object):
def __init__(self):
self.col=0
self.marcas=Tkinter.BooleanVar()
self.color=Tkinter.BooleanVar()
self.sonido=Tkinter.BooleanVar()
self.fil=0
self.byob=0
menubar= Menu(root)
archivo=Menu(menubar,tearoff=0)
archivo.add_command(label="Nuevo",underline=0,command=self.initialize)
archivo.add_radiobutton(label="Fácil",underline=0,command=lambda:
self.nivel(9,9,10))
archivo.add_radiobutton(label="Medio",underline=0,command=lambda:
self.nivel(16,16,40))
archivo.add_radiobutton(label="Dificil",underline=0,command=lambda:
self.nivel(16,30,99))
archivo.add_radiobutton(label="Personalizado",underline=0,command=self.hello)
archivo.add_separator()
archivo.add_checkbutton(label="Marcas
(?)",variable=self.marcas,underline=0,command=self.hello)
archivo.add_checkbutton(label="Color",underline=0,command=self.hello)
archivo.add_checkbutton(label="Sonido",underline=1,command=self.hello)
archivo.add_separator()
archivo.add_command(label="Mejores
Tiempos",underline=8,command=self.hello)
archivo.add_separator()
archivo.add_command(label="Salir",underline=0,command=root.destroy)
menubar.add_cascade(label="Juego",underline=0,menu=archivo)
help = Menu(menubar, tearoff=0)
help.add_command(label="Contenido", command=self.hello)
help.add_command(label="Indice de ayuda", command=self.hello)
help.add_command(label="Como usar la ayuda", command=self.hello)
help.add_separator()
help.add_command(label="Acerca de...", command=self.hello)
menubar.add_cascade(label="Ayuda",underline=1, menu=help)
root.config(menu=menubar)
root.bind_all("<F1>",lambda d: self.hello())
root.bind_all("<F2>",lambda s: self.initialize())
root.bind_all("<Control-x>",lambda x: root.destroy())
def hello(self):
print "funca"
def initialize(self):
self.buttonmatrix=[]
self.statematrix=[]
self.bombmatrix=[]
for i in range(self.fil):
aux=[]
for j in range(self.col):
aux.append(None)
self.buttonmatrix.append(aux)
self.bombmatrix.append(aux)
self.statematrix.append(aux)
self.bombs()
def nivel(self,f,c,b):
self.col=c
self.fil=f
self.byob=b
self.initialize()
def bombs(self):
c=0
while not c==self.byob:
fi=random.randrange(self.fil)
co=random.randrange(self.col)
if self.bombmatrix[fi][co]==None:
self.bombmatrix[fi][co]=9
c+=1
self.numbers()
def putnumber(self,row,column):
c=0
fi=row-1
ci=column-1
ff=row+2
cf=column+2
for i in range(fi,ff):
for j in range(ci,cf):
if (i<0) or (i>=self.fil) or (j<0) or (j>=self.col):
pass
elif self.bombmatrix[i][j]==9:
c+=1
else:
pass
return c
def numbers(self):
for i in range(self.fil):
for j in range(self.col):
if self.bombmatrix[i][j]==9:
pass
else:
self.bombmatrix[i][j]=self.putnumber(i,j)
self.nuevo()
def nuevo(self):
for el in self.buttonmatrix:
print el
for l in self.statematrix:
print l
for e in self.bombmatrix:
print e
def main():
global root
root=Tk()
root.title("Buscaminas")
root.iconbitmap("images\mina.ico")
a=buscaminas()
root.mainloop()
main()
##############
The following variables are the one with the problem:
self.bombmatrix, self.buttonmatrix, self.statematrix
I initialize them by putting None along all the fields (this is to allow the
user to have any size of matrix) then I charge with numbers the
self.bombmatrix but weirdly the others variables are filled with the same
values, which is very strange to me since I'm not assigning them each others
Any hint?
Thanks in advanced and sorry for the long piece of code
Alberto
More information about the Tutor
mailing list