Пример #1
0
        /// <summary>
        /// Calculate the
        /// </summary>
        /// <param name="dice"></param>
        /// <param name="stepType"></param>
        /// <returns></returns>
        private byte CalculateDiceScore(int[] dice, YachtCombination stepType)
        {
            // Get the first and the last dice for calculation.
            int first = dice[0];
            int last  = dice[dice.Length - 1];

            switch (stepType)
            {
            case YachtCombination.Yacht:
                // If all dice are same.
                if (Times(dice, first) == 5)
                {
                    return(50);
                }
                return(0);

            case YachtCombination.LargeStraight:
                // Dice is 2-6.
                if (FollowingDice(dice) && last == 6)
                {
                    return(30);
                }
                return(0);

            case YachtCombination.SmallStraight:
                // Dice is 1-5.
                if (FollowingDice(dice) && last == 5)
                {
                    return(30);
                }
                return(0);

            case YachtCombination.FourOfAKind:
                // 4 Dice are identical.
                if (Times(dice, first) >= 4 || Times(dice, last) >= 4)
                {
                    return(Sum(dice, null));
                }
                return(0);

            case YachtCombination.FullHouse:
                // 2 dice of the same value and 3 dice on the save value but different value from the other two.
                if ((Times(dice, first) == 3 && Times(dice, last) == 2) ||
                    (Times(dice, first) == 2 && Times(dice, last) == 3))
                {
                    return(Sum(dice, null));
                }
                return(0);

            case YachtCombination.Choise:
                return(Sum(dice, null));

            case YachtCombination.Sixes:
            case YachtCombination.Fives:
            case YachtCombination.Fours:
            case YachtCombination.Threes:
            case YachtCombination.Twos:
            case YachtCombination.Ones:
                // Calculate the some of the current value.
                return(Sum(dice, (int)stepType));

            default:
                throw new Exception("Cannot calculate Value for this dice");
            }
        }
Пример #2
0
        /// <summary>
        /// Calculate the score of the supplied dice according to a specified combination.
        /// </summary>
        /// <param name="combination">The score combination to consider when calculating the dice score.</param>
        /// <param name="dice">An array of five dice. Some of the array members may be null.</param>
        /// <returns>The combination score for the supplied dice.</returns>
        public static byte CombinationScore(YachtCombination combination, Dice[] dice)
        {
            // Make sure the five dice are supplied
            if (dice.Length != 5)
            {
                throw new ArgumentException("The array must contain five members", "dice");
            }

            // Sort the dice.
            dice = (Dice[])dice.Clone();
            Array.Sort(dice);

            // Get the first and the last dice for calculation.
            Dice first = First(dice);
            Dice last  = Last(dice);

            switch (combination)
            {
            case YachtCombination.Yacht:
                // If all dice are the same
                if (first != null && last != null)
                {
                    if (Times(dice, first.Value) == 5)
                    {
                        return(50);
                    }
                }
                return(0);

            case YachtCombination.LargeStraight:
                // Dice are 2-6
                if (first != null && last != null)
                {
                    if (CheckConsecutiveDice(dice) && last.Value == DiceValue.Six)
                    {
                        return(30);
                    }
                }
                return(0);

            case YachtCombination.SmallStraight:
                // Dice are 1-5
                if (first != null && last != null)
                {
                    if (CheckConsecutiveDice(dice) && last.Value == DiceValue.Five)
                    {
                        return(30);
                    }
                }
                return(0);

            case YachtCombination.FourOfAKind:
                // 4 dice are identical
                if (first != null && last != null)
                {
                    if (Times(dice, first.Value) >= 4 || Times(dice, last.Value) >= 4)
                    {
                        return(Sum(dice, null));
                    }
                }
                return(0);

            case YachtCombination.FullHouse:
                // There is a pair of identical dice, and all other dice have an identical yet different value
                if (first != null && last != null)
                {
                    if ((Times(dice, first.Value) == 3 && Times(dice, last.Value) == 2) ||
                        (Times(dice, first.Value) == 2 && Times(dice, last.Value) == 3))
                    {
                        return(Sum(dice, null));
                    }
                }
                return(0);

            case YachtCombination.Choise:
                return(Sum(dice, null));

            case YachtCombination.Sixes:
            case YachtCombination.Fives:
            case YachtCombination.Fours:
            case YachtCombination.Threes:
            case YachtCombination.Twos:
            case YachtCombination.Ones:
                // Calculate the sum of the selected die faces.
                return(Sum(dice, (DiceValue)(int)combination));

            default:
                throw new Exception("Cannot calculate Value for this combination.");
            }
        }