Exemple #1
0
        /// <summary>
        /// Does the actual scoring based on standard 10 frame bowling rules
        /// First 9 frames are scored base on two rolls;
        ///     if the first roll = 10 (strike) scoring is effected by the next two rolls that contains a value (0-10)
        ///     if the sum of both rolls = 10 (spare) score is effected by the next roll value
        ///     all other rolls score will just add rolls sum to the total score
        /// The 10th frame can have up to 3 rolls if the sum of the first two meets or exceeds 10.
        /// </summary>
        /// <param name="frameToScore">The selected frame to adjust it's total score on</param>
        private void CalculateNewScore(FrameScore frameToScore)
        {
            int index = frameToScore.FrameNumber - 1;
            int previousFrameScore = (index > 0) ? _currentGame.Frames[index - 1].ScoreTotal : 0;

            //no bonus in 10th frame
            if (frameToScore.FrameNumber < 10)
            {
                switch (frameToScore.BonusType)
                {
                case BonusType.NoBonus:
                    frameToScore.ScoreTotal = previousFrameScore + frameToScore.Rolls.Sum();
                    break;

                case BonusType.Spare:
                    bool hasBonusToApply = (_currentGame.Frames.Count > index + 1);
                    frameToScore.ScoreTotal  = hasBonusToApply ? _currentGame.Frames[index + 1].Rolls.Take(1).Sum() : 0;
                    frameToScore.ScoreTotal += previousFrameScore + frameToScore.Rolls.Sum();
                    break;

                case BonusType.Strike:
                    bool hasBonusToApplyFromFirstFrame  = (_currentGame.Frames.Count > index + 1);
                    bool hasBonusToApplyFromSecondFrame = (_currentGame.Frames.Count > index + 2) &&
                                                          _currentGame.Frames[index + 1].BonusType == BonusType.Strike;
                    frameToScore.ScoreTotal  = hasBonusToApplyFromFirstFrame ? _currentGame.Frames[index + 1].Rolls.Take(2).Sum() : 0;
                    frameToScore.ScoreTotal += hasBonusToApplyFromSecondFrame ? _currentGame.Frames[index + 2].Rolls.Take(1).Sum() : 0;
                    frameToScore.ScoreTotal += previousFrameScore + frameToScore.Rolls.Sum();
                    break;
                }
            }
            else
            {
                frameToScore.ScoreTotal = previousFrameScore + frameToScore.Rolls.Sum();
            }
        }
        public void bowling_a_strike_2_frames_in_a_row_will_add_the_first_roll_of_the_next_frame_to_the_first_strikes_frame()
        {
            var game = new FrameScorer()
            {
                CurrentFrame = 2, RemainingPins = 5
            };
            var frame1 = new FrameScore()
            {
                Bowl1 = 10, Strike = true
            };
            var frame2 = new FrameScore()
            {
                Bowl1 = 10, Strike = true
            };
            var frame3 = new FrameScore()
            {
                Bowl1 = 5, Bowl2 = 3
            };
            var expectedResult = 25;

            game.FrameList.Add(frame1);
            game.FrameList.Add(frame2);
            game.FrameList.Add(frame3);
            game.StrikeAdder();
            var actualResult = frame1.TotalScore;

            Assert.Equal(expectedResult, actualResult);
        }
Exemple #3
0
        /// <summary>
        /// Validation of roll
        ///     roll must be between 0-10
        ///     in first 9 frames total of both rolls cannot exceed 10
        ///     in the 10th frame if the first roll is not 10 then the total of the first two roll cannot exceed 10
        /// </summary>
        /// <param name="roll">roll value being entered</param>
        /// <param name="currentFrame">Frame where the roll is to be applied</param>
        /// <returns>if passes validation will return true (default is false)</returns>
        private static bool ValidateRoll(int roll, FrameScore currentFrame)
        {
            bool validateRoll = false;

            if ((roll >= 0) && (roll <= 10))
            {
                if (currentFrame.FrameNumber == 10)
                {
                    validateRoll = currentFrame.Rolls.Take(1).Sum() == 10 || roll + currentFrame.Rolls.Take(1).Sum() <= 10;
                }
                else if (roll + currentFrame.Rolls.Sum() <= 10)
                {
                    validateRoll = true;
                }
            }

            return(validateRoll);
        }
Exemple #4
0
        private static void TrimData(List <string> dataPoints, List <int[]> frames, List <FrameScore> ballThrowList)
        {
            foreach (var var in dataPoints)
            {
                string   trimmed = var.TrimStart('[').TrimEnd(']');
                string[] split   = trimmed.Split(',');
                int[]    myInts  = Array.ConvertAll(split, s => int.Parse(s));
                frames.Add(myInts);
            }

            foreach (var var in frames)
            {
                FrameScore thrown = new FrameScore();
                thrown.FirstThrow  = var[0];
                thrown.SecondThrow = var[1];
                ballThrowList.Add(thrown);
            }
        }
Exemple #5
0
        /// <summary>
        /// Determine if the game is in the final 10th frame and all rolls have been completed.
        /// </summary>
        /// <param name="currentFrame"></param>
        /// <returns></returns>
        private bool GameIsComplete(FrameScore currentFrame)
        {
            bool result = false;

            if (currentFrame.FrameNumber == 10)
            {
                //can be up to 3 rolls if strike or spare in first 2 rolls
                if (currentFrame.Rolls.Count == 3)
                {
                    result = true;
                }
                else if (currentFrame.Rolls.Count > 1 && currentFrame.Rolls.Sum() < 10)
                {
                    result = true;
                }
            }

            return(result);
        }
Exemple #6
0
        private int CalculateBonusScore(int frameNumber, FrameScore frameScore)
        {
            var bonusScore          = 0;
            var bonusRollsRemaining = frameScore.BonusRolls;
            var bonusRollFrame      = frameNumber + 1;

            while (bonusRollsRemaining > 0)
            {
                foreach (var roll in Frames[bonusRollFrame].Rolls)
                {
                    if (bonusRollsRemaining > 0)
                    {
                        bonusScore += roll.Pins;
                        bonusRollsRemaining--;
                    }
                }
                bonusRollFrame += 1;
            }
            return(bonusScore);
        }
Exemple #7
0
        private FrameScore ScoreFrame(Frame frame)
        {
            var frameScore = new FrameScore();

            foreach (var roll in frame.Rolls)
            {
                frameScore.Score += roll.Pins;
            }

            if (frameScore.Score == 10)
            {
                if (frame.Rolls.Count == 1)
                {
                    frameScore.BonusRolls = 2;
                }
                else if (frame.Rolls.Count == 2)
                {
                    frameScore.BonusRolls = 1;
                }
            }

            return(frameScore);
        }
        public void bowling_a_spare_adds_the_next_bowl_to_the_spare_frame()
        {
            var game = new FrameScorer()
            {
                CurrentFrame = 1, RemainingPins = 2
            };
            var frame1 = new FrameScore()
            {
                Bowl1 = 4, Bowl2 = 6, Spare = true
            };
            var frame2 = new FrameScore()
            {
                Bowl1 = 5, Bowl2 = 3
            };
            var expectedResult = 15;

            game.FrameList.Add(frame1);
            game.FrameList.Add(frame2);
            game.SpareAdder();
            var actualResult = frame1.TotalScore;

            Assert.Equal(expectedResult, actualResult);
        }
        public void bowling_a_strike_followed_by_a_non_strike_will_add_both_bowls_from_the_second_frame_to_the_strike_frame()
        {
            var game = new FrameScorer()
            {
                CurrentFrame = 1, RemainingPins = 2
            };
            var frame1 = new FrameScore()
            {
                Bowl1 = 10, Strike = true
            };
            var frame2 = new FrameScore()
            {
                Bowl1 = 5, Bowl2 = 3
            };
            var expectedResult = 18;

            game.FrameList.Add(frame1);
            game.FrameList.Add(frame2);
            game.StrikeAdder();
            var actualResult = frame1.TotalScore;

            Assert.Equal(expectedResult, actualResult);
        }
Exemple #10
0
        /// <summary>
        /// Calculates the frame score and current total score based on input received in querystring
        /// </summary>
        /// <param name="firstRollPinsCount">Pins down in first roll</param>
        /// <param name="secondRollPinsCount">Pins down in second roll</param>
        /// <param name="thirdRollPinsCount">in case of tenth frame, pins down in third roll</param>
        /// <param name="strike">specifies whether last frame was a strike</param>
        /// <param name="spare">specifies whether last frame was a spare</param>
        /// <returns>An object of FrameScore Model class</returns>
        public FrameScore CalculateFrameScore(int firstRollPinsCount,
                                              int secondRollPinsCount,
                                              int thirdRollPinsCount,
                                              int totalScore,
                                              int frame,
                                              bool strike,
                                              bool spare,
                                              bool pstrike,
                                              bool pspare)
        {
            FrameScore frameModel = new FrameScore()
            {
                FirstRollPinsCount  = firstRollPinsCount,
                SecondRollPinsCount = secondRollPinsCount,
                ThirdRollPinsCount  = thirdRollPinsCount,
                TotalScore          = totalScore
            };

            if (FrameScore.TotalPinsCount == frame && (firstRollPinsCount == FrameScore.TotalPinsCount || firstRollPinsCount + secondRollPinsCount == FrameScore.TotalPinsCount) && strike == false && spare == false)
            {
                frameModel.TotalScore += firstRollPinsCount + secondRollPinsCount + thirdRollPinsCount;
            }
            else if (firstRollPinsCount + secondRollPinsCount < FrameScore.TotalPinsCount && strike == false && spare == false)
            {
                frameModel.TotalScore += firstRollPinsCount + secondRollPinsCount + thirdRollPinsCount;
            }

            if (strike)
            {
                frameModel.TotalScore += StrikeFrame(firstRollPinsCount, secondRollPinsCount, thirdRollPinsCount, pstrike);
            }
            if (spare)
            {
                frameModel.TotalScore += SpareFrame(firstRollPinsCount, pspare) + secondRollPinsCount + thirdRollPinsCount;
            }
            return(frameModel);
        }
        public void totalizer_should_return_the_sum_of_all_frame_scores()
        {
            var game   = new FrameScorer();
            var frame1 = new FrameScore()
            {
                TotalScore = 20
            };
            var frame2 = new FrameScore()
            {
                TotalScore = 18
            };
            var frame3 = new FrameScore()
            {
                TotalScore = 8
            };
            var expectedResult = 46;

            game.FrameList.Add(frame1);
            game.FrameList.Add(frame2);
            game.FrameList.Add(frame3);
            var actualResult = game.Totalizer();

            Assert.Equal(expectedResult, actualResult);
        }
Exemple #12
0
 private void Start()
 {
     this.frameScore = this.gameObject.GetComponentInChildren <FrameScore>();
     this.bowls      = this.gameObject.GetComponentsInChildren <Bowl>().ToList <Bowl>();
     this.bowls      = this.bowls.OrderBy(b => b.bowlNumber).ToList <Bowl>();
 }
        public void ValueOfAScore()
        {
            FrameScore frameScore = new FrameScore(1);

            Assert.Equal(1, frameScore.Value);
        }
        public void ToStringOfAScore()
        {
            FrameScore frameScore = new FrameScore(1);

            Assert.Equal(@"1", frameScore.ToString());
        }