Finishing the game

I really enjoyed going through the BowlMaster lesson despite it being a long and difficult one. One of the best parts is that I actually ended up with a functioning real-life bowling simulator, which is awesome!

Unfortunately, the lesson ends a bit abruptly at the end with a few loose ends unsolved - mainly, that the end of a game of bowling is not handled. The code is literally left with a //TODO: Handle end of the game. Here is what I did to quickly & easily handle the end-game condition:

  • Add a button to the UI for starting a new game.

  • In PinSetter (where the TODO is), I added a pinCounter.Reset() but otherwise did not add any code. I decided that GameManager should handle a lot of the end-game tasks since it has access to all the various objects and managers. You may decide to architect it differently.

  • In GameManager, I got a hold of the New Game button I had added, as well as the Touch Input object, as private member variables. I disabled the button in the Start() method [newGameButton.SetActive (false);]. When the game ended, I would disable the touch input and enable the New Game button, and upon starting a new game I would do the opposite.

  • To do this, I rearranged and added the following code to the Bowl() method:

      ActionMaster.Action nextAction = ActionMaster.NextAction (rolls);
      pinSetter.PerformAction (nextAction);
      //Handle the end of a game
      if (nextAction == ActionMaster.Action.EndGame) {
      	touchInput.SetActive (false);
      	newGameButton.SetActive (true);
      }
    
  • And I added a new method, ResetGame(), that did the opposite plus some more important tasks - resetting the actual stored rolls and the score display:

      public void ResetGame() {
      	newGameButton.SetActive (false);
      	rolls = new List<int> ();
      	scoreDisplay.Reset ();
      	touchInput.SetActive (true);
      }
    
  • Then I went back to the Editor and set the OnClick event of the New Game button to call ResetGame() from the GameManager object.

I’m happy with the results; I can now confidently say that the game is “done” to a satisfactory level. Of course there are improvements and upgrades I can make, but I feel that this is the level of complete that the lesson should take it to. I hope it helps, thanks for reading!