/// <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."); } }
/// <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."); } }