Skip to content

Commit

Permalink
sync connect
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasnorre committed Nov 17, 2024
1 parent 1df6486 commit 60bd467
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 121 deletions.
26 changes: 11 additions & 15 deletions exercises/practice/connect/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
2 changes: 1 addition & 1 deletion exercises/practice/connect/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
22 changes: 0 additions & 22 deletions exercises/practice/connect/.meta/example.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<?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);

const NOTHING = 0;
Expand Down
166 changes: 83 additions & 83 deletions exercises/practice/connect/ConnectTest.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<?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);

class ConnectTest extends PHPUnit\Framework\TestCase
Expand All @@ -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 = [
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 . .",
Expand All @@ -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);
}
}

0 comments on commit 60bd467

Please sign in to comment.