import random
class gameOfLife(object):
def __init__(self, columns, rows, grid = []):
self.grid = grid
self.columns = columns
self.rows = rows
for i in range(0,self.rows):
sub = ['-']
self.grid.append(sub)
for sublist in grid:
for i in range(0,self.columns-1):
cell = ['-']
sublist.append(cell)
def printGrid(self):
for sublist in self.grid:
print sublist
def getAdj(self, x, y):
self.x = x
self.y = y
count = 0
if self.x == 0 and self.y == 0:
print 'a'
print 'x',self.x
print 'y',self.y
if self.grid[self.y+1][self.x] == '*':
count = count + 1
if self.grid[self.y][self.x+1] == '*':
count = count + 1
if self.grid[self.y+1][self.x+1] == '*':
count = count + 1
self.count = count
elif self.x == 0 and self.y == self.rows - 1:
print 'b'
print 'x',self.x
print 'y',self.y
if self.grid[self.y-1][self.x] == '*':
count = count + 1
if self.grid[self.y-1][self.x+1] == '*':
count = count + 1
if self.grid[self.y][self.x+1] == '*':
count = count + 1
elif self.x == 0 and self.rows-1>self.y>0:
print 'i'
print 'x',self.x
print 'y',self.y
if self.grid[self.y+1][self.x] == '*':
count = count + 1
if self.grid[self.y-1][self.x] == '*':
count = count + 1
if self.grid[self.y+1][self.x+1] == '*':
count = count + 1
if self.grid[self.y][self.x+1] == '*':
count = count + 1
if self.grid[self.y-1][self.x+1] == '*':
count = count + 1
elif self.x == self.columns - 1 and self.y ==0:
print 'c'
print 'x',self.x
print 'y',self.y
if self.grid[self.y][self.x-1] == '*':
count = count + 1
if self.grid[self.y+1][self.x-1] == '*':
count = count + 1
if self.grid[self.y+1][self.x] == '*':
count = count + 1
elif 0<self.x<self.columns-1 and self.y == 0:
print 'h'
print 'x',self.x
print 'y',self.y
if self.grid[self.y][self.x-1] == '*':
count = count + 1
if self.grid[self.y][self.x+1] == '*':
count = count + 1
if self.grid[self.y+1][self.x-1] == '*':
count = count + 1
if self.grid[self.y+1][self.x] == '*':
count = count + 1
if self.grid[self.y+1][self.x+1] == '*':
count = count + 1
elif 0<self.x<self.columns-1 and self.y == self.rows -1:
print 'g'
print 'x',self.x
print 'y',self.y
if self.grid[self.y][self.x-1] == '*':
count = count + 1
if self.grid[self.y][self.x+1] == '*':
count = count + 1
if self.grid[self.y-1][self.x-1] == '*':
count = count + 1
if self.grid[self.y-1][self.x] == '*':
count = count + 1
if self.grid[self.y-1][self.x+1] == '*':
count = count + 1
elif x == self.columns -1 and 0<self.y<self.rows-1:
print 'f'
print 'x',self.x
print 'y',self.y
if self.grid[self.y-1][self.x] == '*':
count = count + 1
if self.grid[self.y+1][self.x] == '*':
count = count + 1
if self.grid[self.y-1][self.x-1] == '*':
count = count + 1
if self.grid[self.y][self.x-1] == '*':
count = count + 1
if self.grid[self.y+1][self.x-1] == '*':
count = count + 1
elif self.x == self.columns - 1 and self.y == self.rows - 1:
print 'd'
print 'x',self.x
print 'y',self.y
if self.grid[self.y][self.x-1] == '*':
count = count + 1
if self.grid[self.y-1][self.x-1] == '*':
count = count + 1
if self.grid[self.y-1][self.x] == '*':
count = count + 1
else:
print 'e'
print 'x',self.x
print 'y',self.y
if self.grid[self.y+1][self.x-1] == '*':
count = count + 1
if self.grid[self.y+1][self.x] == '*':
count = count + 1
if self.grid[self.y+1][self.x+1] == '*':
count = count + 1
if self.grid[self.y][self.x-1] == '*':
count = count + 1
if self.grid[self.y][self.x+1] == '*':
count = count + 1
if self.grid[self.y-1][self.x-1] == '*':
count = count + 1
if self.grid[self.y-1][self.x] == '*':
count = count + 1
if self.grid[self.y-1][self.x+1] == '*':
count = count + 1
print count
def nextStep(self):
for x in range(0,len(self.grid)):
for y in range(0,len(self.grid[x])):
self.getAdj(y,x)
if self.grid[x][y] == '*':
print "you bet"
if self.count == 2 or self.count == 3:
self.grid[x][y] = '*'
if self.count < 2 or self.count > 3:
self.grid[x][y] = '-'
elif self.grid[x][y] == '-' or self.grid[x][y] == ['-']:
print "nada"
if self.count == 3:
self.grid[x][y] == '*'
else:
print "uhhh"
def placeLivingCellsRandomly(self,living):
self.living = living
for i in range(0,self.living):
self.grid[random.randint(0,self.rows - 1)][random.randint(0,self.columns - 1)] = '*'
columns = int(raw_input("columns: "))
rows = int(raw_input("rows: "))
a = gameOfLife(columns,rows)
a.placeLivingCellsRandomly(living = 4)
a.printGrid()
a.nextStep()
a.printGrid()
a.nextStep()
a.printGrid()
The goal of the project is to make a class that will basically play the Game of Life.
Live cells are *, dead are - .
My getAdj is supposed to determine the number of live cells touching it, whether horizontally, vertically, or diagonally. It works sometimes but a lot of times the count it is reading is incorrect and I cant figure out why.