jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Adding a usedChar array in my program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding a usedChar array in my program" Watch "Adding a usedChar array in my program" New topic
Author

Adding a usedChar array in my program

Klaas Vredevort
Greenhorn

Joined: Dec 27, 2005
Posts: 29
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
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143


Stephen Foy - Microsoft Application Development Consultant
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143


Ugh, my logic sucks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Adding a usedChar array in my program
 
Similar Threads
Hangman problem!
Please explain this line of code to me
Can somebody show me how to implement buttons?
Hangman BufferReader ??
Updating the _ _ _ _ _ in a hangman game?