Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/auto-sync.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ etl
flatten-array
flower-field
food-chain
game-of-life
gigasecond
grade-school
grains
Expand Down
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "game-of-life",
"name": "Conway's Game of Life",
"uuid": "33d09051-b335-453b-80c1-0909a5c05b8a",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "crypto-square",
"name": "Crypto Square",
Expand Down
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
19 changes: 19 additions & 0 deletions exercises/practice/game-of-life/.meta/config.json
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"
}
53 changes: 53 additions & 0 deletions exercises/practice/game-of-life/.meta/example.php
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;
}
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"
30 changes: 30 additions & 0 deletions exercises/practice/game-of-life/GameOfLife.php
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
{
throw new \BadFunctionCallException("Implement the tick function");
}
152 changes: 152 additions & 0 deletions exercises/practice/game-of-life/GameOfLifeTest.php
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]
]));
}
}
Loading