I am trying to develop a number guessing game using the Math and Scanner classes. I have two classes, the GuessingGame class and the GuessingGameTester class.
The programs are supposed to randomly generate a number and tell the user too low/high, getting colder/warmer, and if they won/lost. The user has five guesses to guess the number between 0 and 100. If they run out of guesses or guess the correct number, they are asked if they want to play again.
Both classes compile and the tester class returns the correct results, but it will not allow the user to guess again. Instead, it asks the user if they want to play again after one guess. If the user chooses to play again, no matter what number is the user is told that they won.
Can someone please take a look at my code and point me in the right direction? Thanks!
How far have you got with that game? Can it be played at all? I can see all sorts of things. I looked at the imports (you don’t need to import java.lang.Anything, and you never use Double) and started worrying.
You have got 5 fields which you are setting up very strangely in your constructor. You ask for max as a parameter and then set it to 5, regardless of what value is passed, You are setting four other fields to their default values, rather than giving them values in the constructor.
I think you ought to initialise all the fields in the constructor; if they haven’t got default values (eg max = 5), you can pass those values as parameters.
I think you need work out what that for loop is doing. I think you should write for loops in this sort of format whenever possible for (int i = 0; i < j; i++) ... In that case, the j would appear to be max, so you can get rid of the 5 and write max.
Now, back to that loop. How many guesses does it take, and where does it take a new guess? How does newGuess alter as you go through that loop? Why is newGuess a field when it is passed to that method? Do you need the newGuess field at all? You are never using it.
In the isGameOver method (good name for the method), you are back to the newGuess field, which you have never used, so it doesn’t supply any useful information. I think you should set the gameOver field in the method which takes the guesses, and you can use the gameOver field ot help control the loop.
I think, I am afraid, that you will be starting from scratch in quite a lot of that class.
I agree to all the points of Ritchie. You have to think about re-structuring the logic.
- The reason why your tester class did not ask to continue is, say if i did not pick the guess first time, the guess returns a message say, "Too Low". the while loop in #22 checks if the gameOne.isGameOver is true. because i had the wrong guess, it isn't. so the flow doesn't continue into the loop and the code exits.
- Why do you need a loop in the guess?
- dont you think having just one variable currentDiff would take the guesser closer to the number? Comparing the currentDiff with the previous differential may miss lead some time.
- To start with, I have re-structured just the tester class in a way to make it readable and modular. See the tester class below: I have moved the logic in main to a playGame method. To facilitate this you have to change the GuessingGame.guess to return a boolean instead of String. you can simply print the messages in the guess method itself. and work on the Guess logic.
The code above is not a perfect solution yet. Atleast it will get you into the second and further attempts which wasn't working for you before.
posted 7 years ago
Thanks for the feedback! I took all of the advice. I'm still having problems with the loop in the GuessingGame class. I'm going to keep at it and see what I can come up with. If you have any other suggestions, I'm all ears. Thanks again!!!
posted 7 years ago
Don’t just keep at it. Write down your algorithm and refine it until it is in words of one syllable. Once you get it to that stage, you should find the code very easy to write.
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library