Ejemplo n.º 1
0
        /// <summary>
        /// This will clone a CommonFrame. The frame will have the next frames, previous frame and BallScores set back to null in the clone.
        /// </summary>
        /// <returns>clone of the CommonFrame with next frames, previous frame and BallScores set back to null.</returns>
        public object Clone()
        {
            //The cloning process is to create a copy of the frame and then to clear out the next frames, previous frame and ball scores so they can be created after cloning.
            CommonFrame newCommonFrame = (CommonFrame)MemberwiseClone();

            newCommonFrame.FrameScore            = 0;
            newCommonFrame.IsClosed              = false;
            newCommonFrame.IsReadyForNextFrame   = false;
            newCommonFrame.IsReadyToScore        = false;
            newCommonFrame.IsPreviousFrameScored = false;
            newCommonFrame.LostPinsByFault       = 0;
            newCommonFrame.NextTwoFrames         = new List <CommonFrame>();
            newCommonFrame.PreviousFrame         = null;
            newCommonFrame.BallScores            = new List <BowlingNumber>();
            return(newCommonFrame);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Preffered constructor. Takes in both previous frame and next two frames to provide the class with all required objects.
 /// </summary>
 /// <param name="previousFrame">The previous frame in the game.</param>
 /// <param name="nextTwoFrames">The next two frames in the game.</param>
 public CommonFrame(CommonFrame previousFrame, List <CommonFrame> nextTwoFrames)
 {
     NextTwoFrames = nextTwoFrames;
     PreviousFrame = previousFrame;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// This method allows all joined bowlers to take their turns until the game is over and scores are ready to be calculated.
        /// </summary>
        /// <exception cref="GameNotStartedException">GameNotStartedException can be thrown if a game has not yet been started but play is attempted.</exception>
        public void Play(List <int> ballScores)
        {
            if (CurrentPhase.CurrentPhase == Constant.PLAY_GAME_PHASE)
            {
                try
                {
                    //Ensure this queue has plenty of data to dequeue.
                    while ((ballScores.Count) < (Constant.DEFAULT_FRAMES_PER_GAME * Constant.DEFAULT_FRAMES_BALLS) + (Constant.DEFAULT_SPECIAL_FRAMES_PER_GAME * Constant.DEFAULT_SPECIAL_FRAMES_BALLS))
                    {
                        ballScores.AddRange(ballScores);
                    }
                    Queue <int> balls = new Queue <int>(ballScores);
                    //ensure the game plays all frames.
                    for (int frame = SpecialFramesPerGame; frame < FramesPerGame; frame++)
                    {
                        //for now we will have each bowler take their turn and let the bowler control the score recieved in the TakeTurn method.
                        foreach (Player bowler in JoinedPlayers)
                        {
                            ///<TODO>
                            ///Redesign this section of the game to have a bowler complete a frame instead of just taking a turn.
                            ///</TODO>
                            int ballScore = bowler.TakeTurn(balls.Dequeue());
                            Scorecard.MarkScore(bowler, ballScore);

                            //Did the player get a strike or all pin fault? If so the next player goes otherwise the bowler gets the second turn.
                            CommonFrame checkForSecondBall = new CommonFrame();
                            checkForSecondBall.MarkScore(ballScore);
                            if (!checkForSecondBall.IsReadyForNextFrame)
                            {
                                Scorecard.MarkScore(bowler, bowler.TakeTurn(balls.Dequeue()));
                            }
                        }

                        //Lets have the bowlers display their progress as part of the game as well, after the turn is taken.
                        foreach (Player bowler in JoinedPlayers)
                        {
                            bowler.ReviewHistory();
                        }
                    }

                    //run the final frames
                    for (int frame = 0; frame < SpecialFramesPerGame; frame++)
                    {
                        //for now we will have each bowler take their turn and let the bowler control the score recieved in the TakeTurn method.
                        foreach (Player bowler in JoinedPlayers)
                        {
                            int finalFrameOpenCounter = 0;
                            for (int finalBalls = 0; finalBalls < FinalFrame.BallCount; finalBalls++)
                            {
                                ///<TODO>
                                ///Redesign this section of the game to have a bowler complete a frame instead of just taking a turn.
                                ///</TODO>
                                int ballScore = bowler.TakeTurn(balls.Dequeue());
                                Scorecard.MarkScore(bowler, ballScore);

                                finalFrameOpenCounter += ballScore;
                                //Break out of the loop if The first two balls are an open frame.
                                if (finalFrameOpenCounter < Constant.NUMBER_OF_PINS && finalBalls == 1)
                                {
                                    break;
                                }
                            }
                        }

                        //Lets have the bowlers display their progress as part of the game as well, after the turn is taken.
                        foreach (Player bowler in JoinedPlayers)
                        {
                            //To ensure all frames were scored. The frames need to have their scores calculated one last time.
                            ((FinalFrame)Scorecard.Sheet[bowler][bowler.CurrentGame.Scorecard.Sheet[bowler].Count - 1]).HandleFrameStatus();

                            bowler.ReviewHistory();
                        }
                    }
                }
                finally
                {
                    FinishGame();
                }
            }
            else
            {
                throw new GameNotStartedException("The game has not yet been started. Please start the game before play can begin.");
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// constructor allowing previousFrame to be passed in. Useful for the last frame. nextTwoFrames has to be managed through the property.
 /// </summary>
 public CommonFrame(CommonFrame previousFrame)
 {
     PreviousFrame = previousFrame;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// This method allows all joined bowlers to take their turns until the game is over and scores are ready to be calculated.
        /// </summary>
        /// <exception cref="GameNotStartedException">GameNotStartedException can be thrown if a game has not yet been started but play is attempted.</exception>
        public void Play()
        {
            if (CurrentPhase.CurrentPhase == Constant.PLAY_GAME_PHASE)
            {
                try
                {
                    //ensure the game plays all frames.
                    for (int frame = SpecialFramesPerGame; frame < FramesPerGame; frame++)
                    {
                        //for now we will have each bowler take their turn and let the bowler control the score recieved in the TakeTurn method.
                        foreach (Player bowler in JoinedPlayers)
                        {
                            ///<TODO>
                            ///Redesign this section of the game to have a bowler complete a frame instead of just taking a turn.
                            ///</TODO>
                            int ballScore = bowler.TakeTurn();
                            Scorecard.MarkScore(bowler, ballScore);

                            //Did the player get a strike or all pin fault? If so the next player goes otherwise the bowler gets the second turn.
                            CommonFrame checkForSecondBall = new CommonFrame();
                            checkForSecondBall.MarkScore(ballScore);
                            if (!checkForSecondBall.IsReadyForNextFrame)
                            {
                                Scorecard.MarkScore(bowler, bowler.TakeTurn());
                            }
                        }

                        //Lets have the bowlers display their progress as part of the game as well, after the turn is taken.
                        foreach (Player bowler in JoinedPlayers)
                        {
                            bowler.ReviewHistory();
                        }
                    }

                    //run the final frames
                    for (int frame = 0; frame < SpecialFramesPerGame; frame++)
                    {
                        //for now we will have each bowler take their turn and let the bowler control the score recieved in the TakeTurn method.
                        foreach (Player bowler in JoinedPlayers)
                        {
                            int finalFrameLessThanPins = 0;
                            for (int finalBalls = 0; finalBalls < FinalFrame.BallCount; finalBalls++)
                            {
                                ///<TODO>
                                ///Redesign this section of the game to have a bowler complete a frame instead of just taking a turn.
                                ///</TODO>
                                int ballScore = bowler.TakeTurn();
                                Scorecard.MarkScore(bowler, ballScore);

                                ///<TODO>
                                ///This code needs to be cleaned up and put into a task. My design didn't quite work out for making this code clean.
                                /// </TODO>
                                if (ballScore < Constant.NUMBER_OF_PINS)
                                {
                                    finalFrameLessThanPins += ballScore;
                                    if (finalFrameLessThanPins < Constant.NUMBER_OF_PINS && finalBalls == 1)
                                    {
                                        break;
                                    }
                                }
                            }
                        }

                        //Lets have the bowlers display their progress as part of the game as well, after the turn is taken.
                        foreach (Player bowler in JoinedPlayers)
                        {
                            //To ensure all frames were scored. The frames need to have their scores calculated one last time.
                            ((FinalFrame)Scorecard.Sheet[bowler][bowler.CurrentGame.Scorecard.Sheet[bowler].Count - 1]).HandleFrameStatus();

                            bowler.ReviewHistory();
                        }
                    }
                }
                finally
                {
                    FinishGame();
                }
            }
            else
            {
                throw new GameNotStartedException("The game has not yet been started. Please start the game before play can begin.");
            }
        }