Skip to content

Commit db7f626

Browse files
committed
Migrate doctest for ConwaysGameOfLife.js
(remove the animate code, too)
1 parent 6b820f3 commit db7f626

File tree

2 files changed

+12
-53
lines changed

2 files changed

+12
-53
lines changed

Cellular-Automata/ConwaysGameOfLife.js

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,10 @@ The Game of Life is a cellular automaton devised by the British mathematician Jo
88
(example adapted from https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/conways_game_of_life.py )
99
*/
1010

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) {
11+
/**
12+
* Generates the next generation for a given state of Conway's Game of Life.
13+
*/
14+
export function newGeneration (cells) {
2215
const nextGeneration = []
2316
for (let i = 0; i < cells.length; i++) {
2417
const nextGenerationRow = []
@@ -46,45 +39,3 @@ function newGeneration (cells) {
4639
}
4740
return nextGeneration
4841
}
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)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { newGeneration } from './ConwaysGameOfLife'
2+
3+
describe('newGeneration', () => {
4+
it('should produce the next generation according to the rules', () => {
5+
expect(newGeneration([[0, 1, 0], [0, 1, 0], [0, 1, 0]]))
6+
.toEqual([[0, 0, 0], [1, 1, 1], [0, 0, 0]])
7+
})
8+
})

0 commit comments

Comments
 (0)