Add game-of-life exercice#1007
Open
resu-xuniL wants to merge 3 commits into
Open
Conversation
35 tasks
mk-mxp
requested changes
Jun 29, 2026
mk-mxp
left a comment
Contributor
There was a problem hiding this comment.
Great contribution. Thanks!
| "uuid": "33d09051-b335-453b-80c1-0909a5c05b8a", | ||
| "practices": [], | ||
| "prerequisites": [], | ||
| "difficulty": 1 |
Contributor
There was a problem hiding this comment.
Let's make this a 4. It's looping over a nested array with basic operations and some state tracking.
|
|
||
| declare(strict_types=1); | ||
|
|
||
| function tick(array $matrix): array |
Contributor
There was a problem hiding this comment.
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 example.php cannot use this, as it has to support PHP8.2, too.
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);
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Another exercice for validation.