]>
Softwares of Agnibho - life.git/blob - Board.py
242c9bba57d30ed99d4502fe164433c713d0e34c
1 from Tkinter
import BooleanVar
13 def __init__(self
, master
=None):
14 tk
.Frame
.__init
__(self
, master
)
16 self
.auto
=BooleanVar()
18 def render(self
, width
, depth
,):
21 self
.contain
=tk
.Canvas(self
, width
=self
.WIDTH
*10, height
=self
.DEPTH
*10)
23 for i
in range(0, self
.WIDTH
):
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
)
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()
43 self
.stepbtn
.grid(row
=1)
44 self
.resetbtn
.grid(row
=1, sticky
="E")
45 self
.autobtn
.grid(row
=1, sticky
="W")
47 self
.stepbtn
.grid(row
=1, sticky
="W")
48 self
.resetbtn
.grid(row
=1, sticky
="E")
49 self
.autobtn
.grid(row
=2)
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()
70 for i
in range(0, self
.WIDTH
):
71 for j
in range (0, self
.DEPTH
):
73 if(self
.cells
[i
][j
].getState()=="live"):
75 self
.cells
[i
][j
].makeDead()
76 elif(self
.cells
[i
][j
].getState()=="dead"):
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()
83 self
.timer
=self
.after(1000, self
.step
)
87 self
.stepbtn
["state"]="disabled"
88 self
.timer
=self
.after(1000, self
.step
)
90 self
.stepbtn
["state"]="normal"
91 self
.after_cancel(self
.timer
)
93 def count(self
, i
, j
):
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"):
100 def getCell(self
, x
, y
):
109 return self
.cells
[x
][y
]