]> Softwares of Agnibho - life.git/blob - Board.py
242c9bba57d30ed99d4502fe164433c713d0e34c
[life.git] / Board.py
1 from Tkinter import BooleanVar
2 import Tkinter as tk
3 import Cell
4
5 class Board(tk.Frame):
6 cells=[]
7 WIDTH=20
8 DEPTH=20
9 Num=0
10 auto={}
11 timer={}
12
13 def __init__(self, master=None):
14 tk.Frame.__init__(self, master)
15 self.grid()
16 self.auto=BooleanVar()
17
18 def render(self, width, depth,):
19 self.WIDTH=width
20 self.DEPTH=depth
21 self.contain=tk.Canvas(self, width=self.WIDTH*10, height=self.DEPTH*10)
22 self.contain.grid()
23 for i in range(0, self.WIDTH):
24 self.cells.append([])
25 for j in range (0, self.DEPTH):
26 self.cells[i].append(Cell.Cell(self.contain.create_rectangle(i*10, j*10, i*10+10, j*10+10, fill="white", outline="gray"), self.contain))
27 self.startbtn=tk.Button(self, text="Start", command=self.start)
28 self.autobtn=tk.Checkbutton(self, text="Automatic", command=self.run, variable=self.auto)
29 self.stepbtn=tk.Button(self, text="Next Step", command=self.step)
30 self.resetbtn=tk.Button(self, text="Reset", command=self.reset)
31 self.startbtn.grid()
32
33 def setdim(w, h):
34 self.WIDTH=w
35 self.DEPTH=h
36
37 def start(self):
38 for i in range(0, self.WIDTH):
39 for j in range (0, self.DEPTH):
40 self.cells[i][j].freeze()
41 self.startbtn.grid_remove()
42 if(self.WIDTH>30):
43 self.stepbtn.grid(row=1)
44 self.resetbtn.grid(row=1, sticky="E")
45 self.autobtn.grid(row=1, sticky="W")
46 elif(self.WIDTH>10):
47 self.stepbtn.grid(row=1, sticky="W")
48 self.resetbtn.grid(row=1, sticky="E")
49 self.autobtn.grid(row=2)
50 else:
51 self.stepbtn.grid()
52 self.resetbtn.grid()
53 self.autobtn.grid()
54
55 def reset(self):
56 for i in range(0, self.WIDTH):
57 for j in range (0, self.DEPTH):
58 self.cells[i][j].makeDead()
59 self.cells[i][j].commit()
60 self.cells[i][j].unfreeze()
61 self.stepbtn.grid_remove()
62 self.resetbtn.grid_remove()
63 self.autobtn.grid_remove()
64 self.startbtn.grid()
65 if(self.auto.get()):
66 self.autobtn.invoke()
67
68 def step(self):
69 self.Num+=1
70 for i in range(0, self.WIDTH):
71 for j in range (0, self.DEPTH):
72 z=self.count(i, j)
73 if(self.cells[i][j].getState()=="live"):
74 if(z<2 or z>3):
75 self.cells[i][j].makeDead()
76 elif(self.cells[i][j].getState()=="dead"):
77 if(z==3):
78 self.cells[i][j].makeLive()
79 for i in range(0, self.WIDTH):
80 for j in range (0, self.DEPTH):
81 self.cells[i][j].commit()
82 if(self.auto.get()):
83 self.timer=self.after(1000, self.step)
84
85 def run(self):
86 if(self.auto.get()):
87 self.stepbtn["state"]="disabled"
88 self.timer=self.after(1000, self.step)
89 else:
90 self.stepbtn["state"]="normal"
91 self.after_cancel(self.timer)
92
93 def count(self, i, j):
94 z=0
95 for cl in [self.getCell(i-1, j-1), self.getCell(i-1, j), self.getCell(i, j-1), self.getCell(i+1, j+1), self.getCell(i+1, j), self.getCell(i, j+1), self.getCell(i-1, j+1), self.getCell(i+1, j-1)]:
96 if(cl.getState()=="live"):
97 z+=1
98 return z
99
100 def getCell(self, x, y):
101 if(x==-1):
102 x=self.WIDTH-1
103 elif(x>=self.WIDTH):
104 x=0
105 if(y==-1):
106 y=self.DEPTH-1
107 elif(y>=self.DEPTH):
108 y=0
109 return self.cells[x][y]