Skip to content

Commit 9fddc0b

Browse files
authored
Merge pull request TheAlgorithms#570 from algobytewise/ConwaysGameOfLife
Added Conways game of life
2 parents 33549a3 + a9e12e3 commit 9fddc0b

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Conway's Game of Life
3+
The Game of Life is a cellular automaton devised by the British mathematician John Horton Conway in 1970. 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, (or populated and unpopulated, respectively). Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
4+
1. Any live cell with two or three live neighbours survives.
5+
2. Any dead cell with three live neighbours becomes a live cell.
6+
3. All other live cells die in the next generation. Similarly, all other dead cells stay dead.
7+
(description adapted from https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life )
8+
(example adapted from https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/conways_game_of_life.py )
9+
*/
10+
11+
/*
12+
* Doctests
13+
*
14+
* > newGeneration([[0, 1, 0], [0, 1, 0], [0, 1, 0]])
15+
* [ [ 0, 0, 0 ], [ 1, 1, 1 ], [ 0, 0, 0 ] ]
16+
*/
17+
18+
/*
19+
* Generates the next generation for a given state of Conway's Game of Life.
20+
*/
21+
function newGeneration (cells) {
22+
const nextGeneration = []
23+
for (let i = 0; i < cells.length; i++) {
24+
const nextGenerationRow = []
25+
for (let j = 0; j < cells[i].length; j++) {
26+
// Get the number of living neighbours
27+
let neighbourCount = 0
28+
if (i > 0 && j > 0) neighbourCount += cells[i - 1][j - 1]
29+
if (i > 0) neighbourCount += cells[i - 1][j]
30+
if (i > 0 && j < cells[i].length - 1) neighbourCount += cells[i - 1][j + 1]
31+
if (j > 0) neighbourCount += cells[i][j - 1]
32+
if (j < cells[i].length - 1) neighbourCount += cells[i][j + 1]
33+
if (i < cells.length - 1 && j > 0) neighbourCount += cells[i + 1][j - 1]
34+
if (i < cells.length - 1) neighbourCount += cells[i + 1][j]
35+
if (i < cells.length - 1 && j < cells[i].length - 1) neighbourCount += cells[i + 1][j + 1]
36+
37+
// Decide whether the cell is alive or dead
38+
const alive = cells[i][j] === 1
39+
if ((alive && neighbourCount >= 2 && neighbourCount <= 3) || (!alive && neighbourCount === 3)) {
40+
nextGenerationRow.push(1)
41+
} else {
42+
nextGenerationRow.push(0)
43+
}
44+
}
45+
nextGeneration.push(nextGenerationRow)
46+
}
47+
return nextGeneration
48+
}
49+
50+
/*
51+
* utility function to display a series of generations in the console
52+
*/
53+
async function animate (cells, steps) {
54+
/*
55+
* utility function to print one frame
56+
*/
57+
function printCells (cells) {
58+
console.clear()
59+
for (let i = 0; i < cells.length; i++) {
60+
let line = ''
61+
for (let j = 0; j < cells[i].length; j++) {
62+
if (cells[i][j] === 1) line += '\u2022'
63+
else line += ' '
64+
}
65+
console.log(line)
66+
}
67+
}
68+
69+
printCells(cells)
70+
71+
for (let i = 0; i < steps; i++) {
72+
await new Promise(resolve => setTimeout(resolve, 250)) // sleep
73+
cells = newGeneration(cells)
74+
printCells(cells)
75+
}
76+
}
77+
78+
// Define glider example
79+
const glider = [
80+
[0, 1, 0, 0, 0, 0, 0, 0],
81+
[0, 0, 1, 0, 0, 0, 0, 0],
82+
[1, 1, 1, 0, 0, 0, 0, 0],
83+
[0, 0, 0, 0, 0, 0, 0, 0],
84+
[0, 0, 0, 0, 0, 0, 0, 0],
85+
[0, 0, 0, 0, 0, 0, 0, 0],
86+
[0, 0, 0, 0, 0, 0, 0, 0],
87+
[0, 0, 0, 0, 0, 0, 0, 0]
88+
]
89+
90+
animate(glider, 16)

0 commit comments

Comments
 (0)