Avatar of artemkorsakov

artemkorsakov's solution

to Yacht in the C# Track

Published at May 03 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.

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.

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)

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.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)
    {
        switch (category)
        {
            case YachtCategory.Yacht:
                return GetYACHTScore(dice);
            case YachtCategory.Ones:
                return GetNumberScore(dice, 1);
            case YachtCategory.Twos:
                return GetNumberScore(dice, 2);
            case YachtCategory.Threes:
                return GetNumberScore(dice, 3);
            case YachtCategory.Fours:
                return GetNumberScore(dice, 4);
            case YachtCategory.Fives:
                return GetNumberScore(dice, 5);
            case YachtCategory.Sixes:
                return GetNumberScore(dice, 6);
            case YachtCategory.FullHouse:
                return GetFullHouseScore(dice);
            case YachtCategory.FourOfAKind:
                return GetFourOfAKindScore(dice);
            case YachtCategory.LittleStraight:
                return GetLittleStraightScore(dice);
            case YachtCategory.BigStraight:
                return GetBigStraightScore(dice);
            case YachtCategory.Choice:
                return dice.Sum();
        }

        throw new ArgumentException();
    }


    private static int GetYACHTScore(int[] dice) => dice.Distinct().Count() == 1 ? 50 : 0;

    private static int GetNumberScore(int[] dice, int number) => dice.Where(d => d == number).Sum();

    private static int GetFullHouseScore(int[] dice)
    {
        var isOnlyTwoUnique = dice.Distinct().Count() == 2;
        var firstCount = dice.Where(d => d == dice[0]).Count();
        var isFullHouse = isOnlyTwoUnique && (firstCount == 3 || firstCount == 2);
        return isFullHouse ? dice.Sum() : 0;
    }

    private static int GetFourOfAKindScore(int[] dice)
    {
        if (dice.Distinct().Count() > 2)
        {
            return 0;
        }
        int firstCount = dice.Where(d => d == dice[0]).Count();
        return firstCount >= 4 ? 4 * dice[0] : firstCount == 1 ? dice.Sum() - dice[0] : 0;
    }

    private static int GetLittleStraightScore(int[] dice)
    {
        var isLittleStraightSum = dice.Sum() == 15;
        return IsStraight(dice) && isLittleStraightSum ? 30 : 0;
    }

    private static int GetBigStraightScore(int[] dice)
    {
        var isBigStraightSum = dice.Sum() == 20;
        return IsStraight(dice) && isBigStraightSum ? 30 : 0;
    }

    private static bool IsStraight(int[] dice) => dice.Distinct().Count() == 5;
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?
  • Are there new concepts here that you could read more about to improve your understanding?