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

Just need some help quick with Hangman program  RSS feed

 
William Nyqvister
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have wrote a Hangman program. It is all working fine, but there is one issue. I want the user to have 5 "tries" before the program stops. The "tries" are only decreasing if the user puts the wrong letter into it. Now though the "tries" are based on the length of the word. And either if it is a wrong or right letter the "tries" are decreasing. How can i change the "tries" to 5 and make it only decreasing when there is a wrong input? Sorry for my bad english..


 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Nyqvister wrote:Hi. I have wrote a Hangman program. It is all working fine, but there is one issue. I want the user to have 5 "tries" before the program stops.

Well that's not how you've written it - I see no mention of 5 anywhere, only randomGuessWord.length.

My suggestion: Explain to us exactly how many guesses you want to allow for each of your words, because right now I'm a bit confused.

2. I presume your logic at line 56 is meant to allow the player to end the game prematurely, but it won't work because you don't set ALL the requirements to break out.

3. I applaud your efforts at writing descriptive names for your fields. WELL DONE, and have a cow.
However, you could shorten them by leaving out redundant bits; for example:
  • 'amountOfGuesses' - we already know it's an "amount" because it's an int, so how about just 'guesses'?
  • 'randomGuessWord' - the fact that it's random is irrelevant to our understanding of the program, so how about 'wordToGuess'?
  • That sort of stuff will come with practise, and whatever you do, don't stop writing descriptive names. Just maybe ease back on the "narrative stuff" a bit.

    4. Try to keep to Java naming conventions. For example, your array at line 28 should be 'wordlist', not 'Wordlist'.

    5. DontWriteLongLines when you post code. It makes it difficult to read. I've broken up some of the worst offenders for you, but best is to use the 'Preview' button yourself before you post.

    Winston
     
    Knute Snortum
    Sheriff
    Posts: 4073
    112
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think I would look where tries is incremented. Only increment when a guess is wrong. At line 61 you have an if statement that determines if a guess is correct. How could you use this to increment tries only if the guess is incorrect?
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!