diff --git a/bin/auto-sync.txt b/bin/auto-sync.txt index 16e02841..dc779762 100644 --- a/bin/auto-sync.txt +++ b/bin/auto-sync.txt @@ -27,6 +27,7 @@ etl flatten-array flower-field food-chain +game-of-life gigasecond grade-school grains diff --git a/config.json b/config.json index 3268f8fc..3741fd7e 100644 --- a/config.json +++ b/config.json @@ -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", diff --git a/exercises/practice/game-of-life/.docs/instructions.md b/exercises/practice/game-of-life/.docs/instructions.md new file mode 100644 index 00000000..49531406 --- /dev/null +++ b/exercises/practice/game-of-life/.docs/instructions.md @@ -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. diff --git a/exercises/practice/game-of-life/.docs/introduction.md b/exercises/practice/game-of-life/.docs/introduction.md new file mode 100644 index 00000000..2347b936 --- /dev/null +++ b/exercises/practice/game-of-life/.docs/introduction.md @@ -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 diff --git a/exercises/practice/game-of-life/.meta/config.json b/exercises/practice/game-of-life/.meta/config.json new file mode 100644 index 00000000..7595c48e --- /dev/null +++ b/exercises/practice/game-of-life/.meta/config.json @@ -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" +} diff --git a/exercises/practice/game-of-life/.meta/example.php b/exercises/practice/game-of-life/.meta/example.php new file mode 100644 index 00000000..3afc9712 --- /dev/null +++ b/exercises/practice/game-of-life/.meta/example.php @@ -0,0 +1,53 @@ + $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; +} diff --git a/exercises/practice/game-of-life/.meta/tests.toml b/exercises/practice/game-of-life/.meta/tests.toml new file mode 100644 index 00000000..398cd454 --- /dev/null +++ b/exercises/practice/game-of-life/.meta/tests.toml @@ -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" diff --git a/exercises/practice/game-of-life/GameOfLife.php b/exercises/practice/game-of-life/GameOfLife.php new file mode 100644 index 00000000..d0e6a1df --- /dev/null +++ b/exercises/practice/game-of-life/GameOfLife.php @@ -0,0 +1,30 @@ +. + * + * 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"); +} diff --git a/exercises/practice/game-of-life/GameOfLifeTest.php b/exercises/practice/game-of-life/GameOfLifeTest.php new file mode 100644 index 00000000..5c08d779 --- /dev/null +++ b/exercises/practice/game-of-life/GameOfLifeTest.php @@ -0,0 +1,152 @@ +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] + ])); + } +}