-'''
- Agnibho's Game of Life - Python implementation
- Copyright (C) 2014 Agnibho Mondal
-
- This file is part of .Agnibho's Game of Life
-
- Agnibho's Game of Life is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- Agnibho's Game of Life is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Agnibho's Game of Life. If not, see <http://www.gnu.org/licenses/>.
-'''
-
+from Tkinter import BooleanVar
import Tkinter as tk
import Cell
-
-WIDTH=20
-DEPTH=20
-
-Num=0
class Board(tk.Frame):
cells=[]
+ WIDTH=20
+ DEPTH=20
+ Num=0
+ auto={}
+ timer={}
def __init__(self, master=None):
- global WIDTH
- global DEPTH
tk.Frame.__init__(self, master)
self.grid()
- self.contain=tk.Frame(self)
+ self.auto=BooleanVar()
+
+ def render(self, width, depth,):
+ self.WIDTH=width
+ self.DEPTH=depth
+ self.contain=tk.Canvas(self, width=self.WIDTH*10, height=self.DEPTH*10)
self.contain.grid()
- for i in range(0, WIDTH):
+ for i in range(0, self.WIDTH):
self.cells.append([])
- for j in range (0, DEPTH):
- self.cells[i].append(Cell.Cell(self.contain))
- self.cells[i][j].grid(row=i, column=j)
- self.launch=tk.Button(self, text="Start", command=self.start)
- self.launch.grid()
+ for j in range (0, self.DEPTH):
+ 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))
+ self.startbtn=tk.Button(self, text="Start", command=self.start)
+ self.autobtn=tk.Checkbutton(self, text="Automatic", command=self.run, variable=self.auto)
+ self.stepbtn=tk.Button(self, text="Next Step", command=self.step)
+ self.resetbtn=tk.Button(self, text="Reset", command=self.reset)
+ self.startbtn.grid()
+
+ def setdim(w, h):
+ self.WIDTH=w
+ self.DEPTH=h
def start(self):
- global WIDTH
- global DEPTH
- for i in range(0, WIDTH):
- for j in range (0, DEPTH):
+ for i in range(0, self.WIDTH):
+ for j in range (0, self.DEPTH):
self.cells[i][j].freeze()
- self.launch.grid_remove()
- self.btn=tk.Button(self, text="Next Step", command=self.step)
- self.btn.grid()
+ self.startbtn.grid_remove()
+ if(self.WIDTH>30):
+ self.stepbtn.grid(row=1)
+ self.resetbtn.grid(row=1, sticky="E")
+ self.autobtn.grid(row=1, sticky="W")
+ elif(self.WIDTH>10):
+ self.stepbtn.grid(row=1, sticky="W")
+ self.resetbtn.grid(row=1, sticky="E")
+ self.autobtn.grid(row=2)
+ else:
+ self.stepbtn.grid()
+ self.resetbtn.grid()
+ self.autobtn.grid()
+
+ def reset(self):
+ for i in range(0, self.WIDTH):
+ for j in range (0, self.DEPTH):
+ self.cells[i][j].makeDead()
+ self.cells[i][j].commit()
+ self.cells[i][j].unfreeze()
+ self.stepbtn.grid_remove()
+ self.resetbtn.grid_remove()
+ self.autobtn.grid_remove()
+ self.startbtn.grid()
+ if(self.auto.get()):
+ self.autobtn.invoke()
def step(self):
- global WIDTH
- global DEPTH
- global Num
- #print str(Num)+" step"
- Num+=1
- for i in range(0, WIDTH):
- for j in range (0, DEPTH):
+ self.Num+=1
+ for i in range(0, self.WIDTH):
+ for j in range (0, self.DEPTH):
z=self.count(i, j)
if(self.cells[i][j].getState()=="live"):
- #print str(Num)+" "+str(i)+" "+str(j)+" "+str(z)+" "+self.cells[i][j].getState()
if(z<2 or z>3):
self.cells[i][j].makeDead()
elif(self.cells[i][j].getState()=="dead"):
if(z==3):
self.cells[i][j].makeLive()
- for i in range(0, WIDTH):
- for j in range (0, DEPTH):
+ for i in range(0, self.WIDTH):
+ for j in range (0, self.DEPTH):
self.cells[i][j].commit()
+ if(self.auto.get()):
+ self.timer=self.after(1000, self.step)
+
+ def run(self):
+ if(self.auto.get()):
+ self.stepbtn["state"]="disabled"
+ self.timer=self.after(1000, self.step)
+ else:
+ self.stepbtn["state"]="normal"
+ self.after_cancel(self.timer)
def count(self, i, j):
z=0
return z
def getCell(self, x, y):
- global WIDTH
- global DEPTH
if(x==-1):
- x=WIDTH-1
- elif(x>=WIDTH):
+ x=self.WIDTH-1
+ elif(x>=self.WIDTH):
x=0
if(y==-1):
- y=DEPTH-1
- elif(y>=DEPTH):
+ y=self.DEPTH-1
+ elif(y>=self.DEPTH):
y=0
return self.cells[x][y]
\ No newline at end of file
-'''
- Agnibho's Game of Life - Python implementation
- Copyright (C) 2014 Agnibho Mondal
-
- This file is part of .Agnibho's Game of Life
-
- Agnibho's Game of Life is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- Agnibho's Game of Life is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Agnibho's Game of Life. If not, see <http://www.gnu.org/licenses/>.
-'''
-
import Tkinter as tk
-class Cell(tk.Canvas):
+class Cell():
+ __obj={}
+ __parent={}
__state="dead"
__buff="dead"
- __obj={}
- def __init__(self, master=None):
- tk.Canvas.__init__(self, master, height=10, width=10)
- self.__obj=self.create_rectangle(0, 0, 10, 10, fill="white")
- self.tag_bind(self.__obj, '<ButtonPress-1>', self.toggle)
+ def __init__(self, item, parent):
+ self.__obj=item
+ self.__parent=parent
+ self.__parent.tag_bind(self.__obj, '<ButtonPress-1>', self.toggle)
def getState(self):
return self.__state
def commit(self):
self.__state=self.__buff
if(self.__state=="live"):
- self.itemconfig(self.__obj, fill="black")
+ self.__parent.itemconfig(self.__obj, fill="green")
elif(self.__state=="dead"):
- self.itemconfig(self.__obj, fill="white")
+ self.__parent.itemconfig(self.__obj, fill="white")
def toggle(self, event):
if(self.__state=="live"):
self.__state="dead"
- self.itemconfig(self.__obj, fill="white")
+ self.__buff="dead"
+ self.__parent.itemconfig(self.__obj, fill="white")
elif(self.__state=="dead"):
self.__state="live"
- self.itemconfig(self.__obj, fill="black")
+ self.__buff="live"
+ self.__parent.itemconfig(self.__obj, fill="green")
def freeze(self):
- self.tag_unbind(self.__obj, '<ButtonPress-1>')
\ No newline at end of file
+ self.__parent.tag_unbind(self.__obj, '<ButtonPress-1>')
+
+ def unfreeze(self):
+ self.__parent.tag_bind(self.__obj, '<ButtonPress-1>', self.toggle)
\ No newline at end of file