Skip to content

Commit f01c396

Browse files
authored
Add Game of Life exercise (#2793)
* Add Game of Life exercise * resolve PR reviews * fix lint markdown issue
1 parent 4b52644 commit f01c396

File tree

18 files changed

+664
-1
lines changed

18 files changed

+664
-1
lines changed

CONTRIBUTING.md

+4
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ Each problem/submodule has three source sets:
207207
- `.meta/src/reference/java/` — a reference solution that passes all the tests
208208
- `src/main/java/` — starter source file(s).
209209

210+
### Update/sync Gradle versions
211+
212+
Please read [How to Update Gradle](../reference/how-to-update-gradle.md)
213+
210214
## Contributing to Concept Exercises
211215

212216
Please read [Implementing a Concept Exercise](reference/implementing-a-concept-exercise.md).

config.json

+11
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,17 @@
17911791
"lists"
17921792
],
17931793
"difficulty": 10
1794+
},
1795+
{
1796+
"slug": "game-of-life",
1797+
"name": "Conway's Game of Life",
1798+
"uuid": "749de7fc-3dcb-4231-9b4f-115d153af74f",
1799+
"practices": [],
1800+
"prerequisites": [
1801+
"arrays",
1802+
"if-statements"
1803+
],
1804+
"difficulty": 5
17941805
}
17951806
],
17961807
"foregone": [
-9 Bytes
Binary file not shown.
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.
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"authors": [
3+
"akbatra567"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/main/java/GameOfLife.java"
8+
],
9+
"test": [
10+
"src/test/java/GameOfLifeTest.java"
11+
],
12+
"example": [
13+
".meta/src/reference/java/GameOfLife.java"
14+
],
15+
"invalidator": [
16+
"build.gradle"
17+
]
18+
},
19+
"blurb": "Implement Conway's Game of Life.",
20+
"source": "Wikipedia",
21+
"source_url": "https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class GameOfLife {
2+
public int[][] tick(int[][] matrix) {
3+
if (matrix.length == 0) {
4+
return matrix;
5+
}
6+
int rowCount = matrix.length;
7+
int columnCount = matrix[0].length;
8+
int[][] resultMatrix = new int[rowCount][columnCount];
9+
10+
for (int row = 0; row < rowCount; row++) {
11+
for (int column = 0; column < columnCount; column++) {
12+
int liveNeighbors = countLiveNeighbors(matrix, row, column);
13+
14+
if ((matrix[row][column] == 1 && (liveNeighbors == 2 || liveNeighbors == 3)) ||
15+
(matrix[row][column] == 0 && liveNeighbors == 3)) {
16+
resultMatrix[row][column] = 1;
17+
}
18+
}
19+
}
20+
return resultMatrix;
21+
}
22+
23+
private int countLiveNeighbors(int[][] matrix, int row, int col) {
24+
int rowCount = matrix.length;
25+
int columnCount = matrix[0].length;
26+
int count = 0;
27+
28+
for (int i = Math.max(0, row - 1); i <= Math.min(row + 1, rowCount - 1); i++) {
29+
for (int j = Math.max(0, col - 1); j <= Math.min(col + 1, columnCount - 1); j++) {
30+
if (i != row || j != col) {
31+
count += matrix[i][j];
32+
}
33+
}
34+
}
35+
return count;
36+
}
37+
}
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"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
plugins {
2+
id "java"
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testImplementation platform("org.junit:junit-bom:5.10.0")
11+
testImplementation "org.junit.jupiter:junit-jupiter"
12+
testImplementation "org.assertj:assertj-core:3.25.1"
13+
}
14+
15+
test {
16+
useJUnitPlatform()
17+
18+
testLogging {
19+
exceptionFormat = "full"
20+
showStandardStreams = true
21+
events = ["passed", "failed", "skipped"]
22+
}
23+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
4+
validateDistributionUrl=true
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)