Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
life.LifeBoard Class Reference

List of all members.

Public Member Functions

def __init__
def set
def toggle
def erase
def display
def make_random

Public Attributes

 state
 scr
 Y
 char
 boring

Detailed Description

Encapsulates a Life board

Attributes:
X,Y : horizontal and vertical size of the board
state : dictionary mapping (x,y) to 0 or 1

Methods:
display(update_board) -- If update_board is true, compute the
                         next generation.  Then display the state
                         of the board and refresh the screen.
erase() -- clear the entire board
make_random() -- fill the board randomly
set(y,x) -- set the given cell to Live; doesn't refresh the screen
toggle(y,x) -- change the given cell from live to dead, or vice
               versa, and refresh the screen display

Definition at line 22 of file life.py.


Constructor & Destructor Documentation

def life.LifeBoard.__init__ (   self,
  scr,
  char = ord('*') 
)
Create a new LifeBoard instance.

scr -- curses screen object to use for display
char -- character used to render live cells (default: '*')

Definition at line 40 of file life.py.

00040 
00041     def __init__(self, scr, char=ord('*')):
00042         """Create a new LifeBoard instance.
00043 
00044         scr -- curses screen object to use for display
00045         char -- character used to render live cells (default: '*')
00046         """
00047         self.state = {}
00048         self.scr = scr
00049         Y, X = self.scr.getmaxyx()
00050         self.X, self.Y = X-2, Y-2-1
00051         self.char = char
00052         self.scr.clear()
00053 
00054         # Draw a border around the board
00055         border_line = '+'+(self.X*'-')+'+'
00056         self.scr.addstr(0, 0, border_line)
00057         self.scr.addstr(self.Y+1, 0, border_line)
00058         for y in range(0, self.Y):
00059             self.scr.addstr(1+y, 0, '|')
00060             self.scr.addstr(1+y, self.X+1, '|')
00061         self.scr.refresh()

Here is the caller graph for this function:


Member Function Documentation

def life.LifeBoard.display (   self,
  update_board = True 
)
Display the whole board, optionally computing one generation

Definition at line 89 of file life.py.

00089 
00090     def display(self, update_board=True):
00091         """Display the whole board, optionally computing one generation"""
00092         M,N = self.X, self.Y
00093         if not update_board:
00094             for i in range(0, M):
00095                 for j in range(0, N):
00096                     if (i,j) in self.state:
00097                         self.scr.addch(j+1, i+1, self.char)
00098                     else:
00099                         self.scr.addch(j+1, i+1, ' ')
00100             self.scr.refresh()
00101             return
00102 
00103         d = {}
00104         self.boring = 1
00105         for i in range(0, M):
00106             L = range( max(0, i-1), min(M, i+2) )
00107             for j in range(0, N):
00108                 s = 0
00109                 live = (i,j) in self.state
00110                 for k in range( max(0, j-1), min(N, j+2) ):
00111                     for l in L:
00112                         if (l,k) in self.state:
00113                             s += 1
00114                 s -= live
00115                 if s == 3:
00116                     # Birth
00117                     d[i,j] = 1
00118                     if curses.has_colors():
00119                         # Let's pick a random color!
00120                         self.scr.attrset(curses.color_pair(
00121                             random.randrange(1, 7)))
00122                     self.scr.addch(j+1, i+1, self.char)
00123                     self.scr.attrset(0)
00124                     if not live: self.boring = 0
00125                 elif s == 2 and live: d[i,j] = 1       # Survival
00126                 elif live:
00127                     # Death
00128                     self.scr.addch(j+1, i+1, ' ')
00129                     self.boring = 0
00130         self.state = d
00131         self.scr.refresh()

Here is the caller graph for this function:

def life.LifeBoard.erase (   self)
Clear the entire board and update the board display

Definition at line 84 of file life.py.

00084 
00085     def erase(self):
00086         """Clear the entire board and update the board display"""
00087         self.state = {}
00088         self.display(update_board=False)

Here is the call graph for this function:

Definition at line 132 of file life.py.

00132 
00133     def make_random(self):
00134         "Fill the board with a random pattern"
00135         self.state = {}
00136         for i in range(0, self.X):
00137             for j in range(0, self.Y):
00138                 if random.random() > 0.5:
00139                     self.set(j,i)
00140 

Here is the call graph for this function:

def life.LifeBoard.set (   self,
  y,
  x 
)
Set a cell to the live state

Definition at line 62 of file life.py.

00062 
00063     def set(self, y, x):
00064         """Set a cell to the live state"""
00065         if x<0 or self.X<=x or y<0 or self.Y<=y:
00066             raise ValueError("Coordinates out of range %i,%i"% (y, x))
00067         self.state[x,y] = 1

Here is the caller graph for this function:

def life.LifeBoard.toggle (   self,
  y,
  x 
)
Toggle a cell's state between live and dead

Definition at line 68 of file life.py.

00068 
00069     def toggle(self, y, x):
00070         """Toggle a cell's state between live and dead"""
00071         if x < 0 or self.X <= x or y < 0 or self.Y <= y:
00072             raise ValueError("Coordinates out of range %i,%i"% (y, x))
00073         if (x, y) in self.state:
00074             del self.state[x, y]
00075             self.scr.addch(y+1, x+1, ' ')
00076         else:
00077             self.state[x, y] = 1
00078             if curses.has_colors():
00079                 # Let's pick a random color!
00080                 self.scr.attrset(curses.color_pair(random.randrange(1, 7)))
00081             self.scr.addch(y+1, x+1, self.char)
00082             self.scr.attrset(0)
00083         self.scr.refresh()

Here is the call graph for this function:


Member Data Documentation

Definition at line 103 of file life.py.

Definition at line 50 of file life.py.

Definition at line 47 of file life.py.

Definition at line 46 of file life.py.

Definition at line 49 of file life.py.


The documentation for this class was generated from the following file: