Question

In: Computer Science

Write a program to allow a user to play the game Hangman. DO NOT USE AN...

Write a program to allow a user to play the game Hangman. DO NOT USE AN ARRAY

The program will generate a random number (between 1 and 4581) to pick a word from the file - this is the word you then have to guess.

Note: if the random number you generate is 42, you need the 42nd word - so loop 41 times picking up the word from the file and not doing anything with it, it is the 42nd you need. Here is how you will do this:

String word:

for (int k=0; k<41; k++)

{word=scnr.nextLine();

}//end loop and now pick up the word you want

word=scnr.nextLine() //this is the one you want

The user will be allowed to guess a letter as many times as it takes - but 10 wrong guesses and they lose!!

Eventually either they won or lost. In case they lost print out the answer.

Code language: Java using JGrasp or NetBeans

Solutions

Expert Solution

import java.util.Scanner;
import java.util.Random;
import java.io.*;

// class defination
class Hangman
{
   // main function
   public static void main(String[] args)
   {
       // variable declaration
       Random random = new Random();
       String word = new String();
       int wrongCount = 0;
       // game will run until user wants to exit or made 10 wrong guesses
       while(true)
       {
           // ask user for option weather want to play or exit
           System.out.println("\nChose an option \n1.Play \n2.Exit");
           Scanner scan = new Scanner(System.in);
           int option = scan.nextInt();
          
           // if player chose 2 program will terminat
           if(option == 2)
               System.exit(0);
          
           // generating random number between 1 - 4580
           int number = random.nextInt(4580) + 1;
           System.out.println("\nyou have to guess the word of no. : " + number);
          
           // asking user to guess the word
           System.out.print("Guess your word : ");
           String guessWord = scan.next();
           try
           {
               // opening word.txt file and reaching to the required line
               File file = new File("word.txt");
               Scanner scnr = new Scanner(file);
               for(int i = 0 ; i < number - 1 ; i++)
               {
                   word = scnr.nextLine();
               }
               // storing correct word to the variable word
               word = scnr.nextLine();
               // closing file
               scnr.close();
           }
           catch(FileNotFoundException e)
           {
               e.printStackTrace();
           }
           // checking if the guess word is correct or not
           if(guessWord.equals(word))
               System.out.println("Correct ");
           else
           {
               System.out.println("Wrong guess, correct answer is : " + word);
               wrongCount++;
           }
           // if user made 10 wrong guess terminat the program as mentioned in the question
           if(wrongCount == 10)
           {
               System.out.println("\nGame Over! you have made 10 wrong guesses\nBye Bye");
               System.exit(0);
           }
       }
   }
}

// for testing this code you need a word.txt file in the same directory where Hangman.java file is stored

// and word.txt file must have 4581 line of word as code is generating random number between 1 - 4081 according to question


Related Solutions

Write a program where a user of this program will play a game in which he/she...
Write a program where a user of this program will play a game in which he/she needs to guess a target number, which is a number that the program has randomly picked in the range that the user chooses. The program will repeatedly prompt for the guessed number and provide a clue whether the guessed number is bigger or smaller than the target number, until the guessed number equals the target number.
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play the game of rock, paper, scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly as difficult as it may appear. The way hangman works (for this assignment - we are doing a simplified game) is as follows: the computer will choose a word. (For this version, a word is selected from a static list encoded into the program -- so the words are pretty limited). Let's say the word is "cocoa". the computer shows the user how many...
Write a python program that will allow a user to draw by inputting commands. The program...
Write a python program that will allow a user to draw by inputting commands. The program will load all of the commands first (until it reaches command "exit" or "done"), and then create the drawing. Must include the following: change attributes: color [red | green | blue] width [value] heading [value] position [xval] [yval] drawing: draw_axes draw_tri [x1] [y1] [x2] [y2] [x3] [y3 draw_rect [x] [y] [b] [h] draw_poly [x] [y] [n] [s] draw_path [path] random random [color | width...
Design and implement a Python program which will allow two players to play the game of...
Design and implement a Python program which will allow two players to play the game of Tic-Tac-Toe in a 4x4 grid! X | O | X | O -------------- O | O | X | O -------------- X | X | O | X -------------- X | X | O | X The rules for this game is the same as the classic, 3x3, game – Each cell can hold one of the following three strings: "X", "O", or "...
Write a C++ program that : 1. Allow the user to enter the size of the...
Write a C++ program that : 1. Allow the user to enter the size of the matrix such as N. N must be an integer that is >= 2 and < 11. 2. Create an vector of size N x N. 3. Call a function to do the following : Populate the vector with N2 distinct random numbers. Display the created array. 4. Call a function to determines whether the numbers in n x n vector satisfy the perfect matrix...
Write a program in Basic to play the game of Nim with acom­puter.
Write a program in Basic to play the game of Nim with acom­puter.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT