Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b1ad456
Remove QuickSelect doctest
defaude Oct 5, 2021
4b1799e
Remove AverageMedian doctest
defaude Oct 5, 2021
852e16f
Migrate doctest for BinaryExponentiationRecursive.js
defaude Oct 5, 2021
25c29b6
Migrate doctest for EulersTotient.js
defaude Oct 5, 2021
7b8ec07
Migrate doctest for PrimeFactors.js
defaude Oct 5, 2021
e7836e4
Migrate doctest for BogoSort.js
defaude Oct 5, 2021
402ab5c
Migrate doctest for BeadSort.js
defaude Oct 5, 2021
3eff8fd
Migrate doctest for BucketSort.js
defaude Oct 5, 2021
9627cfb
Migrate doctest for CocktailShakerSort.js
defaude Oct 5, 2021
e0e3c19
Migrate doctest for MergeSort.js
defaude Oct 5, 2021
942f9fb
Migrate doctest for QuickSort.js
defaude Oct 5, 2021
3d48cbf
Migrate doctest for ReverseString.js
defaude Oct 5, 2021
dd619c9
Migrate doctest for ReverseString.js
defaude Oct 5, 2021
6b820f3
Migrate doctest for ValidateEmail.js
defaude Oct 5, 2021
db7f626
Migrate doctest for ConwaysGameOfLife.js
defaude Oct 5, 2021
f2aa40c
Remove TernarySearch doctest
defaude Oct 5, 2021
7f5d7fe
Migrate doctest for BubbleSort.js
defaude Oct 5, 2021
c3747e9
Remove doctest from CI and from dependencies
defaude Oct 5, 2021
8274f40
Migrate doctest for RgbHsvConversion.js
defaude Oct 6, 2021
a383356
Add --fix option to "standard" npm script
defaude Oct 6, 2021
bbeeefb
Migrate doctest for BreadthFirstSearch.js
defaude Oct 6, 2021
1244f3c
Migrate doctest for BreadthFirstShortestPath.js
defaude Oct 6, 2021
d019e85
Migrate doctest for EulerMethod.js
defaude Oct 6, 2021
bf68fdd
Migrate doctest for Mandelbrot.js
defaude Oct 6, 2021
cbece80
Migrate doctest for FloodFill.js
defaude Oct 6, 2021
59796a2
Migrate doctest for KochSnowflake.js
defaude Oct 6, 2021
a8ad3a3
Update npm lockfile
defaude Oct 6, 2021
78f5dc4
Update README and COMMITTING with a few bits & bobs regarding testing…
defaude Oct 6, 2021
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate doctest for ConwaysGameOfLife.js
(remove the animate code, too)
  • Loading branch information
defaude committed Oct 6, 2021
commit db7f62654f265ee282c240cda2353b22c48c36b0
57 changes: 4 additions & 53 deletions Cellular-Automata/ConwaysGameOfLife.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ The Game of Life is a cellular automaton devised by the British mathematician Jo
(example adapted from https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/conways_game_of_life.py )
*/

/*
* Doctests
*
* > newGeneration([[0, 1, 0], [0, 1, 0], [0, 1, 0]])
* [ [ 0, 0, 0 ], [ 1, 1, 1 ], [ 0, 0, 0 ] ]
*/

/*
* Generates the next generation for a given state of Conway's Game of Life.
*/
function newGeneration (cells) {
/**
* Generates the next generation for a given state of Conway's Game of Life.
*/
export function newGeneration (cells) {
const nextGeneration = []
for (let i = 0; i < cells.length; i++) {
const nextGenerationRow = []
Expand Down Expand Up @@ -46,45 +39,3 @@ function newGeneration (cells) {
}
return nextGeneration
}

/*
* utility function to display a series of generations in the console
*/
async function animate (cells, steps) {
/*
* utility function to print one frame
*/
function printCells (cells) {
console.clear()
for (let i = 0; i < cells.length; i++) {
let line = ''
for (let j = 0; j < cells[i].length; j++) {
if (cells[i][j] === 1) line += '\u2022'
else line += ' '
}
console.log(line)
}
}

printCells(cells)

for (let i = 0; i < steps; i++) {
await new Promise(resolve => setTimeout(resolve, 250)) // sleep
cells = newGeneration(cells)
printCells(cells)
}
}

// Define glider example
const glider = [
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
]

animate(glider, 16)
8 changes: 8 additions & 0 deletions Cellular-Automata/ConwaysGameOfLife.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { newGeneration } from './ConwaysGameOfLife'

describe('newGeneration', () => {
it('should produce the next generation according to the rules', () => {
expect(newGeneration([[0, 1, 0], [0, 1, 0], [0, 1, 0]]))
.toEqual([[0, 0, 0], [1, 1, 1], [0, 0, 0]])
})
})