git clone https://github.com/sonmaximum/conways-game-of-life
cd conways-game-of-life/
bundle install
ruby lib/conway.rb
Note that the default demo is 40x40, so be sure the terminal window is large enough (ideally fullscreen) to see the whole gameboard.
During my time at General Assembly, for one of the training sessions we had a full day of "code retreat" where we practiced using different group coding methods and cooperating to try to solve a coding problem under various constraints. The problem we were given was to implement Conway's Game of Life. (See the final README section, below, for a detailed description of Conway's Game of Life, provided by General Assembly.)
I was actually familiar with Conway's Game of Life (from way back in Microsoft Entertainment Pack 3), so after the retreat was over, I wanted to try to complete the problem we had started during that day. This repository contains my follow-up, a simulation of Conway's Game of Life using Ruby.
The logic is found in file lib\conway.rb
. This file can be executed as-is to run a pre-defined demonstration in the terminal consisting of a 40x40 grid with one glider and one three-period pulsar. The glider will proceed across the board until it reaches the edge, while the pulsar will repeat indefinitely.
Alternatively, the data from the file can be loaded into a Ruby interpreter, and a new gameboard can be instantiated with an x and y board size (Gameboard.new(x, y)
). Then the make_alive
method of this gameboard can be given an X and Y coordinate for each initial living cell. Finally, the gameboard's start_game
method is used to run the simulation. See the demo code at the bottom of lib\conway.rb for the example.
The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.
Visualization of the Game of Life:
Interactive Conway's Game of Life
The "game" is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input. One interacts with the Game of Life by creating an initial configuration and observing how it evolves.
The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, live or dead. Every cell interacts with its eight neighbors, which are the cells that are directly horizontally, vertically, or diagonally adjacent.
At each step in time, the following transitions occur:
- Any live cell with fewer than two live neighbors dies, as if caused by underpopulation.
- Any live cell with more than three live neighbors dies, as if by overcrowding.
- Any live cell with two or three live neighbors lives on to the next generation.
- Any dead cell with exactly three live neighbors becomes a live cell.
The initial pattern constitutes the seed of the system. The first generation is
created by applying the above rules simultaneously to every cell in the seed.
Births and deaths happen simultaneously, and the discrete moment at which
this happens is sometimes called a tick
(in other words, each generation is a
pure function of the one before). The rules continue to be applied repeatedly to
create further generations.
Here is a visual representation of what is happening: