Skip to content

Add game-of-life #2620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2656,6 +2656,17 @@
"lists"
],
"difficulty": 5
},
{
"slug": "game-of-life",
"name": "Conway's Game of Life",
"uuid": "e51c01e9-b7b1-4877-939a-6254c4efe338",
"practices": [
"array-loops",
"array-transformations"
],
"prerequisites": [],
"difficulty": 2
}
]
},
Expand Down
11 changes: 11 additions & 0 deletions exercises/practice/game-of-life/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Instructions

After each generation, the cells interact with their eight neighbors, which are cells adjacent horizontally, vertically, or diagonally.

The following rules are applied to each cell:

- Any live cell with two or three live neighbors lives on.
- Any dead cell with exactly three live neighbors becomes a live cell.
- All other cells die or stay dead.

Given a matrix of 1s and 0s (corresponding to live and dead cells), apply the rules to each cell, and return the next generation.
9 changes: 9 additions & 0 deletions exercises/practice/game-of-life/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Introduction

[Conway's Game of Life][game-of-life] is a fascinating cellular automaton created by the British mathematician John Horton Conway in 1970.

The game consists of a two-dimensional grid of cells that can either be "alive" or "dead."

After each generation, the cells interact with their eight neighbors via a set of rules, which define the new generation.

[game-of-life]: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
5 changes: 5 additions & 0 deletions exercises/practice/game-of-life/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
/bin/configlet
/bin/configlet.exe
/package-lock.json
/yarn.lock
19 changes: 19 additions & 0 deletions exercises/practice/game-of-life/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"game-of-life.js"
],
"test": [
"game-of-life.spec.js"
],
"example": [
".meta/proof.ci.js"
]
},
"blurb": "Implement Conway's Game of Life.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"
}
52 changes: 52 additions & 0 deletions exercises/practice/game-of-life/.meta/proof.ci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export class GameOfLife {
#matrix;

constructor(matrix) {
this.#matrix = matrix;
}

tick() {
if (this.#matrix.length === 0) {
return;
}
const rows = this.#matrix.length;
const cols = this.#matrix[0].length;

const newMatrix = JSON.parse(JSON.stringify(this.#matrix));

for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
let liveNeighbors = 0;
for (let newRow = row - 1; newRow <= row + 1; newRow++) {
for (let newCol = col - 1; newCol <= col + 1; newCol++) {
if (newRow === row && newCol === col) {
continue;
}
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols) {
liveNeighbors += this.#matrix[newRow][newCol];
}
}
}

var cell = this.#matrix[row][col];
if (cell === 1) {
if (liveNeighbors < 2 || liveNeighbors > 3) {
cell = 0;
}
} else {
if (liveNeighbors === 3) {
cell = 1;
}
}

newMatrix[row][col] = cell;
}
}

this.#matrix = newMatrix;
}

state() {
return this.#matrix;
}
}
34 changes: 34 additions & 0 deletions exercises/practice/game-of-life/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[ae86ea7d-bd07-4357-90b3-ac7d256bd5c5]
description = "empty matrix"

[4ea5ccb7-7b73-4281-954a-bed1b0f139a5]
description = "live cells with zero live neighbors die"

[df245adc-14ff-4f9c-b2ae-f465ef5321b2]
description = "live cells with only one live neighbor die"

[2a713b56-283c-48c8-adae-1d21306c80ae]
description = "live cells with two live neighbors stay alive"

[86d5c5a5-ab7b-41a1-8907-c9b3fc5e9dae]
description = "live cells with three live neighbors stay alive"

[015f60ac-39d8-4c6c-8328-57f334fc9f89]
description = "dead cells with three live neighbors become alive"

[2ee69c00-9d41-4b8b-89da-5832e735ccf1]
description = "live cells with four or more neighbors die"

[a79b42be-ed6c-4e27-9206-43da08697ef6]
description = "bigger matrix"
1 change: 1 addition & 0 deletions exercises/practice/game-of-life/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
21 changes: 21 additions & 0 deletions exercises/practice/game-of-life/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions exercises/practice/game-of-life/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
presets: [['@exercism/babel-preset-javascript', { corejs: '3.40' }]],
plugins: [],
};
45 changes: 45 additions & 0 deletions exercises/practice/game-of-life/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @ts-check

