示例#1
0
        public void AddPoints(Category category)
        {
            if (_gameStatus[ActivePlayer][category] != null)
            {
                throw new ArgumentException($"Category {category} already taken! Choose other category.");
            }

            int[] rollResult = RollResult.Select(x => x.Result).ToArray();
            var   scorer     = ScorerFactory.GetScorer(_hasYahtzee[ActivePlayer]);

            _gameStatus[ActivePlayer][category] = scorer.CalculateCategoryScore(category, rollResult);

            CalculateScore();

            if (!_hasYahtzee[ActivePlayer] && _gameStatus[ActivePlayer][Category.Yahtzee] == 50)
            {
                _hasYahtzee[ActivePlayer] = true;
            }
            else if (_hasYahtzee[ActivePlayer] && new RegularScorer().CalculateCategoryScore(Category.Yahtzee, rollResult) == 50)
            {
                _gameStatus[ActivePlayer][Category.Yahtzee] += 100;
            }

            ActivePlayer++;
            RollsLeft = 3;

            if (ActivePlayer > Players.Length - 1)
            {
                ActivePlayer = 0;
            }
        }
示例#2
0
        private void CalculateScore()
        {
            var scorer = ScorerFactory.GetScorer(_hasYahtzee[ActivePlayer]);

            PartialScore[ActivePlayer] = scorer.CalculatePartialScore(_gameStatus[ActivePlayer]);
            BonusScore[ActivePlayer]   = scorer.CalculateBonusScore(_gameStatus[ActivePlayer]);
            TotalScore[ActivePlayer]   = scorer.CalculateTotalScore(_gameStatus[ActivePlayer]);
            GetWinner();
        }
示例#3
0
        public Dictionary <Category, int?> GetAvailableCategories()
        {
            var scores = new Dictionary <Category, int?>();
            var scorer = ScorerFactory.GetScorer(_hasYahtzee[ActivePlayer]);

            foreach (Category category in Enum.GetValues(typeof(Category)))
            {
                if (_gameStatus[ActivePlayer][category] == null)
                {
                    int[] rollResult   = RollResult.Select(x => x.Result).ToArray();
                    var   scorerResult = scorer.CalculateCategoryScore(category, rollResult);

                    scores.Add(category, scorerResult);
                }
                else
                {
                    scores.Add(category, null);
                }
            }

            if (_hasYahtzee[ActivePlayer] && scores.Take(6).Any(x => x.Value > 0))
            {
                Category?yahtzeeCategory = null;
                var      results         = scores.Take(6);
                foreach (var item in results)
                {
                    if (item.Value != null && item.Value > 0)
                    {
                        yahtzeeCategory = item.Key;
                    }
                }

                scores.Clear();
                foreach (Category category in Enum.GetValues(typeof(Category)))
                {
                    if (yahtzeeCategory != null && category == yahtzeeCategory)
                    {
                        int[] rollResult   = RollResult.Select(x => x.Result).ToArray();
                        var   scorerResult = scorer.CalculateCategoryScore(category, rollResult);

                        scores.Add(category, scorerResult);
                    }
                    else
                    {
                        scores.Add(category, null);
                    }
                }
            }

            return(scores);
        }