# Ping Pong Table - Issues with Scores

• February 15th, 2013, 12:35 PM
Ping Pong Table - Issues with Scores
Hi everyone!

Forgive me if my problem is fantastically simple to solve, but I'm a first year Computing student, beginner programmer and this is driving me nuts. As part of our portfolio task, we have been asked to make a Ping Pong Table. The code that represents the table, the paddles, the ball movement, the ball bouncing off the walls and some key event listeners to serve the ball, start a new game and quit are all working perfectly.

However, the issue I'm having is with trying to get the scores to work.

This example code, at present, is making the score go up by one until it reaches the score required to win. That score is stored in a final int variable elsewhere. But let's say for example the maximum score is 3. When the player scores a point, the score goes from 0 to 1, then if they score again, 1 to 2. But, when it hits that maximum score ("WINNING_SCORE" in the code) the score just seems to add one forever.

I've tried adding the kidScore = kidScore + 1 to a while loop but that has resulted in the score no longer shifting from zero.

Here is the code as it is so far:

Code java:

```  }else if (ballX < BALL_MIN_X){ //Discovers if the co-ordinates of the ball have entered the "score" zone of the table     kidScore = kidScore + 1; //Adds one to the score       table.setMessageText("Computer: " + computerScore + " Kid " + kidScore); //Sets the score in the table MessageText   if (kidScore<WINNING_SCORE){ //Whilst the player's score is below the winning score, the game just carries on from the kid's serve   ballX = KID_RACKET_X; ballY = KID_RACKET_Y_START; ballServed = false;   }else if (kidScore==WINNING_SCORE){ displayScore(); //If the player has the amount of points to win, this calls the displayScore method }```

Any thoughts on how I could fix this?

Many thanks!
• February 15th, 2013, 12:42 PM
Norm
Re: Ping Pong Table - Issues with Scores
Quote:

when it hits that maximum score ("WINNING_SCORE" in the code) the score just seems to add one forever.
What logic determines when the score is to be incremented and when it is not to be incremented and left as is?

The unformatted code that was posted is hard to read and understand.
[code=java]
[/code]
to get highlighting and preserve formatting.
• February 15th, 2013, 01:22 PM
Re: Ping Pong Table - Issues with Scores
It's the else if at the top of the code. It basically tries to find the co-ordinates of the ball in respect to the table. So when the ball arrives in a particular place on the table, that means the player scores a point.
• February 15th, 2013, 01:38 PM
Norm
Re: Ping Pong Table - Issues with Scores
Quote:

when the ball arrives in a particular place on the table, that means the player scores a point.
Is that what you want to happen? Always? Your first post hinted that there was a problem:
Quote:

score just seems to add one forever
• February 15th, 2013, 01:44 PM
Re: Ping Pong Table - Issues with Scores
Yeah, sorry I am not being clear. What I mean is that I want it to continue adding one to the score until it arrives at the winning score (eg. the score that will win the game)

So if the winning score is set at 10 points - I want it to continue adding 1 every time the player scores until it reaches 10 and then stop. Which sounds very basic to solve with a loop of some sort, but I've not been able to implement a piece of code that actually makes it work.

At the moment, once it hits that winning score of 10, it just quickly keeps adding infinitely, even when the game isn't being played.

Would it be more helpful if I posted all of the classes that I am using?
• February 15th, 2013, 01:48 PM
Norm
Re: Ping Pong Table - Issues with Scores
Quote:

I want it to continue adding 1 every time the player scores until it reaches 10 and then stop.
You could test if the score is at the max and only increment it if it is less than max.
• February 15th, 2013, 02:07 PM
Re: Ping Pong Table - Issues with Scores
But this is my issue, I have tested that and I know that it reaches the value stored in "WINNING_SCORE" because it will always start furiously adding 1 every time it hits that number of points.

So if the WINNING_SCORE variable is 10, the score increments by 1 every time a point is scored until it reaches 9. Then when it reaches 9, the next point scored will cause the score to rapidly increment infinitely. If I were to change the WINNING_SCORE variable to 5, the score will reach 4 and then the next point will cause the score to rapidly increment infinitely.

Instead, what I want it to do is hit the max value which will then call a method that displays "You won 5-0" or "You won 10-0" or whatever the score is.
• February 15th, 2013, 02:18 PM
Norm
Re: Ping Pong Table - Issues with Scores
If you test the value of score BEFORE adding to it and not add to it if its value is too high, then its value should never be increased.

In pseudo code
test score against max
if less than max add one to score
• February 15th, 2013, 03:55 PM
Re: Ping Pong Table - Issues with Scores
Like this?

Code java:

```  if (ballX > BALL_MAX_X ){       if (computerScore<WINNING_SCORE){   computerScore = computerScore + 1; table.setMessageText("Computer: " + computerScore + " Kid: " + kidScore); ballX = KID_RACKET_X; ballY = KID_RACKET_Y_START; ballServed = false;     }else if (computerScore==WINNING_SCORE) { displayScore(); }```
• February 15th, 2013, 04:05 PM
Norm
Re: Ping Pong Table - Issues with Scores
What happens when you compile and execute the code?
Does it do what you want?

There are too many spaces in the posted code pushing it too far to the right and making it very hard to read.
• February 15th, 2013, 04:16 PM
Re: Ping Pong Table - Issues with Scores

Code java:

```if (ballX > BALL_MAX_X) {   if (computerScore < WINNING_SCORE) {   computerScore = computerScore++; table.setMessageText("Computer: " + computerScore + " Kid: " + kidScore);   ballX = KID_RACKET_X; ballY = KID_RACKET_Y_START; ballServed = false;   } else if (computerScore == WINNING_SCORE) { displayScore(); }```

Well now it's no longer infinitely adding 1 anymore. Only issue is, the displayScore() method which will announce the winner and show the final score will only be called when the score is 1 higher than the WINNING_SCORE variable.

So if I set the WINNING_SCORE variable to 10, I have to concede 11 points in the game for it to tell me that the computer won 10 - 0. This has confused me because it I have specified the argument that if ( computerScore == WINNING_SCORE) then it should run the method displayScore. But it's only doing it after the winning score variable is the maximum value + 1.
• February 15th, 2013, 04:33 PM
Norm
Re: Ping Pong Table - Issues with Scores
Quote:

show the final score will only be called when the score is 1 higher than the WINNING_SCORE variable.
where in the code is the logic for that? The posted code says that this computerScore == WINNING_SCORE must be true for the display method to be called.
Can you copy the console's contents that shows what you are talking about. Add some comments to the code to show what you are talking about.

What does the code do when the score is 9? How will the else if condition ever be tested when the score has gone to 10?
• February 15th, 2013, 04:54 PM
Norm
Re: Ping Pong Table - Issues with Scores
Quote:

Right...
Does that mean you see the problem and are fixing it?

Its not necessary to post the constants.
• February 15th, 2013, 04:57 PM
```// Step 5. Update the score if the ball is in the // green area but is not moving if (isBallOnTheTable()) { if (ballX > BALL_MAX_X) { //If the computer scores a point   if (computerScore < WINNING_SCORE) { //If computerScore is less than WINNING_SCORE   computerScore = computerScore+1; //Add one to the computer's score every time they win a point table.setMessageText("Computer: " + computerScore + " Kid: " + kidScore); // Update the scores to reflect the points won   ballX = KID_RACKET_X; // Sets the coordinates of the ball for the restart ballY = KID_RACKET_Y_START; ballServed = false; //Ensures that the ball is not instantly served when the computer wins a point   } else if (computerScore == WINNING_SCORE) { //Once the computer's score is equal to WINNING_SCORE displayScore(); //Run the method that will display the final scores }   } else if (ballX < BALL_MIN_X) { //If the player scores a point       if (kidScore < WINNING_SCORE) { //If the kidScore is less than WINNING_SCORE   kidScore = kidScore + 1; //Add one to the kidScore every time they win a point   table.setMessageText("Computer: " + computerScore + " Kid " + kidScore); //Update the scores to reflect the points won   ballX = KID_RACKET_X;//Sets the coordinates of the ball for the restart ballY = KID_RACKET_Y_START; ballServed = false; //Ensures that the ball is not instantly served when the player wins a point   } else if (kidScore == WINNING_SCORE) { //Once the kids score is equal to WINNING_SCORE displayScore(); //Run the method that will display the final scores }   }```