8 – SCOREBOARD CLASS

 

 

GENERAL INFO

 

We will store the score inside the Scoreboard class.  The Scoreboard class will also keep track of the whether the game is over or not.  This will be done by providing a maximum score to the game.

 

DATAFIELDS

 

Our class will have the following data fields:

 

·         int score1 – Paddle 1’s score.

·         int score2 – Paddle 2’s score.

·         int maxScore – Score at which the game ends.

·         boolean gameOver – False until the maximum score is reached.

·         int winner – Initially zero, will be set to 1 or 2 once the maximum score is reached.

 

Go ahead and create the class with the above data fields.

 

CONSTRUCTOR

 

Our constructor only has one parameter for the max score.  The other data fields get the following values:

 

·         score1 = 0

·         score2 = 0

·         gameOver = false

·         winner = 0;

 

Create the above constructor.

 

GET METHODS

 

Create get methods for all above data fields. 

 

For the gameOver field, call the method gameOver() instead of getGameOver().

 

INCREMENT SCORE METHODS

 

We now need to create an incrementScore1() method.  When called, it simply increases score1 by 1.  It then checks if score1 is now equal to maxScore.  If it is, gameOver is set to true and winner is set to 1.

 

Implement the method incrementScore1() described above.

 

Implement the method incrementScore2() which is very similar.