/*the file that im reading from has 40 lines of words. I need to read from file whatever the size of the file and assign the amount of word from the file to the array size. thank you in advance
*/
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
printIntro();
String[] words = new String[40];// size of the array
readWords(words);// this method store all words in an array
String secretWord = getWord(words);// this method choose a random word
char status[] = new char[secretWord.length()];// takes the length
for (int i = 1; i < secretWord.length(); i++) {//set the limit of the for loop
status[i] = '?';//puts a ? symbol in every token of the string
}
Scanner console = new Scanner(System.in);
int tries = 6;//hold number of misses in the program
while (tries > 0) {
System.out.println("Enter a letter:");
char guessLetter = (console.next()).charAt(0);//hold a char and store it in guessletter
int hits = processGuess(guessLetter, secretWord, status);//this method test whether the guessletter was found in the string and returns the number of char found
displayWord(secretWord, status);//this method display the word found and the misses
if (hits > 0) {//test if the char was found
System.out.println("found!" + guessLetter);
boolean finished = true;//holds value to exit for loop
for (int i = 0; i < secretWord.length(); i++) {
if (status[i] == '?') {//test whether the array as symbol ?. if it does then the word is not complete
finished = false;//false indicates that the array still have unsolved letters
}
}
if (finished) {//if boolean is true finish this program
System.out.println("congratualition");
break;//exit while loop
}
} else if (hits == 0) {//if hits don't execute the letter was not found.
System.out.println("sorry no " + guessLetter);
tries--;//decrease the number of misses
}
System.out.println(tries + " remaining misses");
if (tries == 0) {//execute when all tries have being used.
System.out.println("Sorry you lose, the word was \'"
+ secretWord + "\'");
}
}
}
public static void printIntro() {//prints the introduction of the program
System.out.println("Welcome to hangman try to guess the secret word,"
+ " you have 6 chances.");
}
public static void readWords(String[] words) {//read all the words from the file
File file = new File("words.txt");//set direction of the file
try { //
Scanner console = new Scanner(file);//scanner for the file
int index = 0;//to fill in all the elements of the array
while (console.hasNextLine()) {
String word = console.nextLine();
words[index] = word;//store word in the array
index++;//move index of the array
}
} catch (FileNotFoundException e) {//throw exception not found
e.printStackTrace();
}
}
public static String getWord(String[] words) {//method would pick a random word from file
Random rand = new Random();
int index = rand.nextInt(40);//choose any element
String secretWord = words[index];//choose a random word from a words array
return secretWord;//return the random word
}
public static int processGuess(char guess, String secretWord, char[] status) {//this method test whether the letter was found
int count = 0;
for (int i = 0; i < secretWord.length(); i++) {
if (guess == secretWord.charAt(i)) {//test if the the user input guess letter was found
count++;
status[i] = '+';//store + in every element found
}
}
return count;
}
public static void displayWord(String secret, char[] status) {
for (int i = 0; i < secret.length(); i++) {
if (status[i] == '+') {//if it has a + then the letter was found
System.out.print(secret.charAt(i));//print the letter
} else {
System.out.print("_");//if not found
}
}
System.out.println();//to get a new blank line
}
}