Results 1 to 5 of 5
Thread: Hangman Game // HELP //
- 05-26-2010, 11:22 PM #1
Member
- Join Date
- May 2010
- Location
- Muskoka, Ontario
- Posts
- 3
- Rep Power
- 0
Hangman Game // HELP //
Hi all
I am having trouble with this "simple" hangman game. Could use some pointers.
ASSIGNMENT:
Create a simple guessing game, similar to Hangman, in which the user guess letters then attempts to guess a partially hidden phrase. Display a phrase in which some of the letters are replaced by asterisks; for example "e*e*****" (for "elephant"). Each time the user guesses a letter, either place the letter in the correct spot (or spots) in the phrase and display it again, or tell the user the guessed letter is not in the phrase. Display a congratulatory message when the entire correct phrase has been deduced. Save the game as SecretPhrase.java
Well I have done most of it. Except it's a long series of if statements and the characters are only replaced exactly where I tell them to. It works fine and I wouldn't mind handing it in it's just that I don't know (and can't find) the code to tell the program to end when all the characters are entered and display "Congratulations!"
Also if anyone knows some code that could just detect where the letters are supposed to go, rather than using setCharAt() that would really help. THANKS!
Java Code:import java.util.Scanner; public class SecretPhrase { Scanner keyboard = new Scanner(System.in); String SecretPhrase, guess; int x; String hiddenPhrase = "********"; String secretPhrase = "elelphant"; StringBuilder word = new StringBuilder("********"); public void StringBuilder() { String secretPhrase2 = "elephant"; String hiddenPhrase2 = "********"; } public static void main(String args[]) { SecretPhrase display = new SecretPhrase(); display.Guess(); } public void Guess() { System.out.println("\nGuess a letter in the word!"); guess = keyboard.next(); guess = guess.toLowerCase(); for(x = 0; x < hiddenPhrase.length(); x++) { if(guess.equals("e")) { //StringBuilder word = new StringBuilder("********"); x = hiddenPhrase.length(); word.setCharAt(0, 'e'); word.setCharAt(2, 'e'); System.out.println("Correct!\n"+word); Guess(); } else if(guess.equals("l")) { word.setCharAt(1, 'l'); System.out.println("Correct!\n"+word); Guess(); } else if(guess.equals("p")) { word.setCharAt(3, 'p'); System.out.println("Correct!\n"+word); Guess(); } else if(guess.equals("h")) { word.setCharAt(4, 'h'); System.out.println("Correct!\n"+word); Guess(); } else if(guess.equals("a")) { word.setCharAt(5, 'a'); System.out.println("Correct!\n"+word); Guess(); } else if(guess.equals("n")) { word.setCharAt(6, 'n'); System.out.println("Correct!\n"+word); Guess(); } else if(guess.equals("t")) { word.setCharAt(7, 't'); System.out.println("Correct!\n"+word); Guess(); } else System.out.println("Sorry no "+guess+"'s"); Guess(); ++x; } } }
- 05-26-2010, 11:41 PM #2
Instead of calling Guess() again going down/recuring one level, try putting the code in a forever loop: while(true). Then when your done you can exit the loop with a break statement and give out a message.
Instead of explicitly coding each letter in the hidden word (that is EXTREMELY POOR coding) use a search technique to see if the entered letter is in the hidden word. The String method indexOf() could be used.
You'd need to put it in a loop in case a letter is repeated. Also look at using the starting point of the indexOf() method.
-
Your if blocks have to go as this ties your code irretrievably to the data making your program very inflexible. The question you should ask yourself is this: How hard would it be to change the hiddenPhrase? If you have to fiddle with many if blocks to do this, something is wrong.
One way is to have your, hiddenPhrase StringBuilder (I like your use of this) and secretPhrase String, and then when the user enters a letter, first check if it is in the hiddenPhrase (if it has been uncovered already) by first converting the StringBuilder to String via toString() and then using the String method contains(...), and if not check if it's in the secretPhrase using the same method. If present, then, you might as well iterate through the secretPhrase in a for loop looking at each char via the charAt(int index) method, and if it is the selected char, change the corresponding char in the hiddenPhrase StringBuilder object. After any change to StringBuilder, check if it contains "*", and if not, then the puzzle's been solved.
- 05-26-2010, 11:55 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
No, you don't want to turn this in. :) Take another try at this. You've learned looping constructs, right? You have a secret word in a String, and your user is guessing one letter at a time. So for each letter:
That's one turn. Wrap all of that in another loop, and you're finished when wordGuessed is true.Java Code:add the letter that was guessed to a guessedLetters String start with an empty display String set a boolean correctGuess to false, and set a boolean wordGuessed to true loop from zero to the length of the secret word get the letter at your loop counter's position in the secret word see if that letter is in your guessedLetters String if it is there append it to your display String set correctGuess to true else append a * to your display String set wordGuessed to false show your display String use your two boolean variables to show appropriate messages
-Gary-
- 05-27-2010, 12:01 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Similar Threads
-
Hangman Game Help Please
By 9tjh in forum New To JavaReplies: 4Last Post: 12-04-2009, 03:19 AM -
Hangman Game..
By iPetey in forum New To JavaReplies: 4Last Post: 05-07-2009, 02:24 PM -
Need help with hangman game
By kurt in forum New To JavaReplies: 4Last Post: 04-25-2009, 06:47 PM -
Hangman Game
By L23 in forum New To JavaReplies: 8Last Post: 07-03-2008, 01:56 PM -
Create the game Hangman
By barney in forum New To JavaReplies: 1Last Post: 08-06-2007, 06:16 AM


LinkBack URL
About LinkBacks

Bookmarks