Question

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 the partially hidden phrase. For our example, if the user guessed "o", the new partially hidden phrase would change to: * o * ** o*******

Allow the phrases and guesses to include lowercase and uppercase letters and digits. If a guess is not a letter or digit (e.g., ‘%’) the user should be notified but not penalized a miss.

If the guessed letter does not occur in the phrase, the user is notified of the miss and how many misses/chances are left.

The user should NOT be penalized for guessing a previously correct or incorrect guess, but should be notified.

If the user misses 8 times, they lose and the game is over. If all letters in the phrase are guessed, the user wins!

.

.

.


import java.util.Scanner;
import java.util.Random;
public class Hangman
{
   String[] guesses = {"water", "University", "apple"};
   String currentGuess;
   char playerGuess[];

   public void showGuess(char guess[])
   {
       for (int i = 0; i            {
               System.out.print(guess[i]);
           }
   }
   public void startGame()
   {
       int attempt=0;
       int correct=0;
       int set1=0;
       int set2=0;
       int space=0;
       int count;
       char letter;

       Scanner input=new Scanner(System.in);

       for(count=0; count        {
           if(currentGuess.charAt(count)==' ')
           {
               playerGuess[count]=' ';
               space++;
           }
           else
           {
               playerGuess[count]='*';
           }
       }
       System.out.println("Here is the word guess: ");
       correct=space;
       do
       {
           showGuess(playerGuess);
           System.out.println("\nEnter a letter: ");
           letter=input.next().charAt(0);
           attempt++;
           for(int j=0;j==0;j++)
           {
               if(playerGuess[j]==letter)
               {
                   set2=1;
                   break;
               }
           }
           if(set2==0)
           {
               for(count=0;count                {
                   if(currentGuess.charAt(count)==Character.toLowerCase(letter)||currentGuess.charAt(count)==Character.toUpperCase(letter))
                   {
                       playerGuess[count]=letter;
                       correct++;
                       set1=1;
                   }
               }
               if(set1==0)
               {
                   System.out.println("Wrong guess!");
               }
               else
               {
                   set1=0;
               }
           }
           else
           {
               System.out.println("\nThe letter has already been guessed. Try again.");
               set2=0;
           }
       }
       while(correct!=(playerGuess.length));
       System.out.println("\nNice work! You took "+attempt+" tries to guess "+currentGuess);
   }

}

.

.

.

Main:

import java.util.Scanner;
import java.util.Random;
public class HangmanDemo
{
   public static void main(String args[])
   {
       Hangman h=new Hangman();
       Random r=new Random();

       int guessNum=r.nextInt(10)+0;
       h.currentGuess=h.guess[guessNum];

       h=playerGuess=new char[h.currentGuess.length()];

       h.startGame();
   }
}

.

.

.

Error:

Hangman.java:98: error: '.class' expected h=playerGuess[]=new char[h.currentGuess.length()]; ^ 1 error

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

If you have any doubts, please give me comment...

Hangman.java

import java.util.Scanner;

import java.util.Random;

public class Hangman {

    String[] guesses = { "water", "University", "apple" };

    String currentGuess;

    char playerGuess[];

    public void showGuess(char guess[]) {

        for (int i = 0; i < guess.length; i++) {

            System.out.print(guess[i]);

        }

    }

    public void startGame() {

        int attempt = 0;

        int correct = 0;

        int set1 = 0;

        int set2 = 0;

        int space = 0;

        int count;

        char letter;

        Scanner input = new Scanner(System.in);

        for (count = 0; count < currentGuess.length(); count++) {

            if (currentGuess.charAt(count) == ' ') {

                playerGuess[count] = ' ';

                space++;

            } else {

                playerGuess[count] = '*';

            }

        }

        System.out.println("Here is the word guess: ");

        correct = space;

        do {

            showGuess(playerGuess);

            System.out.println("\nEnter a letter: ");

            letter = input.next().charAt(0);

            attempt++;

            for (int j = 0; j == 0; j++) {

                if (playerGuess[j] == letter) {

                    set2 = 1;

                    break;

                }

            }

            if (set2 == 0) {

                for (count = 0; count < currentGuess.length(); count++) {

                    if (currentGuess.charAt(count) == Character.toLowerCase(letter)

                            || currentGuess.charAt(count) == Character.toUpperCase(letter)) {

                        playerGuess[count] = letter;

                        correct++;

                        set1 = 1;

                    }

                }

                if (set1 == 0) {

                    System.out.println("Wrong guess!");

                } else {

                    set1 = 0;

                }

            } else {

                System.out.println("\nThe letter has already been guessed. Try again.");

                set2 = 0;

            }

        } while (correct != (playerGuess.length) && attempt<8);

if(correct == playerGuess.length)

System.out.println("\nNice work! You took " + attempt + " tries to guess " + currentGuess);

else

System.out.println("\nYou lose and the game is over");

    }

}

HangmanDemo.java

import java.util.Scanner;

import java.util.Random;

public class HangmanDemo {

    public static void main(String args[]) {

        Hangman h = new Hangman();

        Random r = new Random();

        int guessNum = r.nextInt(3) + 0;

        h.currentGuess = h.guesses[guessNum];

        h.playerGuess = new char[h.currentGuess.length()];

        h.startGame();

    }

}

nagarajuanagaraju-Vostro-3550:28092019$ javac HangmanDemo.java nagarajuanagaraju-Vostro-3550:28092019$ java HangmanDemo Here

Add a comment
Know the answer?
Add Answer to:
JAVA Hangman Your game should have a list of at least ten phrases of your choosing.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
  • Original question IN JAVA Show the user a number of blank spaces equal to the number...

    Original question IN JAVA Show the user a number of blank spaces equal to the number of letters in the word For cat, you would show _ _ _. Prompt a user to guess a letter until they have run out of guesses or guessed the full word By default, the user should be able to make 7 failed guesses before they lose the game. If the user guesses a letter that is in the target word, it does not...

  • 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...

  • Hangman is a game for two or more players. One player thinks of a word, phrase...

    Hangman is a game for two or more players. One player thinks of a word, phrase or sentence and the other tries to guess it by suggesting letters or numbers, within a certain number of guesses. You have to implement this game for Single Player, Where Computer (Your Program) will display a word with all characters hidden, which the player needed to be guessed. You have to maintain a dictionary of Words. One word will be selected by your program....

  • 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 { //...

  • 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...

  • (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming...

    (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming for code for the new GameChanger class. // the code for Main class for step number 6 has already been given, please show modified progrraming for Main class and GameCHanger class, thank you. package .games; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner(System.in); // Get upper...

  • Note wordBank, an array of 10 strings (char *s). Your program should do the following: 1....

    Note wordBank, an array of 10 strings (char *s). Your program should do the following: 1. Select a word at random from the wordBank. This is done for you. 2. On each turn display the word, with letters not yet guessed showing as *'s, and letters that have been guessed showing in their correct location 3. The user should have 10 attempts (?lives?). Each unsuccessful guess costs one attempt. Successful guesses do NOT count as a turn. 4. You must...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • 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...

  • I need help with this. I need to create the hangman game using char arrays. I've...

    I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...

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