ChrisPritchard's solution

to Yacht in the C# Track

Published at Sep 19 2018 · 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                   Example
Ones            1 × number of ones      1 1 1 4 5 scores 3
Twos            2 × number of twos      2 2 3 4 5 scores 4
Threes          3 × number of threes    3 3 3 3 3 scores 15
Fours           4 × number of fours     1 2 3 3 5 scores 0
Fives           5 × number of fives     5 1 5 2 5 scores 15
Sixes           6 × number of sixes     2 3 4 5 6 scores 6
Full House      Total of the dice       3 3 3 5 5 scores 19
Four of a Kind  Total of the four dice  4 4 4 4 6 scores 16
Little Straight 30 points               1 2 3 4 5 scores 30
Big Straight    30 points               2 3 4 5 6 scores 30
Choice          Sum of the dice         2 3 3 4 6 scores 18
Yacht           50 points               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.

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.FullHouse:
var fullHouse = dice.GroupBy(d => d).OrderBy(o => o.Count());
return fullHouse.First().Count() == 2 ? dice.Sum() : 0;
case YachtCategory.FourOfAKind:
var fourOfAKind = dice.GroupBy(d => d).OrderBy(o => o.Key);
return fourOfAKind.Last().Count() >= 4 ? fourOfAKind.Last().Take(4).Sum() : 0;
case YachtCategory.LittleStraight:
var littleStraight = dice.GroupBy(d => d).Select(o => o.Key).OrderBy(d => d).ToArray();
return littleStraight.Length == 5 && littleStraight[4] == 5 ? 30 : 0;
case YachtCategory.BigStraight:
var bigStraight = dice.GroupBy(d => d).Select(o => o.Key).OrderBy(d => d).ToArray();
return bigStraight.Length == 5 && bigStraight[0] == 2 ? 30 : 0;
case YachtCategory.Choice:
return dice.Sum();
case YachtCategory.Yacht:
var yaught = dice.GroupBy(d => d);
return yaught.Count() == 1 ? 50 : 0;
default:
return dice.Count(d => d == (int)category) * (int)category;
}
}
}``````