Skip to content

Commit c8ae9b1

Browse files
authored
Add game-of-life (#2620)
* Add `game-of-life` * Format exercise config * Fix copy and paste error with first test * Add practices
1 parent bac02cf commit c8ae9b1

File tree

16 files changed

+7584
-0
lines changed

16 files changed

+7584
-0
lines changed

config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,6 +2656,17 @@
26562656
"lists"
26572657
],
26582658
"difficulty": 5
2659+
},
2660+
{
2661+
"slug": "game-of-life",
2662+
"name": "Conway's Game of Life",
2663+
"uuid": "e51c01e9-b7b1-4877-939a-6254c4efe338",
2664+
"practices": [
2665+
"array-loops",
2666+
"array-transformations"
2667+
],
2668+
"prerequisites": [],
2669+
"difficulty": 2
26592670
}
26602671
]
26612672
},
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Instructions
2+
3+
After each generation, the cells interact with their eight neighbors, which are cells adjacent horizontally, vertically, or diagonally.
4+
5+
The following rules are applied to each cell:
6+
7+
- Any live cell with two or three live neighbors lives on.
8+
- Any dead cell with exactly three live neighbors becomes a live cell.
9+
- All other cells die or stay dead.
10+
11+
Given a matrix of 1s and 0s (corresponding to live and dead cells), apply the rules to each cell, and return the next generation.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Introduction
2+
3+
[Conway's Game of Life][game-of-life] is a fascinating cellular automaton created by the British mathematician John Horton Conway in 1970.
4+
5+
The game consists of a two-dimensional grid of cells that can either be "alive" or "dead."
6+
7+
After each generation, the cells interact with their eight neighbors via a set of rules, which define the new generation.
8+
9+
[game-of-life]: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/node_modules
2+
/bin/configlet
3+
/bin/configlet.exe
4+
/package-lock.json
5+
/yarn.lock
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"game-of-life.js"
8+
],
9+
"test": [
10+
"game-of-life.spec.js"
11+
],
12+
"example": [
13+
".meta/proof.ci.js"
14+
]
15+
},
16+
"blurb": "Implement Conway's Game of Life.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"
19+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
export class GameOfLife {
2+
#matrix;
3+
4+
constructor(matrix) {
5+
this.#matrix = matrix;
6+
}
7+
8+
tick() {
9+
if (this.#matrix.length === 0) {
10+
return;
11+
}
12+
const rows = this.#matrix.length;
13+
const cols = this.#matrix[0].length;
14+
15+
const newMatrix = JSON.parse(JSON.stringify(this.#matrix));
16+
17+
for (let row = 0; row < rows; row++) {
18+
for (let col = 0; col < cols; col++) {
19+
let liveNeighbors = 0;
20+
for (let newRow = row - 1; newRow <= row + 1; newRow++) {
21+
for (let newCol = col - 1; newCol <= col + 1; newCol++) {
22+
if (newRow === row && newCol === col) {
23+
continue;
24+
}
25+
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols) {
26+
liveNeighbors += this.#matrix[newRow][newCol];
27+
}
28+
}
29+
}
30+
31+
var cell = this.#matrix[row][col];
32+
if (cell === 1) {
33+
if (liveNeighbors < 2 || liveNeighbors > 3) {
34+
cell = 0;
35+
}
36+
} else {
37+
if (liveNeighbors === 3) {
38+
cell = 1;
39+
}
40+
}
41+
42+
newMatrix[row][col] = cell;
43+
}
44+
}
45+
46+
this.#matrix = newMatrix;
47+
}
48+
49+
state() {
50+
return this.#matrix;
51+
}
52+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[ae86ea7d-bd07-4357-90b3-ac7d256bd5c5]
13+
description = "empty matrix"
14+
15+
[4ea5ccb7-7b73-4281-954a-bed1b0f139a5]
16+
description = "live cells with zero live neighbors die"
17+
18+
[df245adc-14ff-4f9c-b2ae-f465ef5321b2]
19+
description = "live cells with only one live neighbor die"
20+
21+
[2a713b56-283c-48c8-adae-1d21306c80ae]
22+
description = "live cells with two live neighbors stay alive"
23+
24+
[86d5c5a5-ab7b-41a1-8907-c9b3fc5e9dae]
25+
description = "live cells with three live neighbors stay alive"
26+
27+
[015f60ac-39d8-4c6c-8328-57f334fc9f89]
28+
description = "dead cells with three live neighbors become alive"
29+
30+
[2ee69c00-9d41-4b8b-89da-5832e735ccf1]
31+
description = "live cells with four or more neighbors die"
32+
33+
[a79b42be-ed6c-4e27-9206-43da08697ef6]
34+
description = "bigger matrix"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
audit=false
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
presets: [['@exercism/babel-preset-javascript', { corejs: '3.40' }]],
3+
plugins: [],
4+
};

0 commit comments

Comments
 (0)