/// <summary> /// Validates the scorecard. /// </summary> public static bool IsValid(ScoreCard scoreCard) { // Check if the number of frames has exceeded the maximum allowed. if (IsBeyondExtraRound(scoreCard.Length)) { return(false); } // Validates the extra round if (scoreCard.Length == BowlingGameExtenstions.NUM_OF_REGULAR_ROUNDS + BowlingGameExtenstions.EXTRA_ROUNDS) { int currentFrameIndex = scoreCard.Length - 1; // get the frame's type of the previous round. FrameTypeEnum previousFrameType = GetFrameType(scoreCard, currentFrameIndex - 1); // if the last round is Spare, then having a second try in the extra round is an invalid move. if (previousFrameType == FrameTypeEnum.Spare && scoreCard.GetFrame(currentFrameIndex).Try2 > 0) { return(false); } } // validate rest of the frames return(AreFramesValid(scoreCard, scoreCard.Length - 1)); }
/// <summary> /// Validating a set of frames. /// The validation is based on recursion, to adhere the Functional Programming principles. /// </summary> private static bool AreFramesValid(ScoreCard scoreCards, int length) { // if the scorecard is empty - it is valid. if (length <= 0) { return(true); } // return the validity status of the current frames and the rest of the following frames. return((scoreCards.GetFrame(length)?.IsValid() ?? false) && AreFramesValid(scoreCards, length - 1)); }