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?
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.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Joined: Jan 17, 2006
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.
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 ]
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra