Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding a variable to a list, then printing the list  RSS feed

 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I've got a simple guessing game with a randomly generated integer.

I'm trying to keep a record of the guesses which the player makes.

I've made an array list, and have attempted to add each guess to it, although I'm only getting the most recent guess in diamond brackets (<>).

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because you don't add anything to guesses in else statements.

You have too many repeats in your code. If you want to add a guess to the list on each iteration why not adding it before if conditions? That way you would not need to add this line in every branch of execution.
 
Rodion Gork
Ranch Hand
Posts: 47
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you do not add anything to "guess" unless when the value is guessed.

If you use debugger you probably can see it in step-by-step execution mode.
 
Liutauras Vilda
Marshal
Posts: 4633
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, probably you don't want to instantiate the game if the value passed to the constructor is...?
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, thanks for the help. I'm definitely going to tidy the code once I've got the code down.

I'm having issues with a "Game Counter". I want to count the number of times a player successfully enters the number.

I thought the code I have would've done this, as it seems reasonably straightforward. Can anyone see where I am overlooking a possible error?

 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is myList.clear() supposed to do?
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
myList.clear should wipe the list of entered guesses, which are printed after a correct answer is provided.
 
Liutauras Vilda
Marshal
Posts: 4633
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll repeat myself once again, your Game will suffer from exception, if value to the constructor is passed less or equal to 0. You should make sure it will never happen.
I assume you're not handling this outside Game class.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something wrong with the clear() call. You appear only to clear it is you guess right first time. You have quite a lot of repeated code in lines 28-42 which should be got rid of. Work out which parts are duplicated and consider putting only the unique lines of code should be inside the inner if‑else.

You can use the ?: operator for try/tries.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't the game over option precede too high and too low? Where are you resetting the counter to 0?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!