English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I need to make a Hangman game that only does one 5 letter word. This word, however, needs to be able to be changed by the user easily at the top of the code. So far I have the word divided into characters like this

final char LETTER1='h', LETTER2='o', LETTER3='u', LETTER4='s', LETTER5='e';

And I have a switch for each of the letters. This way makes it difficult to change the word because you have to change the letters in the declarations as well as in the switch. Any suggestions about what I could do?

2007-08-08 06:53:52 · 4 answers · asked by Matt C 1 in Computers & Internet Programming & Design

4 answers

I would store the word in a String ("house"), and just loop through the characters of the String looking for the guessed characters.

What do your switch statements do?

2007-08-08 07:05:09 · answer #1 · answered by McFate 7 · 0 0

Like McFate said, store it as a String instead of individual chars. Then you can use the "contains" method of the String Class to find if the guessed letter is in the string or not. That way you only have one place where the word is.

Final String word = "house";

// Method to find it
private boolean checkString(String guess)
{
return house.contains.ignorcase(guess));
}

I've written it as a method call to return if it has it or not. That way you can call it it your other method which handles the input from the user. Notice the simplicty, the conatins method already returns a boolean, so you can just use that, don't even need a switch. The only real thing you should add here is a check if "guess" is null so it doesn't give a "NullPointerException"

2007-08-08 14:13:32 · answer #2 · answered by mr_moose_man 3 · 0 0

You can use a String for the word and a StringBuffer for the guessed word like this:

Call this myprog.java

import java.io.*;

public class myprog {
// These should be same length
public static final String word = new String("house");
public static StringBuffer guessed = new StringBuffer("-----");
public static final int MAX_GUESSES = 5;

public static void main(String args[]){
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
String used = new String();
int pos, i=0;
char guess;

try{
while( ( i {//keep going till no more guesses or word guessed
System.out.println( "\nGuess letter");
guess = br.readLine().charAt(0);
if( used.indexOf( guess) != -1) {
System.out.println( guess + " already guessed - try again\n");
}
else {
pos = word.indexOf( guess);
if(pos == -1) {
System.out.println( guess + " not there\n");
i++;
}
else {
System.out.println( guess + " is there!\n");
guessed.setCharAt( pos, guess);
}
used += guess;
System.out.println( "Word: " + guessed + " Guesses: " + used + "\n");
}
}
if( i == MAX_GUESSES) {
System.out.println( MAX_GUESSES + " wrong guesses");
}
else {
System.out.println( "Word guessed correctly with " + i + " bad guesses");
}
System.out.println( "Word was " + word);
}catch (IOException e){
e.printStackTrace();
}
}
}

So you only need to modify the first 3 variables.
By using a String, you only need to use indexOf to see if a char exists in the string as opposed to a switch statement.
StringBuffer is more useful when you want to insert characters at specific posiitons - in this case we want to show

h--s-

if the user has already guessed these letters.
We have a string used which displays letters already guessed.

The output looks like this:

Guess letter
a
a not there

Word: ----- Guesses:a

Guess letter
e
e is there!
Word: ----e Guesses:ae

Guess letter
h
h is there!
Word: h---e Guesses: aeh

Guess letter
h
h already guessed - try again

Guess letter
o
o is there!
Word: ho--e Guesses: aeho

Guess letter
u
u is there!
Word: hou-e Guesses: aehou

Guess letter
z
z not there
Word: hou-e Guesses: aehouz

Guess letter
s
s is there!
Word: house Guesses: aehouzs

Word guessed correctly with 2 bad guesses
Word was house

Run this to find out happens if you run out of guesses.

2007-08-09 06:09:34 · answer #3 · answered by E.M.Bed 5 · 0 0

look the user will enter any word so you donot have to declare what the char are . the string the user will enter will consist of chars so you could use the command "name of the string".charAt(the index); and it will return the char at that position
that is it you will enter the string and every time the user will enter a letter you will go throught a for loop (use "name of the string".length() for the condition in the for loop) to check if that letter exist in the word and if it exist then you should make a boolean to true else make it false to be able to control when the changes occur to the hanged man

2007-08-08 14:35:04 · answer #4 · answered by OSAMA 1 · 0 0

fedest.com, questions and answers