This is known as O(N²) computational time, meaning the number of calculations your program makes is the square of the number of cells in your array.
Also, creating cells gets tricky; since we aren’t making objects for empty cells, measuring the right conditions for a new cell isn’t trivial.
In boards with millions of cells, our poor, non-quantum computers will become sad.
Board as ObjectHere’s an option that allows you to only iterate over each cell once, and then each square of the board once.
The board is an object, storing an array of positions (cells), with two main methods: get_board_size, and check_board.
class Board@cells = [(x1, y1), (x2, y2), (x3, y3), .
]def get_board_size @cells.
each find min(x, y) and max(x, y)enddef check_board from board min(x, y) to board max(x, y) check_neighbors if neighbors create_cell / destroy_cellThis allows you to only go over each cell once, to get the size of the board.
Then you iterate over the whole board to create and destroy cells.
This puts you in the realm of O(N) computational time, meaning the number of calculations your program makes will increase linearly with the number of cells on your board.
ConclusionThere’s good reason Game of Life shows up a lot on lists of coding “katas” and exercises; the rules are simple, but the implementation can vary wildly.
The two examples I covered here are only two of the object-oriented possibilities — can you think of ways to make the game in a functional programming environment, for example?.There are solutions with finite boards; with hexagons instead of squares; or even with their grids on three-dimensional objects.
You can find my functional-ish Python solution below, which adds simple visualization and a small list of preset patterns.
Happy gaming!Raphaelaugusto [CC BY-SA 4.
0]More readingConway's Game of Life – LifeWikiThe popularity of Conway's Life was helped by its coming into being just in time for a new generation of inexpensive…www.
comCategory:Patterns – LifeWikiBreeders · Caber tossers · Conduits · Converters · Crawlers · Eaters · Fuses · Growing spaceships · Induction coils ·…www.
comGame Of Life Music Sequencervimeo]http://vimeo.
com/3328851[/vimeo] The Game of Life Sequencer Bank is based on a game of life sequencer screencast…www.
com256hz/Game-of-LifeCommand line Game of Life in Python.
Infinite board, ASCII representation.
comRecreating Game of Life in MinecraftA beginner's guide to Big O notationBig O notation is used in Computer Science to describe the performance or complexity of an algorithm.