Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How come the guess and game count stays the same if I play again as the same player?  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time I play the game with the same player names, it will re-start its game and guess count instead of adding on to it. Can someone explain why? Thanks.


Player Class:





Game Class:

 
Justin Turner
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you are going to have to edit the BufferedReader and BufferedWriter.
 
Rancher
Posts: 3324
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the program's output that shows what you are asking about?
Be sure to add some comments where it is shown.
 
Justin Turner
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Can you post the program's output that shows what you are asking about?
Be sure to add some comments where it is shown.



From the console:

How many players are going to play the game?
1
The data file has been read in.
There are 0 players in your file as you can see above.

What is your name player 1 ?
Mo
Welcome Mo. You are going to be referred as Player 1


The guessing range will be from 1 to 10.

Round 1:
Player 1 please enter your guess: 3
Too low. Guess again next round.

—————————————————————————————————————————————————

Round 2:
Player 1 please enter your guess: 8
Too high. Guess again next round.

—————————————————————————————————————————————————

Round 3:
Player 1 please enter your guess: 6
Too high. Guess again next round.

—————————————————————————————————————————————————

Round 4:
Player 1 please enter your guess: 5
Correct!
—————————————————————————————————————————————————

Mo finished the game with 4 guesses
Mo wrong guesses were:  3 8 6

Saving Data File.
1 players to be saved.

Mo 1 4

Would you like to play again? Type in y to play again.y
How many players? 1
The data file has been read in.
Mo 1 4
There are 1 players in your file as you can see above.

What is your name player 1 ?
Mo
Welcome Mo. You are going to be referred as Player 1


The guessing range will be from 1 to 10.

Round 1:
Player 1 please enter your guess: 3
Too low. Guess again next round.

—————————————————————————————————————————————————

Round 2:
Player 1 please enter your guess: 8
Too low. Guess again next round.

—————————————————————————————————————————————————

Round 3:
Player 1 please enter your guess: 10
Correct!
—————————————————————————————————————————————————

Mo finished the game with 3 guesses
Mo wrong guesses were:  3 8

Saving Data File.
1 players to be saved.

Mo 1 3

Would you like to play again? Type in y to play again.n
Thanks for playing. Goodbye!
 
Justin Turner
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be Mo 2 7 at the end.

2 = number of total games

7 = number of total guesses
 
Norm Radder
Rancher
Posts: 3324
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mo 1 3  



Where is that line printed?  What are the names of the variables with the values 1 and 3?


It would be easier to find in the source code if it included some unique text:

Mo Total Games=1, Total guesses=3


How are you trying to debug the program to see why it does what it does?  Add some print statements that print the values of the variables  every time they are changed and used.  The print out should should you where the problem is.
 
Justin Turner
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

Mo 1 3  



Where is that line printed?  What are the names of the variables with the values 1 and 3?


It would be easier to find in the source code if it included some unique text:

Mo Total Games=1, Total guesses=3


How are you trying to debug the program to see why it does what it does?  Add some print statements that print the values of the variables  every time they are changed and used.  The print out should should you where the problem is.




The problem is that my setCounter() method and setGame() method in the player class are both not working.
Each time the game re-starts if I choose to play again, it will go back to 0.
 
Justin Turner
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

Mo 1 3  



Where is that line printed?  What are the names of the variables with the values 1 and 3?


It would be easier to find in the source code if it included some unique text:

Mo Total Games=1, Total guesses=3


How are you trying to debug the program to see why it does what it does?  Add some print statements that print the values of the variables  every time they are changed and used.  The print out should should you where the problem is.



It is printed out in Line 182, as I created an array which adds the Player Name, Player Game Count, and Player Guess Count.
 
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin,

This certainly is a valiant attempt to decompose the problem into many smaller problems.  There are still a lot of things you can do better though.

Object-oriented programming has a lot to do with assigning responsibilities and behavior to appropriate abstractions.  From the code you have written, I can see that you're trying to use two main abstractions here: the Player and the Game.

Try to think of what responsibilities a Player object would have. Does it really make sense for a Player to be responsible for keeping the value that needs to be guessed? To me it doesn't make sense. That's like you and me playing the game and I say "Ok, Justin, think of a secret number and then try to guess what it is."  Wouldn't it make more sense for the Game class to keep that? So, if I were a Game object, I'd say "OK, Justin, I'm thinking of a number from 1 to 10. Can you guess what it is?"

Now, it might make sense for you (the Player) to keep track of the incorrect guesses you've made so you don't repeat the same ones as the game progresses. Right now, your Player class uses a String to keep that list. Why? You know about Lists, obviously. Wouldn't it make more sense for a list of incorrect guesses be kept in a List?

There are many other puzzling choices you've made in your design, such as why would you create a new instance of a player by giving it a guess? It seems like it would make more sense to create a new Player object by telling it what its name should be. Also, it's easy enough to see what a List contains if you call its toString() method.
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Study this code to see how responsibilities might be assigned in a way that makes more sense:

 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All that leads me to your original question: How come the guess and game count stays the same if I play again as the same player?

You can make an object "remember" state by keeping information in its instance fields, like what I showed previously as the incorrectGuesses list and the Player's name. Obviously, if you want to reset some of that state information, you'd want to have some way of telling the object to do that. The method name you choose should make sense in the context of the larger story you're trying to tell. Something like this perhaps:


So, if you wanted to use the same Player object to play multiple rounds, you might have this kind of code:
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:... The method name you choose should make sense in the context of the larger story you're trying to tell. Something like this perhaps:


I hope you see by now how it helps to make your code more expressive of its intent rather than the implementation, like the name "prepareForNewRound," which says nothing about clearing the list of incorrect guesses and resetting the last guess. In fact, as much as possible, your code should be as expressive of intent as it can be. Take for example the statement that resets the lastGuess instance variable above.

That line of code can also use some improvement. The value being assigned seems to come out of the blue. Where did we get -1? What does it mean? Anyone reading that line of code has to infer/deduce what it means by reading the surrounding code. What if we did this instead though:

With that small change of introducing an explaining variable/constant, we made the intent of the -1 value much more explicit. The -1 value is what is called a "magic number" and you want to avoid those in your program. Again, as much as possible, everything in your program should clearly express its intent.
 
Justin Turner
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Junilu Lacar wrote:... The method name you choose should make sense in the context of the larger story you're trying to tell. Something like this perhaps:


I hope you see by now how it helps to make your code more expressive of its intent rather than the implementation, like the name "prepareForNewRound," which says nothing about clearing the list of incorrect guesses and resetting the last guess. In fact, as much as possible, your code should be as expressive of intent as it can be. Take for example the statement that resets the lastGuess instance variable above.

That line of code can also use some improvement. The value being assigned seems to come out of the blue. Where did we get -1? What does it mean? Anyone reading that line of code has to infer/deduce what it means by reading the surrounding code. What if we did this instead though:

With that small change of introducing an explaining variable/constant, we made the intent of the -1 value much more explicit. The -1 value is what is called a "magic number" and you want to avoid those in your program. Again, as much as possible, everything in your program should clearly express its intent.




I appreciate all your feedback. Thank you!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!