import java.io.*; //for file
import java.util.*; //for scanner
public class Lab65 {
static String userG = "";
public static void main(String[] args)throws FileNotFoundException{
int wrong = 0; // keeps track of user guesses
String word = readwords();
boolean temp = false;
String[] words = new String[word.length()];
String[] words2 = new String[word.length()];
//methods to be used
userGuess(word, words, words2);
printhm(wrong);
//introduce user to game
System.out.println("Welcome to hangman!");
System.out.println("Begin guessing letters!");
System.out.println("These letters have already been guessed!");
userGuess(words);
//keep track of wrong user guesses
while(wrong <6) {
temp = add(words, words2);
userGuess(words);
if(temp == false) {
wrong++;
printhm(wrong);
System.out.println("Incorrect guess!" + (6 - wrong) + " more guesses left!");
System.out.println();
System.out.println(userG);
System.out.println();
System.out.println(" These letters have already been guessed!");
System.out.println("Make another guess!");
}else{ //user guess is correct
printhm(wrong);
System.out.println("Correct guess!");
System.out.println(" These letters have already been guessed!");
System.out.println(userG);
System.out.println("Make another guess!");
}
}
}
//display hangman
public static void printhm(int wrong) {
//hangman display
if(wrong == 0) {
System.out.println(" ------");
System.out.println(" | |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
}
else if (wrong == 1){
System.out.println(" --------");
System.out.println(" | |");
System.out.println(" 0 |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
}
else if (wrong == 2) {
System.out.println(" --------");
System.out.println(" | |");
System.out.println(" 0 |");
System.out.println(" | |");
System.out.println(" |");
System.out.println(" |");
}
else if (wrong == 3) {
System.out.println(" --------");
System.out.println(" | |");
System.out.println(" 0 |");
System.out.println("/| |");
System.out.println(" |");
System.out.println(" |");
}
else if (wrong == 4){
System.out.println(" ---------");
System.out.println(" | |");
System.out.println(" 0 |");
System.out.println("/|\\ |");
System.out.println(" |");
System.out.println(" |");
}
else if (wrong == 5){
System.out.println(" ---------");
System.out.println(" | |");
System.out.println(" 0 |");
System.out.println("/|\\ |");
System.out.println("/ |");
System.out.println(" |");
}
else {
System.out.println(" ---------");
System.out.println(" | |");
System.out.println(" 0 |");
System.out.println("/|\\ |");
System.out.println("/ \\ |");
System.out.println(" |");
System.out.println( "You lost! Better luck next time!!" );
}
}
// keep track of wrong guesses
public static String readwords() throws FileNotFoundException {
Scanner input = new Scanner(new File("hangman.txt"));
String[] word = new String[10];
String rword = "";
for(int i = 0; i < word.length; i++){
if (input.hasNext()) {
word[i] = input.next();
}else {
break;
}
}
Random rand = new Random();
int r = rand.nextInt(9);
rword = word[r];
return rword;
}
public static void userGuess(String y, String[] m, String[] words2){
for (int i=0; i < y.length(); i++){
m[i] = "-";
}
for (int x = 0; x < y.length(); x++){
if (x + 1 < y.length())
words2[x] = y.substring(x, x + 1);
else
words2[x] = y.substring(x, y.length());
}
}
public static boolean add(String[] m, String[] ma) { //find if letters are in the word
Scanner console = new Scanner(System.in);
String letters = console.next();
boolean found = false;
for(int i = 0; i < ma.length; i++) {
if(ma [i].equals(letters)){
m[i] = letters;
found = true;
}
}
if(found) {
return true;
}
else {
return false;
}
}
public static void userGuess(String[] m) {
for(int i = 0; i < m.length; i++) {
System.out.print(m[i] );
}
}
}