patternpythonMinor
General feedback on Conway's Game of Life implementation
Viewed 0 times
conwaygeneralgamelifeimplementationfeedback
Problem
I've just written an implementation of Conway's Game of Life as an exercise and I'd love to get some feedback on how I can improve this program and hopefully my style in general.
I've tested the code in Python 2.7.3. It requires the PyPNG module to render the state of the simulation.
```
"""Simulate Conway's Game of Life.
The world is finite. Cells at its border always stay dead.
The frames of the simulation are saved as .png files.
"""
from numpy.random import random_integers
from png import from_array
DEAD = 1
ALIVE = 0
def saveAsImage(array, number):
"""Save the array to a .png file.
The files are saved as 1-bit greyscale (black/white).
"""
image = from_array(array, 'L', {'bitdepth': 1})
image.save('image%03i.png' % number)
def simulate(world, simulationLength):
"""Simulate a certain number from steps starting from a given state.
The frames are saved as .png images in the current working directory.
Parameters:
world: The initial state of the world as a two-dimensional numpy
array in which world[y][x] describes the state of the
cell at position x, y
simulationLength: The number of simulated steps,
simulationLength + 1 images are created.
"""
# Retrieve the dimensions of the world
worldHeight = len(world)
worldWidth = len(world[0])
# Make sure there are no living cells on the border.
world[0] = DEAD # top
world[worldHeight - 1] = DEAD # bottom
for y in xrange(1, worldHeight - 2):
world[y][0] = DEAD # left
world[y][worldWidth - 1] = DEAD # right
saveAsImage(world, 0)
for frame in xrange(1, simulationLength + 1):
# Simulate simulationLength frames
nextWorld = world.copy()
for y in xrange(1, worldHeight - 1):
for x in xrange(1, worldWidth - 1):
# Iterate over all cells within the border
neighbors = [world[y - 1][x - 1],
I've tested the code in Python 2.7.3. It requires the PyPNG module to render the state of the simulation.
```
"""Simulate Conway's Game of Life.
The world is finite. Cells at its border always stay dead.
The frames of the simulation are saved as .png files.
"""
from numpy.random import random_integers
from png import from_array
DEAD = 1
ALIVE = 0
def saveAsImage(array, number):
"""Save the array to a .png file.
The files are saved as 1-bit greyscale (black/white).
"""
image = from_array(array, 'L', {'bitdepth': 1})
image.save('image%03i.png' % number)
def simulate(world, simulationLength):
"""Simulate a certain number from steps starting from a given state.
The frames are saved as .png images in the current working directory.
Parameters:
world: The initial state of the world as a two-dimensional numpy
array in which world[y][x] describes the state of the
cell at position x, y
simulationLength: The number of simulated steps,
simulationLength + 1 images are created.
"""
# Retrieve the dimensions of the world
worldHeight = len(world)
worldWidth = len(world[0])
# Make sure there are no living cells on the border.
world[0] = DEAD # top
world[worldHeight - 1] = DEAD # bottom
for y in xrange(1, worldHeight - 2):
world[y][0] = DEAD # left
world[y][worldWidth - 1] = DEAD # right
saveAsImage(world, 0)
for frame in xrange(1, simulationLength + 1):
# Simulate simulationLength frames
nextWorld = world.copy()
for y in xrange(1, worldHeight - 1):
for x in xrange(1, worldWidth - 1):
# Iterate over all cells within the border
neighbors = [world[y - 1][x - 1],
Solution
Separate different concerns in your code. The function
Stick to the PEP8 naming conventions: functions and variables should be written
simulate does two things that should be separated, namely changing the state of the world and calling the function that saves it as an image. As it is, it's impossible to re-use only the state-changing logic. Instead, move the game logic to a function simulate_step (or whatever you want to call it).Stick to the PEP8 naming conventions: functions and variables should be written
lowercase_with_underscores. Otherwise, you follow the guide very nicely.Context
StackExchange Code Review Q#29314, answer score: 2
Revisions (0)
No revisions yet.