• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding a usedChar array in my program

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I'm writing a Hangman program and it has three classes, Hangman, HangmanFileReader (it gets the words out of a file) and HangmanManager (which acts as a driver)

The program runs 100% the way I want it, except for one thing: If the user guesses a letter they have guessed correctly or incorrectly before, it still subtracts from their chances left.

What I want to do is add an array of chars, which contains the letters the user has already guessed. Then, if he guesses a letter he has guessed before, the computer says to that the letter has been used, and does not subtract it from the chances he has left.

I have declared the array as



in the Hangman class, which I will paste here. I certainly do not expect of you to go through the entire thing, but I think it should help you. Here is is:



What do you think? should it actually test for the letter before it does anything else?

Your help is greatly appreciated!

Klaas
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stephen Foy:



In this bit of code, you are adding the guessedLetter to the letters array in the line I've marked *1, and then you iterate through the letters array to see if the letter is there in the line I marked *2. Therefore the letter will always be found and the message will always print. The genral premise will work but the logic is a little off.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think an ArrayList<Character> would also work well in this situation.


If you're not familiar with the ArrayList class, you can check out the documentation here.

Garrett
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think a good solution is to just have one array(or ArrayList is you prefer), with every character in the alphabet, when the player picks a char, it is removed from the array. If it isn't found, then you know it was used already.

Your way works just fine, this is just a slightly more efficient and smaller solution.

You definately want to check to see if it has been used already, since the proper behavior of the program depends on that being doen first.
[ March 05, 2006: Message edited by: Rusty Shackleford ]
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Ugh, my logic sucks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic