Stewart Clay Stewart Clay - 2 months ago 3
Java Question

Hangman loop issue

I have been asked to create a hangman game. I am having a problem. when i ask the user if they would like to play again the user will enter Y but the game wont restart the correct way as it wont pick a new word and just goes on to asking to guess the letter and when the letter is enter it then stops the game and asks the user if he wants to play again. Could someone please tell me how to loop my program so that the user can play the game again.
package hangman.
thanks for future reply's!!

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class HangMan {

public static void main(String args[]) throws IOException {

Scanner keyboard = new Scanner(System.in);
int random = (int) (Math.random() * 5);
String s = null;
InputStream input = null;
int play = 0;
Path file = Paths.get("H:\\Varsity work\\Java
Programming\\Programs\\HangMan\\src\\hangman\\HangMan.txt");
input = Files.newInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
ArrayList<String> lines = new ArrayList<String>();
while ((s = reader.readLine()) != null) {
lines.add(s);
}
String[] linesArray = lines.toArray(new String[lines.size()]);
String[] randomWord = new String[1];
while (play == 0) {
System.err.printf("Welcome to hangman.\n");
randomWord[0] = linesArray[random];
System.out.println(randomWord[0]);
Random ran = new Random();
String word = randomWord[ran.nextInt(randomWord.length)];
char[] CharArr = word.toCharArray();
char[] dash = word.toCharArray();
for (int i = 0; i < dash.length; i++)
{
dash[i] = '-';
System.out.print(dash[i]);
}

for (int i = 1; i <= dash.length; i++)

{
System.out.printf("\nGuess a Letter:");
char userLetter = keyboard.next().charAt(0);

for (int j = 0; j < CharArr.length; j++)

{
if (userLetter == dash[j])

{
System.out.println("this word already exist");
}

else if (userLetter == CharArr[j])

{
dash[j] = userLetter;
i--;
}
}
System.out.print(dash);
if (word.equals(new String(dash)))
{
System.out.println("\nYou have guessed the word correctly!");
System.out.println("Play adian? (y/n)");
String name = keyboard.next();

if(name.equals("y"))

{
play = 0;

}

else if(name.equals("n"))

{
play = 1;
return;
}
}
}
}
}
}

Answer

With your current logic it's impossible because your code is to messy. You have to divide the code into methods. I would divide it into methods the following way:

setUpGame();
game();
cleanUpAfterGame();

setUpGame() {
    chooseWord();
    paintBasicHangman();
}

game() {
    while (alive) {
        readLetterFromUser();
        if (missedLetter) {
            paintNextPartOfHangman();
        } else {
            redraw();
            if (won()) {
                return true;
            }
        }
    }
    return false;
}

controller() {
    do {
        setUpGame();
        game();
        cleanUpAfterTheGame(); //optional
        wantsNewGame?();
    } while (userWantsToPlay)
Comments