]> Softwares of Agnibho - life.git/commitdiff
First major improvement
authorAgnibho Mondal <mail@agnibho.com>
Tue, 2 Dec 2014 13:40:01 +0000 (19:10 +0530)
committerAgnibho Mondal <mail@agnibho.com>
Tue, 2 Dec 2014 13:40:01 +0000 (19:10 +0530)
Board.py
Cell.py
README.TXT
__main__.py [new file with mode: 0644]

index d1ad84d288be36487e84c1a2f1f215b81f5e1e96..242c9bba57d30ed99d4502fe164433c713d0e34c 100644 (file)
--- a/Board.py
+++ b/Board.py
@@ -1,78 +1,94 @@
-'''
-    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
@@ -82,14 +98,12 @@ class Board(tk.Frame):
     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
diff --git a/Cell.py b/Cell.py
index a50c12342f0ecbb90555b357e578d4304960083a..f7094b99b5c0d834202ca5f510b94639f0ab14a8 100644 (file)
--- a/Cell.py
+++ b/Cell.py
@@ -1,34 +1,15 @@
-'''
-    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
@@ -42,18 +23,23 @@ class Cell(tk.Canvas):
   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
index f649aa1060b7f11b9c878c6fc46ba81ea6c3892f..ca5240aa97e8d1e28a9fa049e2cfa742ade4943f 100644 (file)
@@ -1,13 +1,16 @@
 =============
-Agnibho's Game of Life 1.0.0
+Agnibho's Game of Life 1.1.0
 =============
 
 General Usage Notes
 -------------------
--Start the program
+-Start the program by running the __main__.py file. Eg: python __main__.py
+-You can specify dimensions by providing width and height as arguments. Eg: python __main__.py 20 20
 -Seed the board by clicking on cells
 -Click <Start> to begin emulation
 -Click <Next Step> for emulating the next step
+-Select the <Automatic> checkbox for automatically running the game
+-Click <Reset> to reset all the cells
 
 Program Notes
 -------------
diff --git a/__main__.py b/__main__.py
new file mode 100644 (file)
index 0000000..8dddb3c
--- /dev/null
@@ -0,0 +1,33 @@
+#! /usr/bin/python
+import sys
+import Board
+
+width=20
+depth=20
+app=Board.Board()
+app.master.title("The Game of Life")
+
+if(len(sys.argv)>1):
+  if(len(sys.argv)==3):
+    try:
+      width=int(sys.argv[1])
+      depth=int(sys.argv[2])
+    except Exception:
+      print "Dimensions must be integer"
+      sys.exit()
+  else:
+    print "Argument not recognized"
+    sys.exit()
+if(width<5 or depth<5):
+  width=20
+  depth=20
+  print "Dimensions must be more than 5x5"
+  sys.exit()
+elif(width>app.winfo_screenwidth()/10 or depth>app.winfo_screenheight()/10-10):
+  width=20
+  depth=20
+  print "Dimensions are too large to fit in the screen. Maximum allowed dimension "+str(app.winfo_screenwidth()/10)+"x"+str(app.winfo_screenheight()/10-10)
+  sys.exit()
+
+app.render(width, depth)
+app.mainloop()
\ No newline at end of file