Question

JAVA PROGRAMMING

(Game: hangman) Write a hangman game that randomly generates a word and prompts the user to guess one letter at a time, as shown in the sample run. Each letter in the word is displayed as an asterisk. When the user makes a correct guess, the actual letter is then displayed. When the user finishes a word, display the number of misses and ask the user whether to continue to play with another word. Declare an array to store words, as follows: // Add any words you wish in this array String[] words = {"write", "that", ...};

the output should be something like this:

(Guess) Enter a letter in word DJEnter (Guess) Enter a letter in word p****** >r Enter (Guess) Enter a letter in word pr**p**

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

code:

import java.util.*;
class Abc
{
   static Scanner sc=new Scanner(System.in);
   static void compute(String s1)
   {
       int n=s1.length();
       int count=0;
       String dispArray[]=new String[n];
       for(int i=0;i<n;i++)
           dispArray[i]="*";
       HashSet<String> h=new HashSet<String>();
       while(true)
       {
           String todisp="";
           for(String s:dispArray)
               todisp+=s;
           System.out.println("(Guess) Enter a letter in word "+todisp);
           if(todisp.equals(s1))
           {
               System.out.println("The word is "+todisp+". You missed "+count+" time(s)");
               break;
           }
           String s=sc.nextLine();
           if(h.contains(s))
           {
               System.out.println("\t"+s+" is already present in the word");
           }
           else
           {
               boolean flag=false;
               for(int i=0;i<s1.length();i++)
               {
                   if(s1.charAt(i)==s.charAt(0))
                   {
                       flag=true;
                       dispArray[i]=""+s.charAt(0);
                   }
               }
               if(flag==true)
               {
                   h.add(s);
               }
               else
               {
                   count++;
                   System.out.println(s+" is not in the word");
               }
           }
       }
   }
   public static void main(String[] args) {
       Random r=new Random();
       String buffer[]=new String[]{"that","program","hello","ezhil","sunny","family"};
       String c="";
       do{
           compute(buffer[r.nextInt(buffer.length)]);
           System.out.println("Do you want to guess another word? Enter y or n >");
           c=sc.nextLine();
       }while(c.equals("y"));
   }
}

sample i/o:

Explanation:

The class Abc presented here will let you play the game. A random string from array of 5 strings are taken and passed to "compute" method, where user is asked to input the data util he reaches the word and the count of his wrong guess is displayed in the end.

Add a comment
Know the answer?
Add Answer to:
JAVA PROGRAMMING (Game: hangman) Write a hangman game that randomly generates a word and prompts the...
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
  • javafx assisantance confused on where to start Problem Description: (Game: hangman) Write a JavaFX program that...

    javafx assisantance confused on where to start Problem Description: (Game: hangman) Write a JavaFX program that lets a user play the hangman game. The user guesses a word by entering one letter at a time, as shown in Figure followings. If the user misses seven times, a hanging man swings, as shown in Figures. Once a word is finished, the user can press the Enter key to continue to guess another word. Guess a word: ***** God Missed letters Guess...

  • hey dear i just need help with update my code i have the hangman program i...

    hey dear i just need help with update my code i have the hangman program i just want to draw the body of hang man when the player lose every attempt program is going to draw the body of hang man until the player lose all his/her all attempts and hangman be hanged and show in the display you can write the program in java language: this is the code bellow: import java.util.Random; import java.util.Scanner; public class Hangmann { //...

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • Overview In this exercise you are going to recreate the classic game of hangman. Your program...

    Overview In this exercise you are going to recreate the classic game of hangman. Your program will randomly pick from a pool of words for the user who will guess letters in order to figure out the word. The user will have a limited number of wrong guesses to complete the puzzle or lose the round. Though if the user answers before running out of wrong answers, they win. Requirements Rules The program will use 10 to 15 words as...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • C++ Hangman (game) In this project, you are required to implement a program that can play...

    C++ Hangman (game) In this project, you are required to implement a program that can play the hangman game with the user. The hangman game is described in https://en.wikipedia.org/wiki/Hangman_(game) . Your program should support the following functionality: - Randomly select a word from a dictionary -- this dictionary should be stored in an ASCII text file (the format is up to you). The program then provides the information about the number of letters in this word for the user to...

  • Create the game hangman using c code: Program description In the game of hangman, one player...

    Create the game hangman using c code: Program description In the game of hangman, one player picks a word, and the other player has to try to guess what the word is by selecting one letter at a time. If they select a correct letter, all occurrences of the letter are shown. If no letter shows up, they use up one of their turns. The player is allowed to use no more than 10 incorrect turns to guess what the...

  • Please create a Hangman game in Java language. For this game a random word will be...

    Please create a Hangman game in Java language. For this game a random word will be selected from the following list (abstract, cemetery, nurse, pharmacy, climbing). You should keep a running tab of the user’s score and display it on screen; the user begins with 100 pts possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 pts should be taken away from what is left of their total possible points. For...

  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

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
ADVERTISEMENT