What my problem is I don't know how do I set it up so that when the entire word has been guessed I can stop the game and tell the user they won or lost.
I tried adding in another if statement after maxTries--;
where it says
if (maxTries == 0) {
println("You lose.")
but the message doesn't show up.
Other problem is that the letters being guessed are not being recorded properly, I thought it was fine but after testing it a few times the letters I've guessed would disappear from the display making the user retype them. And the while loop doesn't stop after finishing my guesses or when the guesses run out.
I could use some advice on this, and I'm just being honest this is for my cs class.
Thanks in advance!
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
//do while loop that has worked great before with lab2
boolean option = true;
String choice;
do{
//display welcome + instructions
Welcome();
//display everything else
everything();
//repeats the entire program if user presses y or ends it if presses n
System.out.println("Play again? (y/n)");
choice = input.nextLine();
if (choice.equals("y")){
option = true;
}
if (choice.equals("n")){
option = false;
System.out.println("Goodbye!");
}
}while (option == true);
}
public static void Welcome() {
System.out.println("Welcome to a game of Hangman!");
//welcome message and simple instructions.
System.out.println("Instructions are simple. Guess what the word is, you have 6 guesses. Use lower case letters only" + "\rand do not repeat the same letters. Once all guesses are used unsuccesfully then you lose!\n\n\n");
}
public static void everything() throws FileNotFoundException{
//create scanners
Scanner filereader = new Scanner (new File("C:/CS/wordFile.txt"));
Scanner input = new Scanner(System.in);
//word is now next word in .txt
String word = filereader.next();
//make while loop to count down the loss of the user
int maxTries = 6;
while (maxTries > 0) {
//build is now the length of whatever word is.
StringBuilder build = new StringBuilder(word.length());
//sets up word to print out in underscores
for(int i = 0; i < word.length(); i++) {
build.append("_");
}
//set char array
char[] wordArray = word.toCharArray();
//count letters in word
int charCount = word.length();
//while loop to count words greater than 0 and goes on from there to
while(charCount >= 0){
//that build being turned into a string
System.out.println(build.toString());
//ask user for letter
System.out.println("Enter letter: ");
//store user input as a char
char guessChar = input.next().toCharArray()[0];
boolean foundletter = false;
for(int i = 0; i < word.length(); i++)
//comparing user input with the wordArray
if(guessChar == wordArray[i]) {
//changing the underscore with the correct word
build.setCharAt(i, guessChar);
foundletter = true;
}
//unsuccessfully print out letters tried.
charCount--;
System.out.println("Letters tried: " + guessChar);
//subtract number of maxTries for every incorrect guess
if (foundletter == false){
maxTries--;
if (maxTries == 0){
System.out.println("You Lose.");
}
}
System.out.println("You have " + maxTries + " " + "wrong guesses left");
}
}
}
}