Java Have you heard of the game of Hangman Well you are goin

Java

Have you heard of the game of Hangman? Well, you are going to write a program that will allow you to play a game of Hangman against the computer. After you play, you will be asked if you wish to play a brand new game, and if so, the program will start all over again. Here\'s how the program should work.

You will need 2 classes: a HangmanGame (driver) class and a Hangman (domain) class.

In the HangmanGame - driver class:

1. At the very beginning and after each game of Hangman, your program will display a menu that looks like this:

Do you wish to play a game of Hangman? (enter \"yes\" or \"no\")

If the user replies \"no,\" the program will end immediately.


2. Once the user replies \"yes,\" the driver will read a small file of 5 words, and will pass them as values to the constructor of the Hangman, when it instantiates a new Hangman - domain object.  In the constructor of the Hangman, the 5 values will be stored in 5 different variables. There is a method called selectSecretWord() which will be called to generate a random number between 1 and 5, used to select one of five words as the \"secret word\" for this game. The secret word will be available with a getter method of the Hangman object.   

3.)Back in the driver class, using a getter method from the Hangman object, the program will count the number of letters in the word, and display that many underscores on the screen, looking like this:

Here is the word for you to guess: _ _ _ _ _ _ _ (one underscore for each letter in the word)
You have x-number of guesses before you lose.  (x-number is calculated as the number of letters in the word times 3).

What is your next letter guess? (enter 1 letter only)

3.) Upon the user entering a letter, your program will check if the letter exists in the secret word (hint: use the String method indexOf(String str, int fromIndex) ). For every location where the letter was found in the word, put the letter in place of the underscore. Each time the user enters a letter, subtract 1 from the total number of guesses left. Keep displaying the same prompt as before, with the updates of the letters in the secret word and the count of guesses decrementing.

Here is the word for you to guess: _ a _ _ _ e _ (one underscore for each letter in the word).

You have X-number of guesses left before you lose.  (x-number is calculated as the number of letters in the word times 3).

What is your letter guess? (enter 1 letter only)

4.) The game is over when either the number of guesses left is equal to 0, or when all the letters in the secret word are correctly guessed. Display one of the following two messages, depending on whether the user guessed the word correctly or not:

You correctly guessed the word. Congratulations!

or

Sorry, you did not guess the word. Better luck next time.

5.) Then, display the original menu again, and if the user answers \"yes\" start all over again.

Do you wish to play a game of Hangman? (enter \"yes\" or \"no\")

If the user replies \"no,\" the program will end immediately. If the user replies \"yes,\" the program will loop again, calling the selectSecretWord() method of the Hangman domain class, to reset the secret word. Then, you will loop to step 3 above, and will start the guessing game all over again with the new word.

6.) Extra credit #1: Keep track of how many games of Hangman were won by the user out of the total number of games played.

7.) Extra credit #2: Write to an output file, with the secret word, and the number of guesses it took the user to win. The file\'s content should look like this:

Secret Word: Graduation Number of Guesses: 25

The output file should be appended to each time the program runs. It should not be overwritten.

The following message would be displayed when the user no longer wishes to keep playing:

You have won x out of y games of Hangman. Keep playing! (where x is the total number of times the secret word was guessed correctly, and y is the total number of games played.)

Solution


/**Copy this code and paste in a file with name HungmanGame.java
* Then Compile and execute.(I used * instead of _ while displaying invisible word for better clarity)**/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import java.util.Random;

class Hangman {
  
   private String[] values = null;
   private int selectSecretWordIndex;
   private Random random;
  
   public Hangman(String[] values) {
       super();
       this.values = values;
       random = new Random();
   }
  
   public void selectSecretWord(){
       selectSecretWordIndex = random.nextInt(values.length);
   }
  
   public String getSecretWord(){
       return values[selectSecretWordIndex];
   }
}

public class HungmanGame {

   public static void main(String args[]) throws IOException{
       int noOfGamesPlayed = 0, noOfGamesWon = 0;
       String inputFilePath = \"/home/kumar/Desktop/input.txt\";  
       String outputFilePath = \"/home/kumar/Desktop/output.txt\";
       Scanner sc = new Scanner(System.in);
       PrintWriter pw = new PrintWriter(new FileWriter(outputFilePath),true);
       while(true){
           System.out.print(\"Do you wish to play a game of Hangman? (enter \\\"yes\\\" or \\\"no\\\")\");
           String response = sc.next();
           if(response.equalsIgnoreCase(\"no\")){
               System.out.println(\"You have won \"+noOfGamesWon+\" out of \"+noOfGamesPlayed+\" games of Hangman. Keep playing!\");
               break;
           }
           noOfGamesPlayed++;
           String[] values = readFile(inputFilePath);
           Hangman hangman = new Hangman(values);
           hangman.selectSecretWord();
           String selectedWord = hangman.getSecretWord();
           StringBuffer userWord = new StringBuffer(new String(new char[selectedWord.length()]).replace(\"\\0\", \"*\"));
           int noOfchances = selectedWord.length() * 3;
           int invisibleCount = selectedWord.length();
           while(noOfchances > 0 && invisibleCount >0){
               System.out.println(\"Here is the word for you to guess:\"+userWord);
               System.out.println(\"You have \"+noOfchances+\"-number of guesses before you lose.\");
               System.out.print(\"What is your next letter guess? (enter 1 letter only)\");
               char guess = sc.next().charAt(0);
               int index = selectedWord.indexOf(guess);
               while(index != -1 ){
                   if(userWord.charAt(index) == guess){
                       index = selectedWord.indexOf(guess, index);
                   } else{
                       userWord.setCharAt(index, guess);
                       invisibleCount--;
                       break;
                   }
               }
               noOfchances--;
           }
           if(invisibleCount == 0){
               noOfGamesWon++;
               pw.println(\"Secret Word: \"+selectedWord+\" Number of Guesses: \"+ (selectedWord.length() * 3 - noOfchances));
               System.out.println(\"You correctly guessed the word. Congratulations!\");
           } else{
               System.out.println(\"Sorry, you did not guess the word. Better luck next time.\");
           }
       }
       sc.close();
       pw.close();
   }
   private static String[] readFile(String filePath) throws FileNotFoundException{
       int maxWords = 5;
       String[] values = new String[maxWords];
       Scanner scanner = new Scanner(new File(filePath));
       for(int index=0; index < maxWords && scanner.hasNext() ; index++){
           values[index] = scanner.next();
       }
       scanner.close();
       return values;
   }
}

Java Have you heard of the game of Hangman? Well, you are going to write a program that will allow you to play a game of Hangman against the computer. After you
Java Have you heard of the game of Hangman? Well, you are going to write a program that will allow you to play a game of Hangman against the computer. After you
Java Have you heard of the game of Hangman? Well, you are going to write a program that will allow you to play a game of Hangman against the computer. After you

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site