-
-
Notifications
You must be signed in to change notification settings - Fork 151
Add game-of-life exercice
#1007
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ etl | |
| flatten-array | ||
| flower-field | ||
| food-chain | ||
| game-of-life | ||
| gigasecond | ||
| grade-school | ||
| grains | ||
|
|
||
| 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. |
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "authors": [ | ||
| "resu-xuniL" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "GameOfLife.php" | ||
| ], | ||
| "test": [ | ||
| "GameOfLifeTest.php" | ||
| ], | ||
| "example": [ | ||
| ".meta/example.php" | ||
| ] | ||
| }, | ||
| "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,53 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| const NEIGHBORS = [ | ||
| [-1, -1], [-1, 0], [-1, 1], | ||
| [ 0, -1], [ 0, 1], | ||
| [ 1, -1], [ 1, 0], [ 1, 1], | ||
| ]; | ||
|
|
||
| function tick(array $matrix): array | ||
| { | ||
| $newMatrix = []; | ||
|
|
||
| foreach ($matrix as $row => $values) { | ||
| foreach ($values as $col => $cell) { | ||
| $countLiveCell = checkNeighborhood($matrix, $row, $col); | ||
|
|
||
| if ($matrix[$row][$col] === 1 && ($countLiveCell === 2 || $countLiveCell === 3)) { | ||
| $newMatrix[$row][$col] = 1; | ||
| } elseif ($matrix[$row][$col] === 0 && $countLiveCell === 3) { | ||
| $newMatrix[$row][$col] = 1; | ||
| } else { | ||
| $newMatrix[$row][$col] = 0; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $newMatrix; | ||
| } | ||
|
|
||
| function checkNeighborhood(array $matrix, int $cellRow, int $cellCol): int | ||
| { | ||
| $count = 0; | ||
|
|
||
| foreach (NEIGHBORS as [$checkRow, $checkCol]) { | ||
| $neighbRow = $checkRow + $cellRow; | ||
| $neighbCol = $checkCol + $cellCol; | ||
|
|
||
| if ( | ||
| $neighbRow < 0 || | ||
| $neighbCol < 0 || | ||
| $neighbRow >= count($matrix) || | ||
| $neighbCol >= count($matrix[0]) | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| $count += $matrix[$neighbRow][$neighbCol]; | ||
| } | ||
|
|
||
| return $count; | ||
| } |
| 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" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * By adding type hints and enabling strict type checking, code can become | ||
| * easier to read, self-documenting and reduce the number of potential bugs. | ||
| * By default, type declarations are non-strict, which means they will attempt | ||
| * to change the original type to match the type specified by the | ||
| * type-declaration. | ||
| * | ||
| * In other words, if you pass a string to a function requiring a float, | ||
| * it will attempt to convert the string value to a float. | ||
| * | ||
| * To enable strict mode, a single declare directive must be placed at the top | ||
| * of the file. | ||
| * This means that the strictness of typing is configured on a per-file basis. | ||
| * This directive not only affects the type declarations of parameters, but also | ||
| * a function's return type. | ||
| * | ||
| * For more info review the Concept on strict type checking in the PHP track | ||
| * <link>. | ||
| * | ||
| * To disable strict typing, comment out the directive below. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| function tick(array $matrix): array | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once again, I would want another interface: the Game of Life is a state changing exercise using ticks to modify some world state. That's what objects are for. So please make it a class. To add some "extra spice", suggest to the student in a comment to use Asymmetric Property Visibility. The So the interface may look like this (with the usual exceptions): class GameOfLife
{
/**
* In PHP 8.4 and newer you can use Asymmetric Property Visibility to enhance data encapsulation
* @see https://www.php.net/manual/en/language.oop5.visibility.php#language.oop5.visibility-members-aviz
*/
public function __construct(public array $matrix);
public function tick(): void;
}And the tests could look like: $matrix = [
[0, 0, 0],
[0, 1, 0],
[0, 0, 0]
];
$expected = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
$subject = new GameOfLife($matrix);
$subject->tick();
$this->assertEquals($expected, $subject->matrix); |
||
| { | ||
| throw new \BadFunctionCallException("Implement the tick function"); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use PHPUnit\Framework\Attributes\TestDox; | ||
|
|
||
| class GameOfLifeTest extends TestCase | ||
| { | ||
| public static function setUpBeforeClass(): void | ||
| { | ||
| require_once 'GameOfLife.php'; | ||
| } | ||
|
|
||
| /** | ||
| * uuid ae86ea7d-bd07-4357-90b3-ac7d256bd5c5 | ||
| */ | ||
| #[TestDox('empty matrix')] | ||
| public function testEmptyMatrix(): void | ||
| { | ||
| $this->assertEquals([], tick([])); | ||
| } | ||
|
|
||
| /** | ||
| * uuid 4ea5ccb7-7b73-4281-954a-bed1b0f139a5 | ||
| */ | ||
| #[TestDox('live cells with zero live neighbors die')] | ||
| public function testLiveCellsWithZeroLiveNeighborsDie(): void | ||
| { | ||
| $this->assertEquals([ | ||
| [0, 0, 0], | ||
| [0, 0, 0], | ||
| [0, 0, 0] | ||
| ], tick([ | ||
| [0, 0, 0], | ||
| [0, 1, 0], | ||
| [0, 0, 0] | ||
| ])); | ||
| } | ||
|
|
||
| /** | ||
| * uuid df245adc-14ff-4f9c-b2ae-f465ef5321b2 | ||
| */ | ||
| #[TestDox('live cells with only one live neighbor die')] | ||
| public function testLiveCellsWithOnlyOneLiveNeighborDie(): void | ||
| { | ||
| $this->assertEquals([ | ||
| [0, 0, 0], | ||
| [0, 0, 0], | ||
| [0, 0, 0] | ||
| ], tick([ | ||
| [0, 0, 0], | ||
| [0, 1, 0], | ||
| [0, 1, 0] | ||
| ])); | ||
| } | ||
|
|
||
| /** | ||
| * uuid 2a713b56-283c-48c8-adae-1d21306c80ae | ||
| */ | ||
| #[TestDox('live cells with two live neighbors stay alive')] | ||
| public function testLiveCellsWithTwoLiveNeighborsStayAlive(): void | ||
| { | ||
| $this->assertEquals([ | ||
| [0, 0, 0], | ||
| [1, 0, 1], | ||
| [0, 0, 0] | ||
| ], tick([ | ||
| [1, 0, 1], | ||
| [1, 0, 1], | ||
| [1, 0, 1] | ||
| ])); | ||
| } | ||
|
|
||
| /** | ||
| * uuid 86d5c5a5-ab7b-41a1-8907-c9b3fc5e9dae | ||
| */ | ||
| #[TestDox('live cells with three live neighbors stay alive')] | ||
| public function testLiveCellsWithThreeLiveNeighborsStayAlive(): void | ||
| { | ||
| $this->assertEquals([ | ||
| [0, 0, 0], | ||
| [1, 0, 0], | ||
| [1, 1, 0] | ||
| ], tick([ | ||
| [0, 1, 0], | ||
| [1, 0, 0], | ||
| [1, 1, 0] | ||
| ])); | ||
| } | ||
|
|
||
| /** | ||
| * uuid 015f60ac-39d8-4c6c-8328-57f334fc9f89 | ||
| */ | ||
| #[TestDox('dead cells with three live neighbors become alive')] | ||
| public function testDeadCellsWithThreeLiveNeighborsBecomeAlive(): void | ||
| { | ||
| $this->assertEquals([ | ||
| [0, 0, 0], | ||
| [1, 1, 0], | ||
| [0, 0, 0] | ||
| ], tick([ | ||
| [1, 1, 0], | ||
| [0, 0, 0], | ||
| [1, 0, 0] | ||
| ])); | ||
| } | ||
|
|
||
| /** | ||
| * uuid 2ee69c00-9d41-4b8b-89da-5832e735ccf1 | ||
| */ | ||
| #[TestDox('live cells with four or more neighbors die')] | ||
| public function testLiveCellsWithFourOrMoreNeighborsDie(): void | ||
| { | ||
| $this->assertEquals([ | ||
| [1, 0, 1], | ||
| [0, 0, 0], | ||
| [1, 0, 1] | ||
| ], tick([ | ||
| [1, 1, 1], | ||
| [1, 1, 1], | ||
| [1, 1, 1] | ||
| ])); | ||
| } | ||
|
|
||
| /** | ||
| * uuid a79b42be-ed6c-4e27-9206-43da08697ef6 | ||
| */ | ||
| #[TestDox('bigger matrix')] | ||
| public function testBiggerMatrix(): void | ||
| { | ||
| $this->assertEquals([ | ||
| [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] | ||
| ], tick([ | ||
| [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] | ||
| ])); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this a
4. It's looping over a nested array with basic operations and some state tracking.