Instructions

Test suite

Solution

The dice game 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.

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.

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.

To run the tests, run the command `dotnet test`

from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip`

property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using `exercism submit Yacht.cs`

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

James Kilfiger, using wikipedia https://en.wikipedia.org/wiki/Yacht_(dice_game)

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
// This file was auto-generated based on version 1.1.0 of the canonical data.
using Xunit;
public class YachtTest
{
[Fact]
public void Yacht()
{
Assert.Equal(50, YachtGame.Score(new[] { 5, 5, 5, 5, 5 }, YachtCategory.Yacht));
}
[Fact(Skip = "Remove to run test")]
public void Not_yacht()
{
Assert.Equal(0, YachtGame.Score(new[] { 1, 3, 3, 2, 5 }, YachtCategory.Yacht));
}
[Fact(Skip = "Remove to run test")]
public void Ones()
{
Assert.Equal(3, YachtGame.Score(new[] { 1, 1, 1, 3, 5 }, YachtCategory.Ones));
}
[Fact(Skip = "Remove to run test")]
public void Ones_out_of_order()
{
Assert.Equal(3, YachtGame.Score(new[] { 3, 1, 1, 5, 1 }, YachtCategory.Ones));
}
[Fact(Skip = "Remove to run test")]
public void No_ones()
{
Assert.Equal(0, YachtGame.Score(new[] { 4, 3, 6, 5, 5 }, YachtCategory.Ones));
}
[Fact(Skip = "Remove to run test")]
public void Twos()
{
Assert.Equal(2, YachtGame.Score(new[] { 2, 3, 4, 5, 6 }, YachtCategory.Twos));
}
[Fact(Skip = "Remove to run test")]
public void Fours()
{
Assert.Equal(8, YachtGame.Score(new[] { 1, 4, 1, 4, 1 }, YachtCategory.Fours));
}
[Fact(Skip = "Remove to run test")]
public void Yacht_counted_as_threes()
{
Assert.Equal(15, YachtGame.Score(new[] { 3, 3, 3, 3, 3 }, YachtCategory.Threes));
}
[Fact(Skip = "Remove to run test")]
public void Yacht_of_3s_counted_as_fives()
{
Assert.Equal(0, YachtGame.Score(new[] { 3, 3, 3, 3, 3 }, YachtCategory.Fives));
}
[Fact(Skip = "Remove to run test")]
public void Sixes()
{
Assert.Equal(6, YachtGame.Score(new[] { 2, 3, 4, 5, 6 }, YachtCategory.Sixes));
}
[Fact(Skip = "Remove to run test")]
public void Full_house_two_small_three_big()
{
Assert.Equal(16, YachtGame.Score(new[] { 2, 2, 4, 4, 4 }, YachtCategory.FullHouse));
}
[Fact(Skip = "Remove to run test")]
public void Full_house_three_small_two_big()
{
Assert.Equal(19, YachtGame.Score(new[] { 5, 3, 3, 5, 3 }, YachtCategory.FullHouse));
}
[Fact(Skip = "Remove to run test")]
public void Two_pair_is_not_a_full_house()
{
Assert.Equal(0, YachtGame.Score(new[] { 2, 2, 4, 4, 5 }, YachtCategory.FullHouse));
}
[Fact(Skip = "Remove to run test")]
public void Four_of_a_kind_is_not_a_full_house()
{
Assert.Equal(0, YachtGame.Score(new[] { 1, 4, 4, 4, 4 }, YachtCategory.FullHouse));
}
[Fact(Skip = "Remove to run test")]
public void Yacht_is_not_a_full_house()
{
Assert.Equal(0, YachtGame.Score(new[] { 2, 2, 2, 2, 2 }, YachtCategory.FullHouse));
}
[Fact(Skip = "Remove to run test")]
public void Four_of_a_kind()
{
Assert.Equal(24, YachtGame.Score(new[] { 6, 6, 4, 6, 6 }, YachtCategory.FourOfAKind));
}
[Fact(Skip = "Remove to run test")]
public void Yacht_can_be_scored_as_four_of_a_kind()
{
Assert.Equal(12, YachtGame.Score(new[] { 3, 3, 3, 3, 3 }, YachtCategory.FourOfAKind));
}
[Fact(Skip = "Remove to run test")]
public void Full_house_is_not_four_of_a_kind()
{
Assert.Equal(0, YachtGame.Score(new[] { 3, 3, 3, 5, 5 }, YachtCategory.FourOfAKind));
}
[Fact(Skip = "Remove to run test")]
public void Little_straight()
{
Assert.Equal(30, YachtGame.Score(new[] { 3, 5, 4, 1, 2 }, YachtCategory.LittleStraight));
}
[Fact(Skip = "Remove to run test")]
public void Little_straight_as_big_straight()
{
Assert.Equal(0, YachtGame.Score(new[] { 1, 2, 3, 4, 5 }, YachtCategory.BigStraight));
}
[Fact(Skip = "Remove to run test")]
public void Four_in_order_but_not_a_little_straight()
{
Assert.Equal(0, YachtGame.Score(new[] { 1, 1, 2, 3, 4 }, YachtCategory.LittleStraight));
}
[Fact(Skip = "Remove to run test")]
public void No_pairs_but_not_a_little_straight()
{
Assert.Equal(0, YachtGame.Score(new[] { 1, 2, 3, 4, 6 }, YachtCategory.LittleStraight));
}
[Fact(Skip = "Remove to run test")]
public void Minimum_is_1_maximum_is_5_but_not_a_little_straight()
{
Assert.Equal(0, YachtGame.Score(new[] { 1, 1, 3, 4, 5 }, YachtCategory.LittleStraight));
}
[Fact(Skip = "Remove to run test")]
public void Big_straight()
{
Assert.Equal(30, YachtGame.Score(new[] { 4, 6, 2, 5, 3 }, YachtCategory.BigStraight));
}
[Fact(Skip = "Remove to run test")]
public void Big_straight_as_little_straight()
{
Assert.Equal(0, YachtGame.Score(new[] { 6, 5, 4, 3, 2 }, YachtCategory.LittleStraight));
}
[Fact(Skip = "Remove to run test")]
public void Choice()
{
Assert.Equal(23, YachtGame.Score(new[] { 3, 3, 5, 6, 6 }, YachtCategory.Choice));
}
[Fact(Skip = "Remove to run test")]
public void Yacht_as_choice()
{
Assert.Equal(10, YachtGame.Score(new[] { 2, 2, 2, 2, 2 }, YachtCategory.Choice));
}
}
```

