Exemple #1
0
    public static int Score(int[] dice, YachtCategory category)
    {
        var groups = dice.GroupBy(x => x).ToList();

        var isYacht          = groups.Count == 1;
        var isFullHouse      = groups.Count == 2 && groups.Any(x => x.Count() == 3);
        var isLittleStraight = groups.Count == 5 && dice.Sum() == LittleStraight;
        var isBigStraight    = groups.Count == 5 && dice.Sum() == BigStraight;

        return(category switch
        {
            YachtCategory.Ones => SumCategory(dice, YachtCategory.Ones),
            YachtCategory.Twos => SumCategory(dice, YachtCategory.Twos),
            YachtCategory.Threes => SumCategory(dice, YachtCategory.Threes),
            YachtCategory.Fours => SumCategory(dice, YachtCategory.Fours),
            YachtCategory.Fives => SumCategory(dice, YachtCategory.Fives),
            YachtCategory.Sixes => SumCategory(dice, YachtCategory.Sixes),
            YachtCategory.Choice => dice.Sum(),
            YachtCategory.FullHouse when isFullHouse => dice.Sum(),
            YachtCategory.LittleStraight when isLittleStraight => 30,
            YachtCategory.BigStraight when isBigStraight => 30,
            YachtCategory.Yacht when isYacht => 50,
            YachtCategory.FourOfAKind => dice
            .GroupBy(x => x)
            .Where(x => x.Count() >= 4)
            .Select(x => x.Take(4).Sum())
            .FirstOrDefault(),
            _ => 0
        });
Exemple #2
0
    public static int Score(int[] dice, YachtCategory category)
    {
        switch (category)
        {
        case YachtCategory.Ones:
        case YachtCategory.Twos:
        case YachtCategory.Threes:
        case YachtCategory.Fours:
        case YachtCategory.Fives:
        case YachtCategory.Sixes:
            return(dice.Where(x => x == (int)category).Sum());

        case YachtCategory.FullHouse:
            return(IsFullHouse(dice) ? dice.Sum() : 0);

        case YachtCategory.FourOfAKind:
            return(IsFourOfAKind(dice) ? GetFourOfAKindTotal(dice) : 0);

        case YachtCategory.LittleStraight:
            return(IsLittleStraight(dice) ? 30 : 0);

        case YachtCategory.BigStraight:
            return(IsBigStraight(dice) ? 30 : 0);

        case YachtCategory.Choice:
            return(dice.Sum());

        case YachtCategory.Yacht:
            return(dice.Distinct().Count() == 1 ? 50 : 0);

        default:
            return(0);
        }
    }
Exemple #3
0
    public static int Score(int[] dice, YachtCategory category)
    {
        switch (category)
        {
        case YachtCategory.Yacht:
            return(dice.IsYacht());

        case YachtCategory.Ones:
        case YachtCategory.Twos:
        case YachtCategory.Threes:
        case YachtCategory.Fours:
        case YachtCategory.Fives:
        case YachtCategory.Sixes:
            return(dice.IsAnyCombinationOf(category));

        case YachtCategory.FullHouse:
            return(dice.IsFullHouse());

        case YachtCategory.FourOfAKind:
            return(dice.IsFourOfAKind());

        case YachtCategory.LittleStraight:
            return(dice.IsLittleStraight());

        case YachtCategory.BigStraight:
            return(dice.IsBigStraight());

        case YachtCategory.Choice:
            return(dice.Sum());

        default:
            return(0);
        }
    }
Exemple #4
0
    public static int Score(int[] dice, YachtCategory category)
    {
        var mode = dice.GroupBy(i => i).OrderByDescending(g => g.Count()).Select(g => g.Key).FirstOrDefault();

        switch (category)
        {
        case YachtCategory.Ones: return(dice.Where(x => x == 1).Sum());

        case YachtCategory.Twos: return(dice.Where(x => x == 2).Sum());

        case YachtCategory.Threes: return(dice.Where(x => x == 3).Sum());

        case YachtCategory.Fours: return(dice.Where(x => x == 4).Sum());

        case YachtCategory.Fives: return(dice.Where(x => x == 5).Sum());

        case YachtCategory.Sixes: return(dice.Where(x => x == 6).Sum());

        case YachtCategory.FullHouse: return(dice.Distinct().Count() == 2 && dice.Where(x => x == mode).Count() < 4 ? dice.Sum() : 0);

        case YachtCategory.FourOfAKind: return(dice.Where(x => x == mode).Count() >= 4 ? dice.Where(x => x == mode).Take(4).Sum() : 0);

        case YachtCategory.LittleStraight: return(dice.Except(new[] { 6 }).Count() == 5 ? 30 : 0);

        case YachtCategory.BigStraight: return(dice.Except(new[] { 1 }).Count() == 5 ? 30 : 0);

        case YachtCategory.Choice: return(dice.Sum());

        case YachtCategory.Yacht: return(dice.Distinct().Count() == 1 ? 50 : 0);

        default: return(0);
        }
    }
Exemple #5
0
    public static int Score(int[] dice, YachtCategory category)
    {
        switch (category)
        {
        case YachtCategory.Ones: return(dice.Where(s => s == 1).Sum());

        case YachtCategory.Twos: return(dice.Where(s => s == 2).Count() * 2);

        case YachtCategory.Threes: return(dice.Where(s => s == 3).Count() * 3);

        case YachtCategory.Fours: return(dice.Where(s => s == 4).Count() * 4);

        case YachtCategory.Fives: return(dice.Where(s => s == 5).Count() * 5);

        case YachtCategory.Sixes: return(dice.Where(s => s == 6).Count() * 6);

        case YachtCategory.FullHouse:
            int tmpCount = dice.Distinct().Count();
            if (tmpCount == 2)
            {
                foreach (int i in dice.Distinct())
                {
                    if (dice.Where(s => s == i).Count() < 2)
                    {
                        return(0);
                    }
                }
                return(dice.Sum());
            }
            return(0);

        case YachtCategory.FourOfAKind: return(dice.Distinct().Select(s => dice.Where(t => t == s).Count() >= 4 ? s * 4 : 0).Sum());

        case YachtCategory.LittleStraight: if (dice.OrderBy(s => s).Distinct().First() == 1 &&
                                               dice.Distinct().Count() == 5 && dice.OrderBy(s => s).Distinct().Last() == 5)
            {
                return(30);
            }
            else
            {
                return(0);
            }

        case YachtCategory.BigStraight: if (dice.OrderBy(s => s).Distinct().First() == 2 &&
                                            dice.OrderBy(s => s).Distinct().Count() == 5 && dice.OrderBy(s => s).Distinct().Last() == 6)
            {
                return(30);
            }
            else
            {
                return(0);
            }

        case YachtCategory.Choice: return(dice.Sum());

        case YachtCategory.Yacht: return((dice.Distinct().Count() == 1) ? 50 : 0);

        default: return(-1);
        }
    }
    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);
        }
    }
Exemple #7
0
    public static int Score(int[] dice, YachtCategory category)
    {
        switch (category)
        {
        case YachtCategory.Ones:
            return(OnesScore(dice));

        case YachtCategory.Twos: return(TwosScore(dice));

        case YachtCategory.Threes:  return(ThreesScore(dice));

        case YachtCategory.Fours:  return(FoursScore(dice));

        case YachtCategory.Fives:  return(FivesScore(dice));

        case YachtCategory.Sixes:  return(SixesScore(dice));

        case YachtCategory.Choice:  return(ChoiceScore(dice));

        case YachtCategory.FourOfAKind:  return(FourOfKindScore(dice));

        case YachtCategory.FullHouse:  return(FullHouseScore(dice));

        case YachtCategory.LittleStraight:  return(LittleStraightScore(dice));

        case YachtCategory.BigStraight:  return(BigStraightScore(dice));

        case YachtCategory.Yacht:  return(YachtScore(dice));

        default: throw new Exception("You need to implement this function.");
        }
        ;
    }
Exemple #8
0
    public static int Score(int[] dice, YachtCategory category)
    {
        var grouped = dice.GroupBy(d => d).OrderByDescending(d => d.Count());

        switch (category)
        {
        case YachtCategory.Yacht:
            if (grouped.Count() == 1 && grouped.First().Count() == 5)
            {
                return(50);
            }
            break;

        case YachtCategory.Ones:
        case YachtCategory.Twos:
        case YachtCategory.Threes:
        case YachtCategory.Fours:
        case YachtCategory.Fives:
        case YachtCategory.Sixes:
            return(dice.Where(d => d == (int)category).Sum());

        case YachtCategory.FullHouse:
            if (grouped.Count() == 2 && grouped.First().Count() == 3)
            {
                return(dice.Sum());
            }
            break;

        case YachtCategory.FourOfAKind:
            if (grouped.Count() <= 2 && grouped.First().Count() >= 4)
            {
                return(grouped.First().Key * 4);
            }
            break;

        case YachtCategory.LittleStraight:
            if (dice.OrderBy(d => d).SequenceEqual(new[] { 1, 2, 3, 4, 5 }))
            {
                return(30);
            }
            break;

        case YachtCategory.BigStraight:
            if (dice.OrderBy(d => d).SequenceEqual(new[] { 2, 3, 4, 5, 6 }))
            {
                return(30);
            }
            break;

        case YachtCategory.Choice:
            return(dice.Sum());
        }

        return(0);
    }
Exemple #9
0
    public static int Score(int[] dice, YachtCategory category)
    {
        var occurences = dice.GroupBy(d => d).ToDictionary(group => group.Key, group => group.Count());

        switch (category)
        {
        case YachtCategory.Ones:
        case YachtCategory.Twos:
        case YachtCategory.Threes:
        case YachtCategory.Fours:
        case YachtCategory.Fives:
        case YachtCategory.Sixes:
            return(dice.Where(d => d == (int)category).Sum());

        case YachtCategory.FullHouse when
            occurences.Keys.Count == 2 &&
            occurences.Values.All(o => o == 2 || o == 3):
            return(dice.Sum());

        case YachtCategory.FourOfAKind when
            occurences.Values.Any(o => o >= 4):
            return(occurences.Single(kvp => kvp.Value >= 4).Key * 4);

        case YachtCategory.LittleStraight when
            occurences.Values.All(o => o == 1) &&
            occurences.ContainsKey(1) &&
            occurences.ContainsKey(5):
            return(30);

        case YachtCategory.BigStraight when
            occurences.Values.All(o => o == 1) &&
            occurences.ContainsKey(2) &&
            occurences.ContainsKey(6):
            return(30);

        case YachtCategory.Choice:
            return(dice.Sum());

        case YachtCategory.Yacht when
            dice.All(o => o == dice[0]):
            return(50);

        default:
            return(0);
        }
    }
Exemple #10
0
    public static int Score(int[] dice, YachtCategory category)
    {
        int score;

        if ((int)category <= 6)
        {
            score = ((int)category * dice.Where(num => num == (int)category).Count());
        }
        else
        {
            switch (category)
            {
            case YachtCategory.FullHouse:
                var pairOrThreeOfKind = dice.Select(x => new { Integer = x, Count = dice.Count(y => y == x) });
                score = pairOrThreeOfKind.All(x => x.Count == 2 || x.Count == 3) ? pairOrThreeOfKind.Sum(x => x.Integer) : 0;
                break;

            case YachtCategory.FourOfAKind:
                score = dice.Select((x) => new { Integer = x, Count = dice.Count(y => y == x) }).Where(x => x.Count >= 4).Take(4).Sum(x => x.Integer);
                break;

            case YachtCategory.LittleStraight:
                score = dice.OrderBy(x => x).SequenceEqual(new[] { 1, 2, 3, 4, 5 }) ? 30 : 0;
                break;

            case YachtCategory.BigStraight:
                score = dice.OrderBy(x => x).SequenceEqual(new[] { 2, 3, 4, 5, 6 }) ? 30 : 0;
                break;

            case YachtCategory.Choice:
                score = dice.Sum();
                break;

            case YachtCategory.Yacht:
                score = dice.Distinct().Count() == 1 ? 50 : 0;
                break;

            default:
                score = 0;
                break;
            }
        }

        return(score);
    }
Exemple #11
0
    public static int Score(int[] dice, YachtCategory category)
    {
        switch (category)
        {
        case YachtCategory.Ones:
            return(ScoreSingleDice(dice, 1));

        case YachtCategory.Twos:
            return(ScoreSingleDice(dice, 2));

        case YachtCategory.Threes:
            return(ScoreSingleDice(dice, 3));

        case YachtCategory.Fours:
            return(ScoreSingleDice(dice, 4));

        case YachtCategory.Fives:
            return(ScoreSingleDice(dice, 5));

        case YachtCategory.Sixes:
            return(ScoreSingleDice(dice, 6));

        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), "Invalid category");
        }
    }
    public static int Score(int[] dice, YachtCategory category)
    {
        string counts = String.Join(",", from grp in dice.Grouped() let c = grp.Count() orderby c select c.ToString());

        bool isStraight() => dice.Sorted().SequenceEqual(Enumerable.Range((int)category - 8, 5));

        switch (category)
        {
        case YachtCategory.FullHouse:
            if (counts == "2,3")
            {
                return(dice.Sum());
            }
            break;

        case YachtCategory.FourOfAKind:
            switch (counts)
            {
            case "1,4":
            case "5":
                return(4 * dice.Grouped().First(g => g.Count() > 1).Key);
            }
            break;

        case YachtCategory.LittleStraight:
        case YachtCategory.BigStraight:
            if (isStraight())
            {
                return(30);
            }
            break;

        case YachtCategory.Choice: return(dice.Sum());

        case YachtCategory.Yacht:
            if (dice.Distinct().Count() == 1)
            {
                return(50);
            }
            break;
        }
        return(dice.SumWhere(d => d == (int)category));
    }
Exemple #13
0
 public static int Score(int[] dice, YachtCategory category)
 => methods[category].Invoke(dice);
Exemple #14
0
 public static int Score(int[] dice, YachtCategory category)
 {
     throw new NotImplementedException("You need to implement this function.");
 }
Exemple #15
0
    public static int Score(int[] dice, YachtCategory category)
    {
        var numbFound = new Dictionary <int, int>();

        var it = dice.Length;



        var diceFaces = new Dictionary <int, int>();

        foreach (var item in dice)
        {
            if (diceFaces.ContainsKey(item))
            {
                diceFaces[item]++;
            }
            else
            {
                diceFaces[item] = 1;
            }
        }

        var pointYacht = 0;

        if (YachtCategory.Yacht == category)
        {
            if (diceFaces.Values.Contains(5))
            {
                pointYacht = 50;
            }

            return(pointYacht);
        }
        if (YachtCategory.Ones == category)
        {
            var pointOnes = 0;
            foreach (var key in diceFaces.Keys)
            {
                if (key == 1)
                {
                    pointOnes = diceFaces[key];
                }
            }

            return(pointOnes);
        }
        if (YachtCategory.Twos == category)
        {
            var pointTwos = 0;
            foreach (var key in diceFaces.Keys)
            {
                if (key == 2)
                {
                    pointTwos = 2 * diceFaces[key];
                }
            }

            return(pointTwos);
        }

        if (YachtCategory.Fours == category)
        {
            var pointFours = 0;
            foreach (var key in diceFaces.Keys)
            {
                if (key == 4)
                {
                    pointFours = 4 * diceFaces[key];
                }
            }

            return(pointFours);
        }

        if (YachtCategory.Threes == category)
        {
            var pointThrees = 0;
            foreach (var key in diceFaces.Keys)
            {
                if (key == 3)
                {
                    pointThrees = 3 * diceFaces[key];
                }
            }

            return(pointThrees);
        }

        if (YachtCategory.Fives == category)
        {
            var pointFives = 0;
            foreach (var key in diceFaces.Keys)
            {
                if (key == 5)
                {
                    pointFives = 5 * diceFaces[key];
                }
            }

            return(pointFives);
        }
        if (YachtCategory.Sixes == category)
        {
            var pointSixes = 0;
            foreach (var key in diceFaces.Keys)
            {
                if (key == 6)
                {
                    pointSixes = 6 * diceFaces[key];
                }
            }

            return(pointSixes);
        }
        if (YachtCategory.FullHouse == category)
        {
            var pointInFullHouse = 0;

            if (diceFaces.Count == 2)
            {
                foreach (var key in diceFaces)
                {
                    if (key.Value == 2 || key.Value == 3)
                    {
                        pointInFullHouse += key.Value * key.Key;
                    }
                }
            }
            return(pointInFullHouse);
        }
        if (YachtCategory.FourOfAKind == category)
        {
            var pointInFourOfAKind = 0;

            if (diceFaces.Count == 2 || diceFaces.Count == 1)
            {
                foreach (var key in diceFaces)
                {
                    if (key.Value == 4)
                    {
                        pointInFourOfAKind = key.Value * key.Key;
                    }
                    if (key.Value == 5)
                    {
                        pointInFourOfAKind = 4 * key.Key;
                    }
                }
            }
            return(pointInFourOfAKind);
        }
        if (YachtCategory.LittleStraight == category)
        {
            var orderedDiceList = dice.OrderByDescending(c => c).ToArray().Reverse();

            var firstDiceOfArray = dice.OrderByDescending(c => c).ToArray().Reverse().First();

            int i = 0;

            foreach (var item in orderedDiceList)
            {
                if (item == firstDiceOfArray + i)
                {
                    i++;
                }
            }

            if (firstDiceOfArray == 1 && i == 5)
            {
                return(30);
            }
            else
            {
                return(0);
            }
        }
        if (YachtCategory.BigStraight == category)
        {
            var orderedDiceList = dice.OrderByDescending(c => c).ToArray().Reverse();

            var firstDiceOfArray = dice.OrderByDescending(c => c).ToArray().Reverse().First();

            int i = 0;

            foreach (var item in orderedDiceList)
            {
                if (item == firstDiceOfArray + i)
                {
                    i++;
                }
            }
            if (firstDiceOfArray == 2 && i == 5)
            {
                return(30);
            }
            else
            {
                return(0);
            }
        }
        if (YachtCategory.Choice == category)
        {
            var pointsChoice = 0;

            foreach (var item in dice)
            {
                pointsChoice += item;
            }

            return(pointsChoice);
        }

        return(-1);
    }
Exemple #16
0
 private static int IsAnyCombinationOf(this int[] dice, YachtCategory yachtCategory)
 => dice.Where(value => value == (int)yachtCategory)
 .Sum();
    public static int Score(int[] dice, YachtCategory category)
    {
        switch (category)
        {
        //Ones: Any combination. The sum of dice with the number 1.
        case YachtCategory.Ones: return(dice.Where(x => x == 1).Sum());

        //Twos: Any combination. The sum of dice with the number 2.
        case YachtCategory.Twos: return(dice.Where(x => x == 2).Sum());

        //Threes: Any combination. The sum of dice with the number 3.
        case YachtCategory.Threes: return(dice.Where(x => x == 3).Sum());

        //Fours: Any combination The sum of dice with the number 4.
        case YachtCategory.Fours: return(dice.Where(x => x == 4).Sum());

        //Fives: Any combination. The sum of dice with the number 5.
        case YachtCategory.Fives: return(dice.Where(x => x == 5).Sum());

        //Sixes: Any combination. The sum of dice with the number 6.
        case YachtCategory.Sixes: return(dice.Where(x => x == 6).Sum());

        //Full House:  Three of one number and two of another.  Sum of all dice.
        case YachtCategory.FullHouse:
        {
            var testHashSet = new HashSet <int>();
            dice.ToList().ForEach(x => testHashSet.Add(x));
            return((testHashSet.Count() == 2) ? dice.Sum() : 0);
        }

        //Four-Of-A-Kind:  At least four dice showing the same face.   Sum of those four dice.
        case YachtCategory.FourOfAKind:
            var testDict = new Dictionary <int, int>();
            dice.ToList().ForEach(x =>
            {
                if (!testDict.ContainsKey(x))
                {
                    testDict.Add(x, 1);
                }
                else
                {
                    testDict[x]++;
                }
            });
            if (testDict.Count > 2)
            {
                return(0);
            }
            else if (testDict.Count == 1 || testDict.ElementAt(0).Value >= 4)
            {
                return(testDict.Keys.ElementAt(0) * 4);
            }
            else if (testDict.ElementAt(1).Value >= 4)
            {
                return(testDict.Keys.ElementAt(1) * 4);
            }
            else
            {
                return(0);
            }

        //Little Straight: 1 - 2 - 3 - 4 - 5. Score varies. Often 30.
        case YachtCategory.LittleStraight:
            return(dice.OrderBy(x => x).SequenceEqual(new[] { 1, 2, 3, 4, 5 }) ? 30 : 0);

        //Big Straight:    2 - 3 - 4 - 5 - 6. Score varies. Often 30.
        case YachtCategory.BigStraight:
            return(dice.OrderBy(x => x).SequenceEqual(new[] { 2, 3, 4, 5, 6 }) ? 30 : 0);

        //Choice: Any combination. Sum of all dice Dice.
        case YachtCategory.Choice: return(dice.Sum());

        //Yacht: All five dice showing the same face - scores 50.
        case YachtCategory.Yacht:
            return(dice.Distinct().Count() == 1 ? 50 : 0);

        default: return(0);
        }
    }
Exemple #18
0
 public static int Score(int[] dice, YachtCategory category) => scoresByCategory[category](dice);