Skip to content

Commit

Permalink
Add Game of Life exercise (#2793)
Browse files Browse the repository at this point in the history
* Add Game of Life exercise

* resolve PR reviews

* fix lint markdown issue
  • Loading branch information
akbatra567 authored May 7, 2024
1 parent 4b52644 commit f01c396
Show file tree
Hide file tree
Showing 18 changed files with 664 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ Each problem/submodule has three source sets:
- `.meta/src/reference/java/` — a reference solution that passes all the tests
- `src/main/java/` — starter source file(s).

### Update/sync Gradle versions

Please read [How to Update Gradle](../reference/how-to-update-gradle.md)

## Contributing to Concept Exercises

Please read [Implementing a Concept Exercise](reference/implementing-a-concept-exercise.md).
Expand Down
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": "749de7fc-3dcb-4231-9b4f-115d153af74f",
"practices": [],
"prerequisites": [
"arrays",
"if-statements"
],
"difficulty": 5
}
],
"foregone": [
Expand Down
Binary file modified exercises/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
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
22 changes: 22 additions & 0 deletions exercises/practice/game-of-life/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
"akbatra567"
],
"files": {
"solution": [
"src/main/java/GameOfLife.java"
],
"test": [
"src/test/java/GameOfLifeTest.java"
],
"example": [
".meta/src/reference/java/GameOfLife.java"
],
"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.7-bin.zip
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit f01c396

Please sign in to comment.