import config from '@exercism/eslint-config-javascript';
import maintainersConfig from '@exercism/eslint-config-javascript/maintainers.mjs';

import globals from 'globals';

export default [
...config,
...maintainersConfig,
{
files: maintainersConfig[1].files,
rules: {
'jest/expect-expect': ['warn', { assertFunctionNames: ['expect*'] }],
},
},
{
files: ['scripts/**/*.mjs'],
languageOptions: {
globals: {
...globals.node,
},
},
},
// <<inject-rules-here>>
{
ignores: [
// # Protected or generated
'/.appends/**/*',
'/.github/**/*',
'/.vscode/**/*',

// # Binaries
'/bin/*',

// # Configuration
'/config',
'/babel.config.js',

// # Typings
'/exercises/**/global.d.ts',
'/exercises/**/env.d.ts',
],
},
];
18 changes: 18 additions & 0 deletions exercises/practice/game-of-life/game-of-life.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// This is only a SKELETON file for the 'Conway's Game of Life' exercise. It's been provided
// as a convenience to get you started writing code faster.
//

export class GameOfLife {
constructor() {
throw new Error('Remove this statement and implement this function');
}

tick() {
throw new Error('Remove this statement and implement this function');
}

state() {
throw new Error('Remove this statement and implement this function');
}
}
142 changes: 142 additions & 0 deletions exercises/practice/game-of-life/game-of-life.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { describe, expect, test, xtest } from '@jest/globals';
import { GameOfLife } from './game-of-life';

describe('Game of Life', () => {
// Empty matrix
test('empty matrix', () => {
const matrix = [];
const game = new GameOfLife(matrix);
game.tick();
const expected = [];
expect(game.state()).toEqual(expected);
});

// Live cells with zero live neighbors die
xtest('live cells with zero live neighbors die', () => {
const matrix = [
[0, 0, 0],
[0, 1, 0],
[0, 0, 0],
];
const game = new GameOfLife(matrix);
game.tick();
const expected = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
];
expect(game.state()).toEqual(expected);
});

// Live cells with only one live neighbor die
xtest('live cells with only one live neighbor die', () => {
const matrix = [
[0, 0, 0],
[0, 1, 0],
[0, 1, 0],
];
const game = new GameOfLife(matrix);
game.tick();
const expected = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
];
expect(game.state()).toEqual(expected);
});

// Live cells with two live neighbors stay alive
xtest('live cells with two live neighbors stay alive', () => {
const matrix = [
[1, 0, 1],
[1, 0, 1],
[1, 0, 1],
];
const game = new GameOfLife(matrix);
game.tick();
const expected = [
[0, 0, 0],
[1, 0, 1],
[0, 0, 0],
];
expect(game.state()).toEqual(expected);
});

// Live cells with three live neighbors stay alive
xtest('live cells with three live neighbors stay alive', () => {
const matrix = [
[0, 1, 0],
[1, 0, 0],
[1, 1, 0],
];
const game = new GameOfLife(matrix);
game.tick();
const expected = [
[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
];
expect(game.state()).toEqual(expected);
});

// Dead cells with three live neighbors become alive
xtest('dead cells with three live neighbors become alive', () => {
const matrix = [
[1, 1, 0],
[0, 0, 0],
[1, 0, 0],
];
const game = new GameOfLife(matrix);
game.tick();
const expected = [
[0, 0, 0],
[1, 1, 0],
[0, 0, 0],
];
expect(game.state()).toEqual(expected);
});

// Live cells with four or more neighbors die
xtest('live cells with four or more neighbors die', () => {
const matrix = [
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
];
const game = new GameOfLife(matrix);
game.tick();
const expected = [
[1, 0, 1],
[0, 0, 0],
[1, 0, 1],
];
expect(game.state()).toEqual(expected);
});

// Bigger matrix
xtest('bigger matrix', () => {
const matrix = [
[1, 1, 0, 1, 1, 0, 0, 0],
[1, 0, 1, 1, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 0],
[1, 0, 0, 0, 1, 1, 0, 0],
[1, 1, 0, 0, 0, 1, 1, 1],
[0, 0, 1, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 1],
];
const game = new GameOfLife(matrix);
game.tick();
const expected = [
[1, 1, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0],
[1, 0, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 0, 0, 1, 0, 0, 1],
[1, 1, 0, 1, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1],
];
expect(game.state()).toEqual(expected);
});
});
Loading