Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with java program assignment. I need help with these parts of the java

ID: 3692960 • Letter: N

Question

Need help with java program assignment. I need help with these parts of the java program assignment (the bold words):

-------------------------------------------------------------------------------------------------------------------

Write a java program that is able to pick a random word from a list of words contained in a file and lets the user who is interacting with the game guess it one character at a time.

Upon startup, the player must be prompted for their name.

While playing, the player may guess at most 5 incorrect times. If the player guesses a character that has already been tried, he or she must NOT be penalized for doing so (i.e., it will not count as a turn).

The game ends when the full word has been guessed (player wins) or when five incorrect guesses have been made.

While reading the input file, the first line of the input file must be skip and lines starting with a semicolon (;) or with a hashmark (#) are comments and must be ignored or skipped.

Upon completion of the game, the player's name, the number of turns played, the word that should have been guessed, and an indicator for win/lose must be added to a file.

----------------------------------------------------------------------------------------------------------------------

package hangman;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class Hangman {

   public String getName() {
       System.out.print("Enter your name: ");
       Scanner in = new Scanner(System.in);
       String name = in.next();
       return name;
   }

   public void playGame(String word) throws IOException {
       Scanner sc = new Scanner(System.in);
       FileWriter writer = new FileWriter("wordsnew.txt");
       char[] guessArray = setDefaultCharArray(word.length());
       char[] wordArray = word.toCharArray();
       int numGuess = 0;
       int numCorrect = 0;
       String name = getName();
       while (!isWordComplete(guessArray) && numGuess < 5) {
           System.out.print("Current word: ");
           printCharArray(guessArray);
           System.out.println();
           System.out.print("Enter your guess: ");
           char guess = sc.next().charAt(0);
           boolean found = false;
           for (int i = 0; i < guessArray.length; i++) {
               if (guessArray[i] == '_' && wordArray[i] == guess) {
                   numCorrect++;
                   guessArray[i] = wordArray[i];
                   found = true;
               }
           }
           if (!found) {
               numGuess++;
           }
           System.out.println(numCorrect + " letters found");
           System.out.println();
           if (numGuess == 5) {
               System.out.println();
               System.out.println("Sorry, you lost.");
           } else if (isWordComplete(guessArray))
               System.out.println("You had won");
       }
       if (numGuess == 5) {
           writer.write("Sorry, you lost.");
       } else if (isWordComplete(guessArray))
           writer.write("You had won");
       writer.close();

       System.out.println("Name: " + name);
       System.out.println("The word is " + word);
       System.out.println("Total " + (numGuess) + " guesses");
       System.out.println("Total " + (numGuess + numCorrect) + " attempts");
   }

   /**
   * This method counts the number of words in a given file and returns that
   * number.
   *
   * @methodName getNumberOfWordsInFile
   * @param fileName
   * @return wordCount
   * @throws FileNotFoundException
   */
   public int getNumberOfWordsInFile(String fileName) throws FileNotFoundException {
       int wordCount = 0;
       File file = new File("word.txt");
       Scanner sc = new Scanner(new FileInputStream(file));
       int count = 0;
       while (sc.hasNext()) {
           sc.next();
           wordCount++;
       }
       return wordCount;
   }

   /**
   * This method accepts a file name. First it gets the number of words in the
   * file by calling the getNumberOfWordsInFile method. Then it creates a
   * String array based on how many words are in the file. Next, it fills the
   * array with all of the words from the file. Then it returns the array.
   *
   * @methodName getWordsFromFile
   * @param fileName
   * @return wordArray
   * @throws FileNotFoundException
   */
   public String[] getWordsFromFile(String fileName) throws FileNotFoundException {
       int wordCount = getNumberOfWordsInFile("word.txt");
       String[] wordArray = new String[wordCount];
       File file = new File("word.txt");
       Scanner sc = new Scanner(new FileInputStream(file));
       int i = 0;
       while (sc.hasNext()) {
           wordArray[i] = sc.next();
           i++;
       }
       sc.close();
       return wordArray;
   }

   /**
   * This method returns a random string from a given String array
   *
   * @methodName getRandomWord
   * @param wordArray
   * @return randomWord
   */
   public String getRandomWord(String[] wordArray) {
       String randomWord = null;
       Random rd = new Random();
       try {
           int count = getNumberOfWordsInFile("word.txt");
           int randNo = rd.nextInt(count - 1);
           randomWord = wordArray[randNo];
           while (count - 1 < randNo) {
               if (count - 1 < randNo) {
                   randomWord = wordArray[randNo];
                   break;
               } else
                   randNo = rd.nextInt(count - 1);
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
       return randomWord;
   }

   /**
   * This method accepts an int and creates a char array of that size Then it
   * sets each element in the array to an underscore '_' Then it returns the
   * char array.
   *
   * @methodName setDefaultCharArray
   * @param size
   * @return charArray
   */
   public char[] setDefaultCharArray(int size) {
       char[] charArray = new char[size];
       for (int i = 0; i < charArray.length; i++)
           charArray[i] = '_';
       return charArray;
   }

   /**
   * This method takes the given char array and uses a loop to print out each
   * element all on the same line.
   *
   * @methodName printCharArray
   * @param charArray
   */
   public void printCharArray(char[] charArray) {
       for (int i = 0; i < charArray.length; i++)
           System.out.print(charArray[i]);
   }

   /**
   * This method takes the given char array and checks if any of the elements
   * are equal to an underscore '_' It returns true if the char array contains
   * an underscore '_' and false if it does not.
   *
   * @methodName isWordComplete
   * @param charArray
   * @return isComplete
   */
   public boolean isWordComplete(char[] charArray) {
       boolean isComplete = true;
       for (int i = 0; i < charArray.length; i++) {
           if (charArray[i] == '_') {
               isComplete = false;
               break;
           }
       }
       return isComplete;
   }
}

package hangman;

import java.util.Scanner;

public class HangmanDriver {

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

   Hangman game = new Hangman();

   String[] wordArray = game.getWordsFromFile("word.txt");

   String word = game.getRandomWord(wordArray);

   game.playGame(word);

   }

}

Explanation / Answer

Hangman.java

package hangman;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Hangman {
   public String getName() {
       System.out.print("Enter your name: ");
       Scanner in = new Scanner(System.in);
       String name = in.next();
       return name;
   }

   public void playGame(String word) throws IOException {
       Scanner sc = new Scanner(System.in);
       FileWriter writer = new FileWriter("wordsnew.txt");
       char[] guessArray = setDefaultCharArray(word.length());
       char[] wordArray = word.toCharArray();
       int numGuess = 0;
       int numCorrect = 0;
       String name = getName();
       while (!isWordComplete(guessArray) && numGuess < 5) {
           System.out.print("Current word: ");
           printCharArray(guessArray);
           System.out.println();
           System.out.print("Enter your guess: ");
           char guess = sc.next().charAt(0);
           boolean found = false;
           for (int i = 0; i < guessArray.length; i++) {
               if (guessArray[i] == '_' && wordArray[i] == guess) {
                   numCorrect++;
                   guessArray[i] = wordArray[i];
                   found = true;
               }
           }
           if (!found) {
               numGuess++;
           }
           System.out.println(numCorrect + " letters found");
           System.out.println();
           if (numGuess == 5) {
               System.out.println();
               System.out.println("Sorry, you lost.");
           } else if (isWordComplete(guessArray))
               System.out.println("You had won");
       }
       if (numGuess == 5) {
           writer.write("Sorry, you lost.");
          
          
       } else if (isWordComplete(guessArray))
           writer.write("You had won");
      
       writer.write(" Name: " + name);
       writer.write(" The word is " + word);
       writer.write(" Total " + (numGuess) + " guesses");
       writer.write(" Total " + (numGuess + numCorrect) + " attempts");
       writer.close();
       System.out.println("Name: " + name);
       System.out.println("The word is " + word);
       System.out.println("Total " + (numGuess) + " guesses");
       System.out.println("Total " + (numGuess + numCorrect) + " attempts");
   }

   /**
   * This method counts the number of words in a given file and returns that
   * number.
   *
   * @methodName getNumberOfWordsInFile
   * @param fileName
   * @return wordCount
   * @throws FileNotFoundException
   */
   public int getNumberOfWordsInFile(String fileName)
           throws FileNotFoundException {
       int wordCount = 0;
       File file = new File("word.txt");
       Scanner sc = new Scanner(new FileInputStream(file));
       int count = 0;
       while (sc.hasNext()) {
           sc.next();
           wordCount++;
       }
       return wordCount;
   }

   /**
   * This method accepts a file name. First it gets the number of words in the
   * file by calling the getNumberOfWordsInFile method. Then it creates a
   * String array based on how many words are in the file. Next, it fills the
   * array with all of the words from the file. Then it returns the array.
   *
   * @methodName getWordsFromFile
   * @param fileName
   * @return wordArray
   * @throws FileNotFoundException
   */
   public String[] getWordsFromFile(String fileName)
           throws FileNotFoundException {
       // int wordCount = getNumberOfWordsInFile("word.txt");
       // String[] wordArray = new String[wordCount];
       List<String> wordsList = new ArrayList<String>();
       boolean firstline = true;
       File file = new File("word.txt");
       Scanner sc = new Scanner(new FileInputStream(file));
       int i = 0;
       while (sc.hasNext()) {
           // wordArray[i] = sc.next();
           // i++;
           if (firstline) {
               sc.next();
               firstline = false;
           } else {
               String data = sc.next();
               // Lines starting with a semicolon (;) or with a hash mark (#)
               // are comments and must be ignored or skipped
               if (data.charAt(0) != ';' && data.charAt(0) != '#'
                       && data.charAt(0) != '/') {
                   wordsList.add(data);
               }
           }
       }
       String[] wordArray = new String[wordsList.size()];
       for (int k = 0; k < wordsList.size(); k++) {
           wordArray[k] = wordsList.get(k);
       }
       sc.close();
       return wordArray;
   }

   /**
   * This method returns a random string from a given String array
   *
   * @methodName getRandomWord
   * @param wordArray
   * @return randomWord
   */
   public String getRandomWord(String[] wordArray) {
       String randomWord = null;
       Random rd = new Random();
       try {
           // int count = getNumberOfWordsInFile("word.txt");
           int count = wordArray.length;
           if (count != 1) {
               int randNo = rd.nextInt(count - 1);
               randomWord = wordArray[randNo];
               while (count - 1 < randNo) {
                   if (count - 1 < randNo) {
                       randomWord = wordArray[randNo];
                       break;
                   } else
                       randNo = rd.nextInt(count - 1);
               }
           } else {
               randomWord = wordArray[0];
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
       return randomWord;
   }

   /**
   * This method accepts an int and creates a char array of that size Then it
   * sets each element in the array to an underscore '_' Then it returns the
   * char array.
   *
   * @methodName setDefaultCharArray
   * @param size
   * @return charArray
   */
   public char[] setDefaultCharArray(int size) {
       char[] charArray = new char[size];
       for (int i = 0; i < charArray.length; i++)
           charArray[i] = '_';
       return charArray;
   }

   /**
   * This method takes the given char array and uses a loop to print out each
   * element all on the same line.
   *
   * @methodName printCharArray
   * @param charArray
   */
   public void printCharArray(char[] charArray) {
       for (int i = 0; i < charArray.length; i++)
           System.out.print(charArray[i]);
   }

   /**
   * This method takes the given char array and checks if any of the elements
   * are equal to an underscore '_' It returns true if the char array contains
   * an underscore '_' and false if it does not.
   *
   * @methodName isWordComplete
   * @param charArray
   * @return isComplete
   */
   public boolean isWordComplete(char[] charArray) {
       boolean isComplete = true;
       for (int i = 0; i < charArray.length; i++) {
           if (charArray[i] == '_') {
               isComplete = false;
               break;
           }
       }
       return isComplete;
   }
}

HangmanDriver.java

package hangman;

public class HangmanDriver {
   public static void main(String[] args) throws Exception {
       Hangman game = new Hangman();
       String[] wordArray = game.getWordsFromFile("word.txt");
       if (wordArray.length > 0) {
           String word = game.getRandomWord(wordArray);
           game.playGame(word);
       }
   }
}

word.txt

firstline
;nature
#success
/kumar
reddy
dance
dirty
melody

Output:

Enter your name: Pavan
Current word: _____
Enter your guess: e
1 letters found

Current word: _e___
Enter your guess: m
1 letters found

Current word: _e___
Enter your guess: r
2 letters found

Current word: re___
Enter your guess: d
4 letters found

Current word: redd_
Enter your guess: y
5 letters found

You had won
Name: Pavan
The word is reddy
Total 1 guesses
Total 6 attempts

wordsnew.txt

You had won
Name: Pavan
The word is reddy
Total 1 guesses
Total 6 attempts