# gnivler's solution

## to Yacht in the C# Track

Published at Apr 09 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

# Score a single throw of dice in Yacht

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.

## 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.

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.

## Running the tests

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`

## Further information

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.

## Source

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

## Submitting Incomplete Solutions

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

### YachtTest.cs

``````// 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.Collections.Generic;
using System.Linq;

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)
{
// anything up to sixes is just summed
if ((int) category <= 6)
{
return dice.Where(die => die == (int) category).Sum();
}

dice = dice.OrderBy(die => die).ToArray();

switch (category)
{
case YachtCategory.FullHouse: return ScoreFullHouse(dice);
case YachtCategory.FourOfAKind: return ScoreFourOfAKind(dice);
case YachtCategory.LittleStraight: return ScoreLittleStraight(dice);
case YachtCategory.BigStraight: return ScoreBigStraight(dice);
case YachtCategory.Choice: return ScoreChoice(dice);
case YachtCategory.Yacht: return ScoreYacht(dice);
default: throw new ArgumentOutOfRangeException(nameof(category), category, null);
}
}

private static int ScoreYacht(int[] dice)
{
return dice.Distinct().Count() == 1 ? 50 : 0;
}

private static int ScoreChoice(int[] dice)
{
return dice.Sum();
}

private static int ScoreBigStraight(int[] dice)
{
return string.Concat(dice.OrderBy(die => die)) == "23456" ? 30 : 0;
}

private static int ScoreLittleStraight(int[] dice)
{
return string.Concat(dice.OrderBy(die => die)) == "12345" ? 30 : 0;
}

private static int ScoreFourOfAKind(int[] dice)
{
// look for dice that appear 4 times and only count it 4 times
return dice.GroupBy(die => die)
.Where(die => die.Count() >= 4)
.Select(die => die.Key * 4)
.Sum();
}

private static int ScoreFullHouse(int[] dice)
{
// 3 of one 2 of another
return (dice.AppearsThisOften(3).Count() == 1 &&
dice.AppearsThisOften(2).Count() == 1)
? dice.Sum()
: 0;
}

/// <summary>
/// Returns IEnumerable<int> of dice face values appearing count times
/// </summary>
/// <param name="dice"></param>
/// <param name="count"></param>
/// <returns></returns>
static IEnumerable<int> AppearsThisOften(this int[] dice, int count)
{
return dice.GroupBy(die => die)
.Where(die => die.Count() == count)
.Select(die => die.Key);
}
}``````

### What can you learn from this solution?

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?