From 079a88f07552ca6ed3d50a0fa5e0e963887b182a Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Sun, 17 Nov 2024 20:03:29 +0100 Subject: [PATCH 1/2] sync connect --- bin/test.sh | 2 +- .../practice/connect/.docs/instructions.md | 26 ++- exercises/practice/connect/.meta/config.json | 2 +- exercises/practice/connect/.meta/example.php | 22 --- exercises/practice/connect/ConnectTest.php | 166 +++++++++--------- 5 files changed, 96 insertions(+), 122 deletions(-) diff --git a/bin/test.sh b/bin/test.sh index 182accb1..33457016 100755 --- a/bin/test.sh +++ b/bin/test.sh @@ -13,7 +13,7 @@ file_ext="php" function main { has_failures=0 - name_filter=() + name_filter=( "-name" "*" ) # MacOS otherwise sees unbound variable if [ $# -ge 1 ] && [ -n "$1" ]; then name_filter=("-name" "$1") fi diff --git a/exercises/practice/connect/.docs/instructions.md b/exercises/practice/connect/.docs/instructions.md index 2fa003a8..7f34bfa8 100644 --- a/exercises/practice/connect/.docs/instructions.md +++ b/exercises/practice/connect/.docs/instructions.md @@ -2,19 +2,14 @@ Compute the result for a game of Hex / Polygon. -The abstract boardgame known as -[Hex](https://en.wikipedia.org/wiki/Hex_%28board_game%29) / Polygon / -CON-TAC-TIX is quite simple in rules, though complex in practice. Two players -place stones on a parallelogram with hexagonal fields. The player to connect his/her -stones to the opposite side first wins. The four sides of the parallelogram are -divided between the two players (i.e. one player gets assigned a side and the -side directly opposite it and the other player gets assigned the two other -sides). +The abstract boardgame known as [Hex][hex] / Polygon / CON-TAC-TIX is quite simple in rules, though complex in practice. +Two players place stones on a parallelogram with hexagonal fields. +The player to connect his/her stones to the opposite side first wins. +The four sides of the parallelogram are divided between the two players (i.e. one player gets assigned a side and the side directly opposite it and the other player gets assigned the two other sides). -Your goal is to build a program that given a simple representation of a board -computes the winner (or lack thereof). Note that all games need not be "fair". -(For example, players may have mismatched piece counts or the game's board might -have a different width and height.) +Your goal is to build a program that given a simple representation of a board computes the winner (or lack thereof). +Note that all games need not be "fair". +(For example, players may have mismatched piece counts or the game's board might have a different width and height.) The boards look like this: @@ -26,6 +21,7 @@ The boards look like this: X O O O X ``` -"Player `O`" plays from top to bottom, "Player `X`" plays from left to right. In -the above example `O` has made a connection from left to right but nobody has -won since `O` didn't connect top and bottom. +"Player `O`" plays from top to bottom, "Player `X`" plays from left to right. +In the above example `O` has made a connection from left to right but nobody has won since `O` didn't connect top and bottom. + +[hex]: https://en.wikipedia.org/wiki/Hex_%28board_game%29 diff --git a/exercises/practice/connect/.meta/config.json b/exercises/practice/connect/.meta/config.json index 76278a1a..3dc8c596 100644 --- a/exercises/practice/connect/.meta/config.json +++ b/exercises/practice/connect/.meta/config.json @@ -23,5 +23,5 @@ ".meta/example.php" ] }, - "blurb": "Compute the result for a game of Hex / Polygon" + "blurb": "Compute the result for a game of Hex / Polygon." } diff --git a/exercises/practice/connect/.meta/example.php b/exercises/practice/connect/.meta/example.php index 527dabb9..28d53331 100644 --- a/exercises/practice/connect/.meta/example.php +++ b/exercises/practice/connect/.meta/example.php @@ -1,27 +1,5 @@ . - * - * To disable strict typing, comment out the directive below. - */ - declare(strict_types=1); const NOTHING = 0; diff --git a/exercises/practice/connect/ConnectTest.php b/exercises/practice/connect/ConnectTest.php index 862e9758..9d225ecd 100644 --- a/exercises/practice/connect/ConnectTest.php +++ b/exercises/practice/connect/ConnectTest.php @@ -1,27 +1,5 @@ . - * - * To disable strict typing, comment out the directive below. - */ - declare(strict_types=1); class ConnectTest extends PHPUnit\Framework\TestCase @@ -32,15 +10,9 @@ public static function setUpBeforeClass(): void } /** - * Strip off the spaces which are only for readability. + * uuid 6eff0df4-3e92-478d-9b54-d3e8b354db56 + * @testdox an empty board has no winner */ - private function makeBoard($lines): array - { - return array_map(function ($line) { - return str_replace(" ", "", $line); - }, $lines); - } - public function testEmptyBoardHasNoWinner(): void { $lines = [ @@ -54,6 +26,9 @@ public function testEmptyBoardHasNoWinner(): void } /** + * uuid 298b94c0-b46d-45d8-b34b-0fa2ea71f0a4 + * @testdox X can win on a 1x1 board + * * @depends testEmptyBoardHasNoWinner */ public function testOneByOneBoardBlack(): void @@ -63,6 +38,9 @@ public function testOneByOneBoardBlack(): void } /** + * uuid 763bbae0-cb8f-4f28-bc21-5be16a5722dc + * @testdox O can win on a 1x1 board + * * @depends testEmptyBoardHasNoWinner */ public function testOneByOneBoardWhite(): void @@ -72,40 +50,57 @@ public function testOneByOneBoardWhite(): void } /** - * @depends testOneByOneBoardBlack - * @depends testOneByOneBoardWhite + * uuid 819fde60-9ae2-485e-a024-cbb8ea68751b + * @testdox only edges does not make a winner */ - public function testConvultedPath(): void + public function testOnlyEgesDoesNotMakeAWinner(): void { $lines = [ - ". X X . .", - " X . X . X", - " . X . X .", - " . X X . .", - " O O O O O", + "O O O X", + " X . . X", + " X . . X", + " X O O O", ]; - $this->assertEquals("black", resultFor($this->makeBoard($lines))); + $this->assertEquals("", resultFor($this->makeBoard($lines))); } /** - * @depends testConvultedPath + * uuid 2c56a0d5-9528-41e5-b92b-499dfe08506c + * @testdox illegal diagonal does not make a winner */ - public function testRectangleWhiteWins(): void + public function testIllegalDiagonalDoesNotMakeAWinner(): void { $lines = [ - ". O . .", + "X O . .", " O X X X", - " O O O .", - " X X O X", - " . O X .", + " O X O .", + " . O X .", + " X X O O", ]; - $this->assertEquals("white", resultFor($this->makeBoard($lines))); + $this->assertEquals("", resultFor($this->makeBoard($lines))); + } + + /** + * uuid 41cce3ef-43ca-4963-970a-c05d39aa1cc1 + * @testdox nobody wins crossing adjacent angles + */ + public function testNobodyWinsCrossingAdjacentAngles(): void + { + $lines = [ + "X . . .", + " . X O .", + " O . X O", + " . O . X", + " . . O .", + ]; + $this->assertEquals("", resultFor($this->makeBoard($lines))); } /** - * @depends testConvultedPath + * uuid cd61c143-92f6-4a8d-84d9-cb2b359e226b + * @testdox X wins crossing from left to right */ - public function testRectangleBlackWins(): void + public function testXWinsCrossingFromLeftToRight(): void { $lines = [ ". O . .", @@ -118,59 +113,64 @@ public function testRectangleBlackWins(): void } /** - * @depends testRectangleWhiteWins - * @depends testRectangleBlackWins + * uuid 73d1eda6-16ab-4460-9904-b5f5dd401d0b + * @testdox O wins crossing from top to bottom */ - public function testSpiralBlackWins(): void + public function testOWinsCrossingFromTopToBottom(): void { $lines = [ - "OXXXXXXXX", - "OXOOOOOOO", - "OXOXXXXXO", - "OXOXOOOXO", - "OXOXXXOXO", - "OXOOOXOXO", - "OXXXXXOXO", - "OOOOOOOXO", - "XXXXXXXXO", + ". O . .", + " O X X X", + " O O O .", + " X X O X", + " . O X .", ]; - $this->assertEquals("black", resultFor($this->makeBoard($lines))); + $this->assertEquals("white", resultFor($this->makeBoard($lines))); } /** - * @depends testRectangleWhiteWins - * @depends testRectangleBlackWins + * uuid c3a2a550-944a-4637-8b3f-1e1bf1340a3d + * @testdox X wins using a convoluted path */ - public function testSpiralNobodyWins(): void + public function testXWinsUsingAConvolutedPath(): void { $lines = [ - "OXXXXXXXX", - "OXOOOOOOO", - "OXOXXXXXO", - "OXOXOOOXO", - "OXOX.XOXO", - "OXOOOXOXO", - "OXXXXXOXO", - "OOOOOOOXO", - "XXXXXXXXO", + ". X X . .", + " X . X . X", + " . X . X .", + " . X X . .", + " O O O O O", ]; - $this->assertEquals(null, resultFor($this->makeBoard($lines))); + $this->assertEquals("black", resultFor($this->makeBoard($lines))); } /** - * @depends testSpiralBlackWins - * @depends testSpiralNobodyWins + * uuid 17e76fa8-f731-4db7-92ad-ed2a285d31f3 + * @testdox X wins using a spiral path */ - public function testIllegalDiagonalNobodyWins(): void + public function testXWinsUsingASpiralPath(): void { $lines = [ - "X O . .", - " O X X X", - " O X O .", - " . O X .", - " X X O O", + "O X X X X X X X X", + " O X O O O O O O O", + " O X O X X X X X O", + " O X O X O O O X O", + " O X O X X X O X O", + " O X O O O X O X O", + " O X X X X X O X O", + " O O O O O O O X O", + " X X X X X X X X O", ]; + $this->assertEquals("black", resultFor($this->makeBoard($lines))); + } - $this->assertEquals(null, resultFor($this->makeBoard($lines))); + /** + * Strip off the spaces which are only for readability. + */ + private function makeBoard($lines): array + { + return array_map(function ($line) { + return str_replace(" ", "", $line); + }, $lines); } } From 4e92ed87be5a18a9f51962a3b6d82503bba09267 Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Sat, 21 Dec 2024 22:05:46 +0100 Subject: [PATCH 2/2] fix: update according to feedback --- exercises/practice/connect/.meta/example.php | 38 ++++---------------- exercises/practice/connect/Connect.php | 4 +-- exercises/practice/connect/ConnectTest.php | 34 ++++++------------ 3 files changed, 18 insertions(+), 58 deletions(-) diff --git a/exercises/practice/connect/.meta/example.php b/exercises/practice/connect/.meta/example.php index 28d53331..38e88913 100644 --- a/exercises/practice/connect/.meta/example.php +++ b/exercises/practice/connect/.meta/example.php @@ -96,40 +96,14 @@ public function blackStartCoords(): array } return $coords; } - - // Prints the board, occasionally useful for debugging. - // Capital letters indicate a connect flag has been set. - public function dump(): void - { - print "\n"; - for ($y = 0; $y < $this->height; $y++) { - print str_repeat(" ", $y); - for ($x = 0; $x < $this->width; $x++) { - $f = $this->fields[$y][$x]; - if ($f & WHITE) { - if ($f & WHITE_CONNECT) { - print "O"; - } else { - print "o"; - } - } elseif ($f & BLACK) { - if ($f & BLACK_CONNECT) { - print "X"; - } else { - print "x"; - } - } else { - print "."; - } - print " "; - } - print "\n"; - } - } } -function resultFor(array $lines) +function winner(array $lines): ?string { + $lines = array_map(function ($line) { + return str_replace(" ", "", $line); + }, $lines); + $board = new Board($lines); // Order of checking black and white doesn't matter, only one can win. foreach ($board->blackStartCoords() as $c) { @@ -145,7 +119,7 @@ function resultFor(array $lines) return null; } -function flood($board, $c, $colour_flag, $connect_flag) +function flood($board, $c, $colour_flag, $connect_flag): bool { $f = $board->at($c); $is_colour = $f & $colour_flag; diff --git a/exercises/practice/connect/Connect.php b/exercises/practice/connect/Connect.php index 7acea120..d5d0e5fc 100644 --- a/exercises/practice/connect/Connect.php +++ b/exercises/practice/connect/Connect.php @@ -24,7 +24,7 @@ declare(strict_types=1); -function resultFor(array $lines) +function winner(array $lines): ?string { - throw new \BadFunctionCallException("Implement the resultFor method"); + throw new \BadFunctionCallException("Implement the winner method"); } diff --git a/exercises/practice/connect/ConnectTest.php b/exercises/practice/connect/ConnectTest.php index 9d225ecd..cae2b1b3 100644 --- a/exercises/practice/connect/ConnectTest.php +++ b/exercises/practice/connect/ConnectTest.php @@ -22,31 +22,27 @@ public function testEmptyBoardHasNoWinner(): void " . . . . .", " . . . . .", ]; - $this->assertEquals(null, resultFor($this->makeBoard($lines))); + $this->assertEquals(null, winner($lines)); } /** * uuid 298b94c0-b46d-45d8-b34b-0fa2ea71f0a4 * @testdox X can win on a 1x1 board - * - * @depends testEmptyBoardHasNoWinner */ public function testOneByOneBoardBlack(): void { $lines = ["X"]; - $this->assertEquals("black", resultFor($this->makeBoard($lines))); + $this->assertEquals("black", winner($lines)); } /** * uuid 763bbae0-cb8f-4f28-bc21-5be16a5722dc * @testdox O can win on a 1x1 board - * - * @depends testEmptyBoardHasNoWinner */ public function testOneByOneBoardWhite(): void { $lines = ["O"]; - $this->assertEquals("white", resultFor($this->makeBoard($lines))); + $this->assertEquals("white", winner($lines)); } /** @@ -61,7 +57,7 @@ public function testOnlyEgesDoesNotMakeAWinner(): void " X . . X", " X O O O", ]; - $this->assertEquals("", resultFor($this->makeBoard($lines))); + $this->assertEquals("", winner($lines)); } /** @@ -77,7 +73,7 @@ public function testIllegalDiagonalDoesNotMakeAWinner(): void " . O X .", " X X O O", ]; - $this->assertEquals("", resultFor($this->makeBoard($lines))); + $this->assertEquals("", winner($lines)); } /** @@ -93,7 +89,7 @@ public function testNobodyWinsCrossingAdjacentAngles(): void " . O . X", " . . O .", ]; - $this->assertEquals("", resultFor($this->makeBoard($lines))); + $this->assertEquals("", winner($lines)); } /** @@ -109,7 +105,7 @@ public function testXWinsCrossingFromLeftToRight(): void " X X O X", " . O X .", ]; - $this->assertEquals("black", resultFor($this->makeBoard($lines))); + $this->assertEquals("black", winner($lines)); } /** @@ -125,7 +121,7 @@ public function testOWinsCrossingFromTopToBottom(): void " X X O X", " . O X .", ]; - $this->assertEquals("white", resultFor($this->makeBoard($lines))); + $this->assertEquals("white", winner($lines)); } /** @@ -141,7 +137,7 @@ public function testXWinsUsingAConvolutedPath(): void " . X X . .", " O O O O O", ]; - $this->assertEquals("black", resultFor($this->makeBoard($lines))); + $this->assertEquals("black", winner($lines)); } /** @@ -161,16 +157,6 @@ public function testXWinsUsingASpiralPath(): void " O O O O O O O X O", " X X X X X X X X O", ]; - $this->assertEquals("black", resultFor($this->makeBoard($lines))); - } - - /** - * Strip off the spaces which are only for readability. - */ - private function makeBoard($lines): array - { - return array_map(function ($line) { - return str_replace(" ", "", $line); - }, $lines); + $this->assertEquals("black", winner($lines)); } }