I am trying to make a Hangman game in Java, but most I found on the internet are with a while loop, but since this is part of a chatbot, I need it to be with if-statements, however it is not working. It grabs the first word correctly, sends 6 dashes, but every guess is "wrong" and a life goes off. Does anyone know what I'm doing wrong? Thanks in advance!
private static String hangmanStarted = "0";
private static String hangmanInitialized = "0";
private static String hangmanSecret = "";
private static int hangmanLives = 10;
private void cmdHangman() {
if (hangmanStarted.equals("0")) {
try {
secretWord();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
hangmanStarted = "1";
sendMessage("Hangman has started!\nType: ''.guess letter''\nto guess a letter!");
}
}
private void cmdGuess() {
if (hangmanStarted.equals("1")) {
String input = lastMsg.replace("[^a-zA-Z]", "");
input = input.replace("guess", "");
input = input.toLowerCase();
try {
Hangman(input);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void Hangman(String input) throws IOException {
StringBuilder guessedLetters = new StringBuilder();
String dashes = secretDashes();
if (guessedLetters.toString().contains(input)) {
sendMessage("This word is already guessed");
} else {
if (hangmanSecret.contains(input)) {
guessedLetters.append(input);
if (hangmanSecret.equals(letterConversion(hangmanSecret, secretDashes(), input))) {
sendMessage("You win!");
hangmanInitialized = "0";
hangmanStarted = "0";
guessedLetters.delete(0, guessedLetters.length());
} else {
guessedLetters.append(input);
sendMessage("Letter was found!\nWord: " + letterConversion(hangmanSecret, secretDashes(), input));
}
} else {
guessedLetters.append(input);
hangmanLives = hangmanLives - 1;
sendMessage("Word: " + dashes + "\nLives: " + hangmanLives);
if (hangmanLives == 0) {
sendMessage("GAME OVER: You are dead.");
hangmanInitialized = "0";
hangmanStarted = "0";
hangmanLives = 10;
guessedLetters.delete(0, guessedLetters.length());
}
}
}
}
public static void secretWord() throws FileNotFoundException {
if (hangmanInitialized.equals("0")) {
hangmanInitialized = "1";
Scanner infile = new Scanner(new FileReader("hangWords.txt"));
hangmanSecret = infile.next();
infile.close();
}
}
public static String secretDashes() throws FileNotFoundException {
StringBuilder dashes = new StringBuilder();
for (int count = 0; count < hangmanSecret.length(); count++) {
dashes.append('-');
}
return dashes.toString();
}
public static String letterConversion(String secret, String dashes, String letter) {
StringBuilder conversion = new StringBuilder();
for (int index = 0; index < secret.length(); index++) {
char secretChar = secret.charAt(index);
secret = String.valueOf(secretChar);
char letterChar = letter.charAt(index);
if (secret.equals(letter))
conversion.setCharAt(index, letterChar);
dashes = conversion.toString();
}
return dashes;
}
What it should do:
I start Hangman with the command. I guess letters through the guess command. The input of the guess command gets forwarded to Hangman void. Hangman checks if the letter guessed already. If not, check if the the letter is in the secret word and display the guessed letters to the user. If it's not in the secret word, remove a life. If life is 0, end game.
PS. Yes, I know, I'm using strings instead of booleans, but it's the same thing and I did it temporarily to test if that was the problem, heh.
EDIT: This is not the same question. After changing that line of code, the problem is still there.