Skip to content
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

Add Game of Life exercise #2793

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -1791,6 +1791,17 @@
"lists"
],
"difficulty": 10
},
{
"slug": "game-of-life",
"name": "Conway's Game of Life",
"uuid": "a5f8aef3-9661-49c7-9eb3",
"practices": ["multi-dimensional-arrays"],
akbatra567 marked this conversation as resolved.
Show resolved Hide resolved
"prerequisites": [
"multi-dimensional-arrays",
akbatra567 marked this conversation as resolved.
Show resolved Hide resolved
"if-statements"
],
"difficulty": 5
}
],
"foregone": [
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
20 changes: 20 additions & 0 deletions exercises/practice/game-of-life/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"authors": [
"akbatra567"
],
"files": {
"solution": [
"src/main/java/GameOfLife.java"
],
"test": [
"src/test/java/GameOfLifeTest.java"
],
"example": [".meta/src/reference/java/GameOfLife.java"],
akbatra567 marked this conversation as resolved.
Show resolved Hide resolved
"invalidator": [
"build.gradle"
]
},
"blurb": "Implement Conway's Game of Life.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class GameOfLife {
public int[][] tick(int[][] matrix) {
if (matrix.length == 0) {
return matrix;
}
int rowCount = matrix.length;
int columnCount = matrix[0].length;
int[][] resultMatrix = new int[rowCount][columnCount];

for (int row = 0; row < rowCount; row++) {
for (int column = 0; column < columnCount; column++) {
int liveNeighbors = countLiveNeighbors(matrix, row, column);

if ((matrix[row][column] == 1 && (liveNeighbors == 2 || liveNeighbors == 3)) ||
(matrix[row][column] == 0 && liveNeighbors == 3)) {
resultMatrix[row][column] = 1;
}
}
}
return resultMatrix;
}

private int countLiveNeighbors(int[][] matrix, int row, int col) {
int rowCount = matrix.length;
int columnCount = matrix[0].length;
int count = 0;

for (int i = Math.max(0, row - 1); i <= Math.min(row + 1, rowCount - 1); i++) {
for (int j = Math.max(0, col - 1); j <= Math.min(col + 1, columnCount - 1); j++) {
if (i != row || j != col) {
count += matrix[i][j];
}
}
}
return count;
}
}
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"
23 changes: 23 additions & 0 deletions exercises/practice/game-of-life/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
plugins {
id "java"
}

repositories {
mavenCentral()
}

dependencies {
testImplementation platform("org.junit:junit-bom:5.10.0")
testImplementation "org.junit.jupiter:junit-jupiter"
testImplementation "org.assertj:assertj-core:3.25.1"
}

test {
useJUnitPlatform()

testLogging {
exceptionFormat = "full"
showStandardStreams = true
events = ["passed", "failed", "skipped"]
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
sanderploegsma marked this conversation as resolved.
Show resolved Hide resolved
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading
Loading