Write a class called Hangman that will be used to play a gam

Write a class called Hangman that will be used to play a game of hangman.

The class should have the following instance variables:

·       The secret word.

·       The disguised word, in which each unknown letter in the secret word is replaced with a question mark (?). For example, if the secret word is abracadabra and the letters a, b, and e have been guessed, the disguised word would be ab?a?a?ab?a.

·       The number of guesses made.

·       The number of incorrect guesses made.

The class should have the following static variables:

·       The total number of incorrect guesses allowed before the game is over. This should be a constant that is set to have the value 6.

·       The number of Hangman games that have been won.

·       The number of Hangman games that have been lost.

The class should have the following constructors:

·       A default constructor that sets the number of guesses and number of incorrect guesses to 0.

·       A constructor that takes one String parameter. The constructor should set the secret word variable to this string value. It should set the number of guesses and number of incorrect guesses to 0. It should also set the value of the disguised word variable (e.g. if the passed in value is “hello”, then the disguised word should be set to “?????”).

The class should have the following methods:

·       makeGuess(c) guesses that character c is in the word.

·       getDisguisedWord() returns a string containing correctly guessed letters in their correct positions and unknown letters replaced with ?.

·       setSecretWord(word) sets the secret word to the parameter value and sets the disguised word variable to its initial value. Note: This method would be called only if the default constructor were used when creating a new Hangman object.

·       getGuessCount() returns the total number of guesses made.

·       getIncorrectGuessCount() returns the number of incorrect guesses made.

·       isFound() returns true if the hidden word has been discovered.

·       gameOver() returns true if the game is over either because the hidden word was discovered or the maximum number of incorrect guesses were made.

·       setDisguisedWord() is a private method that sets the initial value of the disguised word variable. This method should be called by the parameter constructor and by the setSecretWord(word) method

Your Program

After creating the Hangman class, create another class called HangmanDemo that will allow the user of the program to actually play hangman.

When the program starts, the program should print out “Welcome to Hangman!” and ask whether the user wants to play.

If the user types “yes” of “y” to play, print out the initial disguised word and ask the user to guess a letter. After the user guesses a letter, print out the new disguised word as well as the hangman figure and ask for another guess.

After the game is over print out whether the user won or lost, how many total guesses were made, and how many incorrect guesses were made. Then ask the user if they want to play again.

As long as the user selects to play again, a new hangman game with a different secret word should be used.

After the user indicates that he/she doesn’t want to play again, print out the number of games that were won and the number of games that were lost before exiting the program.

For the secret words, store a list of 10 secret words in an array.

Choose any 10 words you want.

Create an integer variable called gameCount which is initially set to 0. Then have code like the following in a loop:

Hangman game = new Hangman(words[gameCount]);

// your code to play the game goes here…

// then after the game is over

gameCount++;

Solution

import java.util.Scanner;

public class HangMan {

private String secretWord;

private String disguisedWord;

private String letters;

private int numberOfGuesses;

private int numberOfIncorrectGuesses;

public void initialize( String secretWord ) {

    this.secretWord = secretWord;

    this.disguisedWord = \"\";

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

      disguisedWord += \"?\";

    }

}

public void makeGuess( char guess ) {

    boolean match = false;

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

      if ( secretWord.charAt( i ) == guess ) {

        StringBuilder newString = new StringBuilder( disguisedWord );

        newString.setCharAt( i, guess );

        disguisedWord = newString.toString( );

        match = true;

      }

    }

    if ( !match ) {

      numberOfIncorrectGuesses++;

    }

}

public boolean isFound( ) {

    if ( disguisedWord.contains( \"?\" ) ) {

      return false;

    } else {

      System.out.println( \"You win\" );

      return true;

    }

}

public String getSecretWord( ) {

    return secretWord;

}

public String getDisguisedWord( ) {

    return disguisedWord;

}

public void playGame( ) {

    while ( !isFound( ) ) {

      System.out.println( \"The disguised word is <\" + disguisedWord + \">\" );

      numberOfGuesses++;

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

      makeGuess( new Scanner( System.in ).next( ).charAt( 0 ) );

      System.out.println( \" Guesses made \" + numberOfGuesses + \" with \"

          + numberOfIncorrectGuesses + \" wrong\" );

    }

}

}

Main.java

public class Main {

public static void main( String[] args ) {

    HangMan game = new HangMan( );

    game.initialize( \"happiness\" );

    System.out.println( \"Lets play a round of hangman.\" );

    game.playGame( );

}

}

Write a class called Hangman that will be used to play a game of hangman. The class should have the following instance variables: · The secret word. · The disgu
Write a class called Hangman that will be used to play a game of hangman. The class should have the following instance variables: · The secret word. · The disgu
Write a class called Hangman that will be used to play a game of hangman. The class should have the following instance variables: · The secret word. · The disgu
Write a class called Hangman that will be used to play a game of hangman. The class should have the following instance variables: · The secret word. · The disgu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site