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.
Write a hangman program in c++.The game is played as follows: 1. The program selects a...
Write a hangman program in c++.The game is played as follows: 1. The program selects a random word for the user to guess (Read words for the user to guess into an array of strings from a file. The words are: revolutionary, meaning, recovery, compartment, trainer, pursuit, harm, platform, error, confusion) 2. User guesses one letter at a time until either they guess the word, or they run out of guesses. 3. Select a random word from the array of...
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...
FileWrite a program that will allow two users to play a tic-tac-toe game. You should write...
FileWrite a program that will allow two users to play a tic-tac-toe game. You should write the program such that two people can play the game without any special instructions. (assume they know how to play the game). You should code the program to do the five items below only. You do not have to write the entire program, just the five items below. • Use a 2D array(3 rows, 3 columns) of datatype char. Initialize the 2D array with...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
Write a program that plays an addition game with the user (imagine the user is a...
Write a program that plays an addition game with the user (imagine the user is a 5th grade student). First ask the user how many addition problems they want to attempt. Next, generate two random integers in the range between 10 and 50 inclusive. You must use the Math.random( ) method call.   Prompt the user to enter the sum of these two integers. The program then reports "Correct" or "Incorrect" depending upon the user's "answer".  If the answer was incorrect, show...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT