-
-
Notifications
You must be signed in to change notification settings - Fork 683
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Game of Life exercise * resolve PR reviews * fix lint markdown issue
- Loading branch information
1 parent
4b52644
commit f01c396
Showing
18 changed files
with
664 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
37 changes: 37 additions & 0 deletions
37
exercises/practice/game-of-life/.meta/src/reference/java/GameOfLife.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
6 changes: 6 additions & 0 deletions
6
exercises/practice/game-of-life/gradle/wrapper/gradle-wrapper.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.