• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Just need some help quick with Hangman program

 
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..


 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    Sheriff
    Posts: 7111
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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?
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic