Example #1
0
        /// <summary>
        /// Print a single frame
        /// </summary>
        public static string PrintFrame(this BowlingFrame frame)
        {
            if (frame.FrameType == FrameTypeEnum.Strike)
            {
                return("(X)");
            }

            if (frame.FrameType == FrameTypeEnum.Spare)
            {
                return($"({frame.Try1}, /)");
            }

            return($"({frame.Try1} , {frame.Try2})");
        }
Example #2
0
        /// <summary>
        /// Rolls a new frame and returns a new instance of the scorecard
        /// </summary>
        public static ScoreCard RollNewFrame(ScoreCard scoreCard, int tryNo1, int tryNo2)
        {
            // creates a new frame (calculate its type based on the tries and the number of rounds (scorecard's length).
            BowlingFrame newFrame = new BowlingFrame(tryNo1, tryNo2,
                                                     GetFrameType(tryNo1, tryNo2, scoreCard.Length));

            // Check the validity of the new frame
            if (!newFrame.IsValid())
            {
                throw new IllegalBowlingActionException("Invalid frame");
            }

            // Ensure the scorecard is eligible or another frame, otherwise, an error is thrown
            if (IsEligibleForAnotherTry(scoreCard) == false)
            {
                throw new IllegalBowlingActionException("The game has reach to its maximum rounds");
            }

            // Add the new frame to the scorecard and returns a new instance of a scorecard.
            // The goal is to keep the instances immutable.
            ScoreCard newScoreCard = scoreCard.Add(newFrame);

            // Validate the new set of frames
            if (Game.IsValid(newScoreCard) == false)
            {
                throw new IllegalBowlingActionException("An invalid frame was added");
            }

            return(newScoreCard);

            // Inner functions that returns the frame's type based on the number of round or the dropped pins.
            FrameTypeEnum GetFrameType(int item1, int item2, int count)
            {
                if (count == BowlingGameExtenstions.NUM_OF_REGULAR_ROUNDS)
                {
                    return(FrameTypeEnum.BonusFrame);
                }
                if ((item1) == BowlingGameExtenstions.NUM_OF_PINS)
                {
                    return(FrameTypeEnum.Strike);
                }
                if ((item1 + item2) == BowlingGameExtenstions.NUM_OF_PINS)
                {
                    return(FrameTypeEnum.Spare);
                }

                return(FrameTypeEnum.Normal);
            }
        }
Example #3
0
 public BowlingFrame(BowlingFrame frame)
 {
     Try1      = frame.Try1;
     Try2      = frame.Try2;
     FrameType = frame.FrameType;
 }
Example #4
0
 /// <summary>
 /// Create a new scorecard with the additional new frame.
 /// Copy the existing frames and adds the new ones.
 /// </summary>
 public ScoreCard Add(BowlingFrame frame)
 {
     return(new ScoreCard(ImmutableArray.Create <BowlingFrame>().
                          AddRange(Frames).
                          Add(frame)));
 }