```
using System;
using System.Linq;
using System.Collections.Generic;
public enum YachtCategory
{
Ones = 1,
Twos = 2,
Threes = 3,
Fours = 4,
Fives = 5,
Sixes = 6,
FullHouse = 7,
FourOfAKind = 8,
LittleStraight = 9,
BigStraight = 10,
Choice = 11,
Yacht = 12,
}
public static class YachtGame
{
public static int Score(int[] dice, YachtCategory category)
{
Dictionary<int, int> numbers = dice.GroupBy(t => t).ToDictionary(t => t.Key, t => t.Count());
switch (category)
{
case YachtCategory.Ones:
case YachtCategory.Twos:
case YachtCategory.Threes:
case YachtCategory.Fours:
case YachtCategory.Fives:
case YachtCategory.Sixes:
return dice.Where(t => t == (int)category).Sum();
case YachtCategory.FullHouse:
return numbers.All(t => t.Value > 1 && t.Value < 4) ? dice.Sum() : 0;
case YachtCategory.FourOfAKind:
return numbers.Where(t => t.Value > 3).Sum(t => t.Key * 4);
case YachtCategory.LittleStraight:
return numbers.Count() == 5 && !numbers.ContainsKey(6) ? 30 : 0;
case YachtCategory.BigStraight:
return numbers.Count() == 5 && !numbers.ContainsKey(1) ? 30 : 0;
case YachtCategory.Choice:
return dice.Sum();
default:
return numbers.Count() == 1 ? 50 : 0;
}
}
}
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,368 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments