Your task is to implement the rules of Conway's Game of Life as explained in the document statement. You can use the code skeleton provided here as a starting point. In that case, implement the evolve
method in Life.py
. Feel free to make changes to the code that help you capture all the rules of the game. You can add more tests to testlife.py
to verify the correctness of your code.
- Your favorite text editor
- Install nosetests
In a command line, navigate to this directory and just run nosetests -v
From the supplied specification, I assumed a TDD approach.
Making an assumption from the provided test case, I decided to use a 3x3 board size approach.
STATE = [[0,0,0],
[0,0,0],
[0,0,0]]
I then implemented the logic using the 4 universal Game of Life rules:
1) Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
2) Any live cell with two or three live neighbours lives on to the next generation.
3) Any live cell with more than three live neighbours dies, as if by overpopulation.
4) Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
I then added test cases for all further scenarios provided.
I then modified the code to make use of the Class based approach, initialising each Life instance with the STATE and SIZE of the board.
STATE must be the same size as the SIZE paramter or the simulation will not run.
Using IPython:
from Life import Life
SIZE = 3
state = [[0 for r in range(SIZE)] for c in range(SIZE)]
life = Life(state=state, size=SIZE)
life.visual_evolve(2) #pass in the number of steps