-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Tim Austin <[email protected]>
- Loading branch information
1 parent
413a5a7
commit 62ea9d4
Showing
6 changed files
with
390 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Description | ||
|
||
The dice game [Yacht][yacht] is from the same family as Poker Dice, Generala and particularly Yahtzee, of which it is a precursor. | ||
In the game, five dice are rolled and the result can be entered in any of twelve categories. | ||
The score of a throw of the dice depends on category chosen. | ||
|
||
## Scores in Yacht | ||
|
||
| Category | Score | Description | Example | | ||
| -------- | ----- | ----------- | ------- | | ||
| Ones | 1 × number of ones | Any combination | 1 1 1 4 5 scores 3 | | ||
| Twos | 2 × number of twos | Any combination | 2 2 3 4 5 scores 4 | | ||
| Threes | 3 × number of threes | Any combination | 3 3 3 3 3 scores 15 | | ||
| Fours | 4 × number of fours | Any combination | 1 2 3 3 5 scores 0 | | ||
| Fives | 5 × number of fives| Any combination | 5 1 5 2 5 scores 15 | | ||
| Sixes | 6 × number of sixes | Any combination | 2 3 4 5 6 scores 6 | | ||
| Full House | Total of the dice | Three of one number and two of another | 3 3 3 5 5 scores 19 | | ||
| Four of a Kind | Total of the four dice | At least four dice showing the same face | 4 4 4 4 6 scores 16 | | ||
| Little Straight | 30 points | 1-2-3-4-5 | 1 2 3 4 5 scores 30 | | ||
| Big Straight | 30 points | 2-3-4-5-6 | 2 3 4 5 6 scores 30 | | ||
| Choice | Sum of the dice | Any combination | 2 3 3 4 6 scores 18 | | ||
| Yacht | 50 points | All five dice showing the same face | 4 4 4 4 4 scores 50 | | ||
|
||
If the dice do not satisfy the requirements of a category, the score is zero. | ||
If, for example, *Four Of A Kind* is entered in the *Yacht* category, zero points are scored. | ||
A *Yacht* scores zero if entered in the *Full House* category. | ||
|
||
## Task | ||
|
||
Given a list of values for five dice and a category, your solution should return the score of the dice for that category. | ||
If the dice do not satisfy the requirements of the category your solution should return 0. | ||
You can assume that five values will always be presented, and the value of each will be between one and six inclusively. | ||
You should not assume that the dice are ordered. | ||
|
||
[yacht]: https://en.wikipedia.org/wiki/Yacht_(dice_game) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"blurb": "Score a single throw of dice in the game Yacht.", | ||
"authors": ["MichaelBunker"], | ||
"contributors": [], | ||
"files": { | ||
"solution": ["Yacht.php"], | ||
"test": ["YachtTest.php"], | ||
"example": [".meta/example.php"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?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 Yacht | ||
{ | ||
public function score(array $rolls, string $category): int | ||
{ | ||
sort($rolls); | ||
|
||
switch ($category) { | ||
case 'ones': | ||
return $this->number($rolls, 1); | ||
case 'twos': | ||
return $this->number($rolls, 2); | ||
case 'threes': | ||
return $this->number($rolls, 3); | ||
case 'fours': | ||
return $this->number($rolls, 4); | ||
case 'fives': | ||
return $this->number($rolls, 5); | ||
case 'sixes': | ||
return $this->number($rolls, 6); | ||
case 'full house': | ||
return $this->fullHouse($rolls); | ||
case 'four of a kind': | ||
return $this->fourKind($rolls); | ||
case 'little straight': | ||
return $this->littleStraight($rolls); | ||
case 'big straight': | ||
return $this->bigStraight($rolls); | ||
case 'choice': | ||
return $this->choice($rolls); | ||
case 'yacht': | ||
return $this->scoreYacht($rolls); | ||
} | ||
} | ||
|
||
private function number(array $rolls, int $number) | ||
{ | ||
$totals = array_count_values($rolls); | ||
|
||
if (array_key_exists($number, $totals)) { | ||
return $number * $totals[$number]; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
private function fullHouse(array $rolls) | ||
{ | ||
$totals = array_count_values($rolls); | ||
|
||
if (count($totals) === 2 && !in_array(4, $totals)) { | ||
return $this->choice($rolls); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
private function fourKind(array $rolls) | ||
{ | ||
$totals = array_count_values($rolls); | ||
|
||
foreach ($totals as $roll => $times) { | ||
if ($times >= 4) { | ||
return $roll * 4; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
private function littleStraight(array $rolls): int | ||
{ | ||
return $rolls === [1, 2, 3, 4, 5] ? 30 : 0; | ||
} | ||
|
||
private function bigStraight(array $rolls): int | ||
{ | ||
return $rolls === [2, 3, 4, 5, 6] ? 30 : 0; | ||
} | ||
|
||
private function choice(array $rolls): int | ||
{ | ||
return array_sum($rolls); | ||
} | ||
|
||
private function scoreYacht(array $rolls): int | ||
{ | ||
if (array_unique($rolls) === [current($rolls)]) { | ||
return 50; | ||
} | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?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 Yacht | ||
{ | ||
public function score(array $rolls, string $category): int | ||
{ | ||
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); | ||
} | ||
} |
Oops, something went wrong.