Testdriven Development (TDD) example of Conway's game of life in Java.
Rules of Conway's life:
- Any live cell with fewer than two live neighbours dies, as if caused by under-population.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overcrowding.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
The field is represented as single string using a space as newline seperator. The fieldsize is fixed (can not grow).
Using the TTD approach as advocated by Robert C. Martin (aka. uncle Bob) the main algorithm comes down to:
public boolean cellWillLive(String[] lines, int x, int y) {
int neighbours = numberOfLivingNeighbours(lines, x, y);
if (cellIsAlive(lines[y], x))
return neighbours > 2 && neighbours < 5;
else
return neighbours == 3;
}