Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Ping Pong Table - Issues with Scores

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

     
    					}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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,144
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default Re: Ping Pong Table - Issues with Scores

    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.
    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,144
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default Re: Ping Pong Table - Issues with Scores

    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:
    score just seems to add one forever
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,144
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default Re: Ping Pong Table - Issues with Scores

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,144
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ping Pong Table - Issues with Scores

    Like this?

     
    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();
    						}

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,144
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ping Pong Table - Issues with Scores

    Is this easier to read?

    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.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,144
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default Re: Ping Pong Table - Issues with Scores

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,144
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default Re: Ping Pong Table - Issues with Scores

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

    Its not necessary to post the constants.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ping Pong Table - Issues with Scores

    And this is where the scores are manipulated...

    // 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
    	}
     
    }

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,144
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default Re: Ping Pong Table - Issues with Scores

    When the score is changed to the max value, when will the posted code detect that and call the display method?

    Play computer with the code and see when each section of the code will be executed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. use array of scores to calculate an average
    By littlebit45 in forum Other Programming Languages
    Replies: 1
    Last Post: November 30th, 2012, 02:25 PM
  2. Replies: 15
    Last Post: July 2nd, 2012, 12:45 PM
  3. How to get scores from user in Canvas J2ME?
    By elenora in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: April 4th, 2011, 04:10 AM
  4. Arcade scores reset
    By JavaPF in forum The Cafe
    Replies: 1
    Last Post: July 3rd, 2010, 05:31 PM
  5. [SOLVED] Fixing of bug for Pong game
    By phoenix in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 14th, 2009, 01:19 PM