Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cangencer committed Sep 29, 2014
1 parent 36b1cfc commit 4e0cc2f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
58 changes: 29 additions & 29 deletions gol.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
import os

Coords = namedtuple('Coords', ['x', 'y'])
World = namedtuple('World', ['size_x', 'size_y', 'state'])

def get_world(size_x,size_y, seed=[]):
def get_world(size_x,size_y, seeds=[]):
world = [[False for y in xrange(size_y)] for x in xrange(size_x)]
for c in seed:
for c in seeds:
world[c[0]][c[1]] = True
return world
return World(size_x, size_y, state)

def print_world(world):
for row in world:
for row in world.state:
for c in row:
print('#', end='') if c else print('.', end='')
print('')
Expand All @@ -29,52 +30,51 @@ def parse_world(rows):
for j, c in enumerate(row):
if c == '#':
seed.append((i,j))
return size_x, size_y, get_world(size_x, size_y, seed)
return get_world(size_x, size_y, seed)

def alive_neighbor_count(c, world) :
neighbors = []
top_row = world[max(c.x-1,0):c.x] # top row
bottom_row = world[c.x+1:c.x+2] # bottom row
def get_first_or_default(list, default=None):
for i in list:
return i
return default

def alive_neighbor_count(c, world):
''' Determine neighbor count of the given cell '''

top = get_first_or_default(world[max(c.x-1,0):c.x],[]) # top row
bottom = get_first_or_default(world[c.x+1:c.x+2],[]) # bottom row
left = world[c.x][c.y-1:c.y] # left
right = world[c.x][c.y+1:c.y+2] # right
top = top_row[0][max(0,c.y-1):c.y+2] if top_row else []
bottom = bottom_row[0][max(0,c.y-1):c.y+2] if bottom_row else []
right = world[c.x][c.y+1:c.y+2] # right
return sum(left + right + top + bottom)

def simulate(size_x, size_y, world):
next = get_world(size_x, size_y)
def simulate(world):
''' Play one round of game of life and return the new state '''

next = get_world(world.size_x, world.size_y)
for x, row in enumerate(world):
for y, c in enumerate(row):
count = alive_neighbor_count(Coords(x,y), world)
# print (x,y, world[x][y], count)
if world[x][y]:
count = alive_neighbor_count(Coords(x,y), world)
if world.state[x][y]:
if (count == 2) or (count == 3):
next[x][y] = True
next.state[x][y] = True
else:
if count == 3:
next[x][y] = True
next.state[x][y] = True
return next

if __name__ == '__main__':
if len(sys.argv) != 3:
print('Usage: %s SEED_FILE NO_GENERATIONS' % sys.argv[0])
exit(0)

no_rounds = int(sys.argv[2])
seed = []
size_x = 0
size_y = 0
no_rounds = int(sys.argv[2])
current = None
with open(sys.argv[1], 'r') as f:
rows = [row.strip() for row in f]
size_x, size_y, current = parse_world(rows)
current = parse_world(rows)

for i in xrange(no_rounds):
os.system('clear')
print("Round %s" % i)
print_world(current)
current = simulate(size_x, size_y, current)
sleep(0.1)



current = simulate(current)
sleep(0.1)
2 changes: 1 addition & 1 deletion test_gol.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class GameOfLifeTests(unittest.TestCase):
def assert_neighbors(self, str, p, expected):
rows = [s.strip() for s in str.split('\n')]
x,y,world = parse_world(rows)
world = parse_world(rows)
#print_world(world)
count = alive_neighbor_count(Coords(p[0],p[1]), world)
self.assertEqual(expected, count)
Expand Down

0 comments on commit 4e0cc2f

Please sign in to comment.