private bool UpdateUpperSection(List <Die> dice, int targetValue, Cat catToUpdate)
        {
            int score = 0;

            foreach (Die die in dice)
            {
                if (die.Value == targetValue)
                {
                    score += targetValue;
                }
            }

            var iterableCats = GetCategories();

            foreach (Category cat in iterableCats)
            {
                if ((cat.CatType == catToUpdate) && !cat.IsUsed)
                {
                    cat.Score = score;

                    if (CheckUpperBonus() && !cat_UpperBonus.IsUsed)
                    {
                        cat_UpperBonus.Score = 35;
                    }

                    return(true);
                }
            }

            return(false);
        }
Esempio n. 2
0
        public void ComputerPlaysRound()
        {
            ResetRollsLeft();

            while (RollsLeft > 0)
            {
                RollDice();

                Cat chosenCat = m_playStrategy.Use(this, RollsLeft);

                if (chosenCat != Cat.NoCategory)
                {
                    if (chosenCat == Cat.YahtzeeBonus)
                    {
                        Cat chosenBonusCat = m_playStrategy.UseYahtzeeBonus(this);
                        ScoreCard.UpdateYahtzeeBonus(Dice, chosenBonusCat);

                        RollsLeft = 0;
                        UnholdAllDice();
                    }
                    else if (ScoreCard.Update(Dice, chosenCat))
                    {
                        RollsLeft = 0;
                        UnholdAllDice();
                    }
                }
            }
        }
        public bool UpdateYahtzeeBonus(List <Die> dice, Cat chosenCat)
        {
            if (IsBonusYahtzee(dice))
            {
                var iterableCats = GetCategories();

                foreach (Category cat in iterableCats)
                {
                    if ((cat.CatType == chosenCat) && !cat.IsUsed)
                    {
                        if ((chosenCat == Cat.FullHouse || chosenCat == Cat.Small ||
                             chosenCat == Cat.Large) && !cat.IsUsed)
                        {
                            switch (chosenCat)
                            {
                            /// FIX BUGG HERE - YAHTZEE BONUS DOES NOT TALLY IN TOTAL SCORE
                            ///     PERHAPS BECAUSE I MADE CAT.SCORE A NULLABLE<INT> ????????????
                            case Cat.FullHouse:
                                cat_FullHouse.Score     = 25;
                                cat_YahtzeeBonus.Score += 100;
                                return(true);

                            case Cat.Small:
                                cat_Small.Score         = 30;
                                cat_YahtzeeBonus.Score += 100;
                                return(true);

                            case Cat.Large:
                                cat_Large.Score         = 40;
                                cat_YahtzeeBonus.Score += 100;
                                return(true);

                            default:
                                return(false);
                            }
                        }
                        else
                        {
                            if (Update(dice, chosenCat))
                            {
                                cat_YahtzeeBonus.Score += 100;
                                return(true);
                            }
                        }
                    }
                }

                return(false);
            }
            else
            {
                return(false);
            }
        }
        public bool IsUsed(Cat queriedCatType)
        {
            var  cats   = GetCategories();
            bool isUsed = false;

            foreach (Category c in cats)
            {
                if (c.CatType == queriedCatType)
                {
                    isUsed = c.IsUsed;
                }
            }

            return(isUsed);
        }
        public bool Update(List <Die> dice, Cat category)
        {
            switch (category)
            {
            case Cat.Ones:
                return(UpdateUpperSection(dice, 1, Cat.Ones));

            case Cat.Twos:
                return(UpdateUpperSection(dice, 2, Cat.Twos));

            case Cat.Threes:
                return(UpdateUpperSection(dice, 3, Cat.Threes));

            case Cat.Fours:
                return(UpdateUpperSection(dice, 4, Cat.Fours));

            case Cat.Fives:
                return(UpdateUpperSection(dice, 5, Cat.Fives));

            case Cat.Sixes:
                return(UpdateUpperSection(dice, 6, Cat.Sixes));

            case Cat.x3:
                return(UpdateX3(dice));

            case Cat.x4:
                return(UpdateX4(dice));

            case Cat.FullHouse:
                return(UpdateFullHouse(dice));

            case Cat.Small:
                return(UpdateSmall(dice));

            case Cat.Large:
                return(UpdateLarge(dice));

            case Cat.Yahtzee:
                return(UpdateYahtzee(dice));

            case Cat.Chance:
                return(UpdateChance(dice));

            default:
                // no exception or message here?
                return(false);
            }
        }
Esempio n. 6
0
        /// returns a Category.Type which informs Player which category to use on
        /// their scorecard. NoCategory is returned when Player should roll again
        public Cat Use(Player player, int rollsLeft)
        {
            var scoreCard = player.ScoreCard;
            var dice      = player.Dice;

            UnholdAllDice(player);

            // CHECK FOR BONUS YAHTZEE
            if (scoreCard.IsBonusYahtzee(dice))
            {
                return(Cat.YahtzeeBonus);
            }

            // CHECK FOR YAHTZEE
            if (scoreCard.IsYahtzee(dice))
            {
                return(Cat.Yahtzee);
            }

            // CHECK FOR LARGE SEQUENCE
            if (scoreCard.IsSequence(dice, 5) && !scoreCard.IsUsed(Cat.Large))
            {
                return(Cat.Large);
            }

            if (rollsLeft == 0)
            {
                // CHECK FOR SMALL SEQUENCE
                if (scoreCard.IsSequence(dice, 4) && !scoreCard.IsUsed(Cat.Small))
                {
                    return(Cat.Small);
                }

                // CHECK FOR 3 OR 4 OF A KIND
                if (scoreCard.IsThreeOfAKind(dice) || scoreCard.IsFourOfAKind(dice))
                {
                    // USE IN UPPER SECTION IF UNUSED
                    Cat upperCat = UpdateUpperSection(scoreCard, dice);
                    if (upperCat != Cat.NoCategory)
                    {
                        return(upperCat);
                    }

                    // CHECK FOR EMPTY 4 OF A KIND CATEGORY
                    if (scoreCard.IsFourOfAKind(dice) && !scoreCard.IsUsed(Cat.x4))
                    {
                        return(Cat.x4);
                    }

                    // CHECK FOR EMPTY FULL HOUSE CATEGORY
                    if (scoreCard.IsFullHouse(dice) && !scoreCard.IsUsed(Cat.FullHouse))
                    {
                        return(Cat.FullHouse);
                    }

                    // CHECK FOR EMPTY 3 OF A KIND CATEGORY
                    if (scoreCard.IsThreeOfAKind(dice) && !scoreCard.IsUsed(Cat.x3))
                    {
                        return(Cat.x3);
                    }
                }

                // CHECK FOR EMPTY CHANCE CATEGORY
                if (!scoreCard.IsUsed(Cat.Chance))
                {
                    return(Cat.Chance);
                }

                // AS A LAST RESORT - USE THE FIRST EMPTY CATEGORY FOUND ON SCORECARD
                Cat firstUnusedCat = GetFirstUnusedCat(player);
                return(firstUnusedCat);
            }
            // ELSE IF ROLLSLEFT > 0
            else
            {
                // IF 3 OR 4 OF A KIND - HOLD THEM AND ROLL AGAIN
                if (scoreCard.IsThreeOfAKind(dice) || scoreCard.IsFourOfAKind(dice))
                {
                    int   commonValue;
                    int[] values = new int [5];

                    for (int i = 0; i < values.Count(); i++)
                    {
                        values[i] = dice[i].Value;
                    }

                    commonValue = values.GroupBy(v => v)
                                  .OrderByDescending(g => g.Count())
                                  .First()
                                  .Key;

                    foreach (Die d in dice)
                    {
                        if (d.Value == commonValue)
                        {
                            d.IsHeld = true;
                        }
                    }
                }

                // IF SMALL SEQUENCE EXISTS...
                if (scoreCard.IsSequence(dice, 4))
                {
                    // ...AND LARGE CAT NOT USED - ROLL FOR IT
                    if (!scoreCard.IsUsed(Cat.Large))
                    {
                        HoldSequenceValues(dice, 4);
                        return(Cat.NoCategory);
                    }
                    // ...AND IS NOT USED BUT LARGE IS USED - USE SMALL
                    else if (!scoreCard.IsUsed(Cat.Small))
                    {
                        return(Cat.Small);
                    }
                }

                // IF 3 IN A ROW ON FIRST GO AND SMALL NOT USED
                if (scoreCard.IsSequence(dice, 3) &&
                    rollsLeft == 2 &&
                    !scoreCard.IsUsed(Cat.Small))
                {
                    HoldSequenceValues(dice, 3);
                    return(Cat.NoCategory);
                }

                // IF 2 IN ROW EXISTS
                List <int> pairs = FindTwoOfAKind(dice);
                foreach (int pairValue in pairs)
                {
                    var iterableCats = player.ScoreCard.GetCategories();

                    foreach (Category c in iterableCats)
                    {
                        if ((pairValue == c.UpperValue) && !c.IsUsed)
                        {
                            foreach (Die d in dice)
                            {
                                if (d.Value == pairValue)
                                {
                                    d.IsHeld = true;
                                }
                            }
                            return(Cat.NoCategory);
                        }
                    }
                }
            }

            return(Cat.NoCategory);
        }