/// <summary> /// record a ball score in the frame. /// </summary> /// <param name="score">score of the ball between -X and X. where X is the number of possible pins. negative numbers allow for faults to be recorded.</param> public virtual void MarkScore(int score) { HandleFault(score); BowlingNumber ballScore = new BowlingNumber(); ballScore.Value = score; BallScores.Add(ballScore); HandleFrameStatus(); }
/// <summary> /// record a ball score in the frame. /// </summary> /// <param name="score">score of the ball between -X and X. where X is the number of possible pins. negative numbers allow for faults to be recorded.</param> public override void MarkScore(int score) { base.HandleFault(score); BowlingNumber ballScore = new BowlingNumber(); ballScore.Value = score; BallScores.Add(ballScore); HandleFrameStatus(); }
/// <summary> /// The method to calculate the score of a frame. /// </summary> /// <returns>integer Score of the frame</returns> /// <exception cref="FrameNotReadyToScoreException">FrameNotReadyToScoreException</exception> public virtual int CalculateScore() { int score = (null != PreviousFrame) ? PreviousFrame.FrameScore : 0; if (IsReadyToScore) { //In this case the score is being calculated for a full frame of balls. if (BallScores.Count == CommonFrame.BallCount) { score += BowlingNumber.Sum(BallScores[0], BallScores[1]); //If the ball made a spare then the next ball also needs to be added to the ball. if (IsSpare(BallScores)) { //find the next ball BowlingNumber nextBall = (null != NextTwoFrames && null != NextTwoFrames[0].BallScores) ? NextTwoFrames[0].BallScores[0] : null; if (null != nextBall) { score += nextBall.Value; } } } else { //In this case the frame is finished in the first frame. ///<TODO> /// Not required for this release however It could be interesting to check how this would work in a higher number of ballCount. /// </TODO> if (BallScores.Count == 1) { score += BallScores[0].Value; if (BallScores[0].IsStrike()) { //find the next two balls and add the scores. BowlingNumber nextBall = (null != NextTwoFrames && null != NextTwoFrames[0].BallScores) ? NextTwoFrames[0].BallScores[0] : null; score += null != nextBall ? nextBall.Value : 0; if ((null != NextTwoFrames && (null != NextTwoFrames[0].BallScores || null != NextTwoFrames[1].BallScores))) { BowlingNumber nextNextBall = (NextTwoFrames[0].BallScores.Count == 1) ? NextTwoFrames[1].BallScores[0] : NextTwoFrames[0].BallScores[1]; score += nextNextBall.Value; } } } } FrameScore = score; return(FrameScore); } else { throw new FrameNotReadyToScoreException("Frame is not ready to be scored. Please continue to bowl."); } }
/// <summary> /// The method to calculate the score of a frame. /// </summary> /// <returns>integer Score of the frame</returns> /// <exception cref="FrameNotReadyToScoreException">FrameNotReadyToScoreException</exception> public override int CalculateScore() { int score = null != PreviousFrame ? PreviousFrame.FrameScore : 0; if (IsReadyToScore) { int ball = 0; score += BallScores[ball].Value; //If the ball made a spare then the next ball also needs to be added to the ball. if (IsSpare(BallScores)) { //find the next ball BowlingNumber nextBall = (null != BallScores[ball + 1]) ? BallScores[ball + 1] : ( (null != NextTwoFrames && null != NextTwoFrames[0].BallScores) ? NextTwoFrames[0].BallScores[0] : null); if (null != nextBall) { score += nextBall.Value; } } //else if the ball is a strick calculate it two balls forward. else if (BallScores[ball].IsStrike()) { //find the next ball and add the scores. BowlingNumber nextBall = (BallScores.Count > ball + 1) ? BallScores[ball + 1] : ((NextTwoFrames.Count > 0 && NextTwoFrames[0].BallScores.Count > 0) ? NextTwoFrames[0].BallScores[0] : null); score += null != nextBall ? nextBall.Value : 0; //find the next next ball and add the scores. BowlingNumber nextNextBall = new BowlingNumber(); if (BallScores.Count > (ball + 2)) { nextNextBall = BallScores[ball + 2]; } else if ((NextTwoFrames.Count > 0 && (NextTwoFrames[0].BallScores.Count > 0))) { nextNextBall = (NextTwoFrames[0].BallScores.Count > 1) ? NextTwoFrames[0].BallScores[1] : new BowlingNumber(); } if (nextNextBall.Value > 0) { score += nextNextBall.Value; } } FrameScore = score; return(FrameScore); } else { throw new FrameNotReadyToScoreException("Frame is not ready to be scored. Please continue to bowl."); } }
/// <summary> /// Sums up the values found in two bowling number objects and returns that value. /// </summary> /// <param name="addend">first bowling number object to add.</param> /// <param name="addend2">second bowling number object to add.</param> /// <returns>the sum of the values of the two inputs.</returns> public static int Sum(BowlingNumber addend, BowlingNumber addend2) { return(addend.Value + addend2.Value); }
/// <summary> /// confirms a strike. Also known as all pins are down with only one ball thrown. /// </summary> /// <returns>true for all pins down and false for any other value.</returns> public bool IsStrike() { return(BowlingNumber.Sum(this, this) / 2 == Constant.NUMBER_OF_PINS); }
/// <summary> /// Confirm if a frame is a spare. /// </summary> /// <param name="ballScores">The BowlingNumber list of scores for the frame.</param> /// <returns>True when the balls scores are a spare, otherwise false.</returns> public bool IsSpare(List <BowlingNumber> ballScores) { return(BallScores.Count == 2 && BowlingNumber.Sum(BallScores[0], BallScores[1]) == Constant.NUMBER_OF_PINS); }