Skip to content

Commit

Permalink
4 snakes and ladders
Browse files Browse the repository at this point in the history
compile xdebug from source cause `pecl install xdebug` wont work anymore
setup snakes and ladders kata with first unit test
  • Loading branch information
Andre Rademacher committed Oct 11, 2023
1 parent fac010d commit 914abc4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
54 changes: 52 additions & 2 deletions kata/SnakesAndLadders/SnakesLadders.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,63 @@

final class SnakesLadders
{
const MAP = [
2 => 38, 7 => 14, 8 => 31,
15 => 26, 16 => 6,
21 => 42, 28 => 84,
36 => 44,
46 => 25, 49 => 11,
51 => 67,
62 => 19, 64 => 60,
71 => 91, 74 => 53, 78 => 98,
87 => 94, 89 => 68,
92 => 88, 95 => 75, 99 => 80,

// implicit snakes in case player goes over 100
101 => 80, 102 => 98, 103 => 97, 104 => 96, 105 => 75, 106 => 94,
107 => 93, 108 => 88, 109 => 91, 110 => 90, 111 => 68,
];

private $currentPlayer = 1;

private $playerPosition = [
1 => 0,
2 => 0,
];

/**
* Initializes the game.
*/
public function __construct()
{

}

/**
* The turn of the current player is executed.
*/
public function play(int $die1, int $die2): string
{
return '';
// in case one player has already won, don't execute another turn
if (in_array(100, $this->playerPosition, true)) {
return 'Game over!';
}

// move the player token
$newPosition = $this->playerPosition[$this->currentPlayer] + $die1 + $die2;
$finalPosition = self::MAP[$newPosition] ?? $newPosition;

// report the result string
$result = ($newPosition === 100)
? 'Player ' . $this->currentPlayer . ' Wins!'
: 'Player ' . $this->currentPlayer . ' is on square ' . $finalPosition;

$this->playerPosition[$this->currentPlayer] = $finalPosition;

// if the current player rolled a double, it's his turn again, else switch players
$this->currentPlayer = ($die1 === $die2)
? $this->currentPlayer
: $this->currentPlayer ^ 3;

return $result;
}
}
17 changes: 16 additions & 1 deletion kata/SnakesAndLadders/SnakesLaddersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,28 @@ public function testPlay(array $expectedOutput, array $diceThrows)

public function providePlay(): Generator
{
yield 'Player 1 just throws 1 & 1' => [
yield 'player 1 just throws 1 & 1' => [
[
'Player 1 is on square 38',
],
[
[1, 1],
]
];

yield 'simple game with 4 turns' => [
[
'Player 1 is on square 38',
'Player 1 is on square 44',
'Player 2 is on square 31',
'Player 1 is on square 25',
],
[
[1, 1],
[1, 5],
[6, 2],
[1, 1],
]
];
}
}

0 comments on commit 914abc4

Please sign in to comment.