Question

/**Instructions You are going to write a program that will allow the user to play a...

/**Instructions
You are going to write a program that will allow the user to play a game like hangman (Links to an external site.)Links to an external site., where your program chooses a word from a text file and then displays blanks for the user to guess the word. The user will have a set number of incorrect guesses and if they do not guess the word within that number of tries, they will lose the game.

Max Wrong
You should define a class constant for the maximum number of incorrect guesses that the user gets before they lose the game. You may choose this number. The more guesses the user gets, the easier the game.

You should print the number of tries the user gets in the intro text to the game.
Max Words
You should define a class constant for the maximum number of words in the dictionary.txt file. This constant will be used when you construct the array to hold all the words in the file.

Methods
At a minimum, your program should have the following methods in addition to the main:

a method to load the input from a file into an array
a method to pick the word for this game from the array of words
a method to print the word
a method to play one game of hangman
a method to check if the word is guessed
a method to check if the guess was in the word
Method: Load Input
You need to create a method that will read in words from a text file called dictionary.txt

The dictionary.txt file has 127,100 words in it so you can construct an array of the appropriate size before reading in all the words.
Your method should return an array of all the words
Your method should receive a String parameter that indicates the file to be loaded.
Note that this method, in addition to the main, will need to throw a FileNotFoundException in the case that dictionary.txt does not exist
Method: Pick Word
You should have a method that takes an array of words as a parameter and a Random number generator object. This method should return a random word to be guessed. (This method returns a String.)

Method: Play game
You should have a method that plays one game of hangman.

You are expected to create a boolean array to track whether or not each letter in the word should be displayed to the user. When a spot in this array is true, you display the letter to the user. When a spot in this array is false, you display an underscore ("_") to the user.
The user should be prompted to guess a letter as long as they have not guessed incorrectly more times than the MAX_WRONG constant and they have not already guessed the word.
If the user's guess is not in the word, they should lose one of their tries for guessing; however, if the user does not guess incorrectly, they do not lose a try.
A message should be printed to the console to indicate whether the user's guess was in the word. If it is, print how many times it occurred. If it isn't, print the number of tries they still have before the game ends.
If the user does not solve the word correctly, the correct word should be displayed at the end of the game.
Method: Print Word
This method should print the word to the screen according to the true/false values contained in the boolean array that is passed to it as a parameter.

Method: Check if Solved
This method should return true if all the boolean values in the array are true; false, otherwise. The puzzle is solved when all the characters in the puzzle are displayed to the user.

Method: Check Guess
This method should check if the user's guess is in the word and should return the number of occurrences of the guess in the word.

Hints and Other Notes
Note that you can solve this program creating exactly two arrays (one for the words read in from the file and one for the true/false values of whether or not to display a character). If you solve the problem this way, you will utilize string methods (like charAt) to look at each letter in the word String.
Remember that to lowercase a string, you say s.toLowerCase(), where s is a String. To lowercase a character, you say Character.toLowerCase(c), where c is a char
Remember that arrays are passed by reference to methods, so if you change an array in the method you do NOT need to return it to see the changes take place.


**/

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Hangman {
  

   // declare constants
   static int MAX_GUESS=5;
   static int MAX_WORDS=127100;
   // MAX_WORDS
   // MAX_GUESSES
   /**
* This method may throw a FileNotFoundException if
* @throws FileNotFoundException
*/
   public static void main(String[] args) throws FileNotFoundException{
       System.out.println("Lets play a game of hangman!");
       System.out.println("you can guess up to "+ MAX_GUESS);

       String parts=loadInput(fileIn);
      
       }

      
      
  
  
   /**
   * This method reads in a text file and constructs a String array with
   * the contents of the file
   * @param fileName to be opened by method
   * @return a String array filled with text
   * @throws FileNotFoundException
   */
   public static String[] loadInput(String fileName) throws FileNotFoundException{
       int[] StringArray = new int[MAX_WORDS];

       Scanner fileIn=new Scanner(new File("dictionary.txt"));
       //int lineCount=0;
       while (fileIn.hasNextLine())
       {
           String lines= fileIn.nextLine();
          
           //split in new line and print it out
       String[] parts=lines.split(" ");
       System.out.println(Arrays.toString(parts));

          
          

       }
       fileIn.close();

      


      
       return null;
   }
   /**
   * This method receives the array of words that were contained in the dictionary
* and a random number generator
   * @param words - array of words to select the secretWord from
   * @param gen - Random number generator to select index for word in array
   * @return the secretWord
   */
   public static String pickWord(String[] words, Random gen) {
      
      
       return "";
   }
   /**
* This method manages the game play and calls the other methods in this class
   * @param secretWord - the word to be guessed
   */
   public static void playGame(String secretWord) {
      
   }
   /**
   * This method uses a parallel array of true/false values matching the number of
* letters to be guessed. If the letters array contains a true value, then the corresponding
* letter in the secretWord will be printed. If the letters array contains a false value,
* then a dash will be printed for the corresponding letter in the secretWord
   * @param letters - parallel array indicating whether the letter has been guessed
* true - print the guessed letter, false - print a dash
   * @param secretWord - parallel "array" of characters of letters to be guessed
   */
   public static void printWord(boolean[] letters,String secretWord) {
      
   }
   /**
   * This method checks if all the elements in the letters array are true.
* If all are true, all letters have been guessed and the game is over,
* otherwise false is returned
   * @param letters - true/false values indicating if the corresponding letter in
* secretWord has been guessed
   * @return true/false - true game over, false game not over
   */
   public static boolean gameOver(boolean [] letters) {
      
       return false;
   }
   /**
   * This method determines if the guessLetter is in the secretWord. If it is then it updates
* the corresponding indices in the letters boolean array and counts up the number of times
* the guessLetter occurred in the secretWord and returns this value
   * @param letters - boolean array indicating if letter has been guessed
   * @param secretWord - word to be guessed
   * @param guessLetter - the letter the player chose
   * @return count of the number of times the guessLetter is in the secretWord
   */
   public static int checkGuess (boolean[] letters, String secretWord, char guessLetter) {
      
       return 0;
   }
  
  
  

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package org.students;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Hangman {

   // declare constants
   static int MAX_GUESS = 5;
   static int MAX_WORDS = 127100;

   /*
   * Creating an Scanner class object which is used to get the inputs entered
   * by the user
   */
   static Scanner sc = new Scanner(System.in);

   // MAX_WORDS
   // MAX_GUESSES
   /**
   * This method may throw a FileNotFoundException if
   *
   * @throws FileNotFoundException
   */
   public static void main(String[] args) throws FileNotFoundException {
       System.out.println("Lets play a game of hangman!");
       System.out.println("you can guess up to " + MAX_GUESS);
       String fileIn = "dictionary.txt";
       String array[] = loadInput(fileIn);

       // Creating a random Class object
       Random r = new Random();

       String secretWord = pickWord(array, r);
       playGame(secretWord);

   }

   /**
   * This method reads in a text file and constructs a String array with the
   * contents of the file
   *
   * @param fileName
   * to be opened by method
   * @return a String array filled with text
   * @throws FileNotFoundException
   */
   public static String[] loadInput(String fileName)
           throws FileNotFoundException {
       int i = 0;
       String[] stringArray = new String[MAX_WORDS];

       Scanner fileIn = new Scanner(new File(fileName));
       // int lineCount=0;
       while (fileIn.hasNextLine()) {
           String word = fileIn.next();

           stringArray[i] = word;
           i++;

       }
       fileIn.close();

       return stringArray;
   }

   /**
   * This method receives the array of words that were contained in the
   * dictionary and a random number generator
   *
   * @param words
   * - array of words to select the secretWord from
   * @param gen
   * - Random number generator to select index for word in array
   * @return the secretWord
   */
   public static String pickWord(String[] words, Random gen) {
       int random = gen.nextInt((words.length) + 1);
       return words[random];
   }

   /**
   * This method manages the game play and calls the other methods in this
   * class
   *
   * @param secretWord
   * - the word to be guessed
   */
   public static void playGame(String secretWord) {
       int cnt = 0, tot = 0;
       boolean letters[] = new boolean[secretWord.length()];
       printWord(letters, secretWord);
       System.out.print("Enter a guess letter :");
       char guessLetter = sc.next(".").charAt(0);

       while (true) {
           cnt = checkGuess(letters, secretWord, guessLetter);
           boolean b = gameOver(letters);

           if (!b) {
               printWord(letters, secretWord);
              
               System.out.print("Enter a guess letter :");
               guessLetter = sc.next(".").charAt(0);
              

           } else
               break;
       }
   }

   /**
   * This method uses a parallel array of true/false values matching the
   * number of letters to be guessed. If the letters array contains a true
   * value, then the corresponding letter in the secretWord will be printed.
   * If the letters array contains a false value, then a dash will be printed
   * for the corresponding letter in the secretWord
   *
   * @param letters
   * - parallel array indicating whether the letter has been
   * guessed true - print the guessed letter, false - print a dash
   * @param secretWord
   * - parallel "array" of characters of letters to be guessed
   */
   public static void printWord(boolean[] letters, String secretWord) {
       for (int i = 0; i < letters.length; i++) {
           if (letters[i] == false) {
               System.out.print("_");
           } else {
               System.out.print(secretWord.charAt(i));
           }
       }
   }

   /**
   * This method checks if all the elements in the letters array are true. If
   * all are true, all letters have been guessed and the game is over,
   * otherwise false is returned
   *
   * @param letters
   * - true/false values indicating if the corresponding letter in
   * secretWord has been guessed
   * @return true/false - true game over, false game not over
   */
   public static boolean gameOver(boolean[] letters) {

       return false;
   }

   /**
   * This method determines if the guessLetter is in the secretWord. If it is
   * then it updates the corresponding indices in the letters boolean array
   * and counts up the number of times the guessLetter occurred in the
   * secretWord and returns this value
   *
   * @param letters
   * - boolean array indicating if letter has been guessed
   * @param secretWord
   * - word to be guessed
   * @param guessLetter
   * - the letter the player chose
   * @return count of the number of times the guessLetter is in the secretWord
   */
   public static int checkGuess(boolean[] letters, String secretWord,
           char guessLetter) {
       int cnt = 0;
       for (int i = 0; i < letters.length; i++) {
           if (letters[i] == false) {
               if (secretWord.charAt(i) == guessLetter) {
                   cnt++;
                   letters[i] = true;
               }
           }
       }
       return cnt;
   }

}
_________________Thank you

Add a comment
Know the answer?
Add Answer to:
/**Instructions You are going to write a program that will allow the user to play a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT