Question

Consider a class that could be used to play a game of hangman. The class has...

Consider a class that could be used to play a game of hangman. The class has the following attributes:

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.

It will 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 ?.
getSecretWord returns the secret word.
getGuessCount returns the number of guesses made.
isFound returns true if the hidden word has been discovered.


Write a method heading for each method.
Write preconditions and postconditions for each method.
Write some Java statements that test the class.
Implement the class.
List any additional methods and attributes needed in the implementation that were not listed in the original design. List any other changes made to the original design.
Write a program that implements the game of hangman using the class you wrote for Part d.

Help needed!! this is for java

thank you

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

The required java code is given below in case of any doubts you can ask me in comments. Please give positive ratings.

Main.java

import java.util.Scanner;

public class Main
{
// varibales
private String thesecret_Word;
private String thedisguised_Word;
private String remaining_Letters;
private int totalGuess;
private int total_Wrong_Guess;
  
// constructor
public Main(String given_Word)
{
thesecret_Word = given_Word.toLowerCase().trim();
remaining_Letters = thesecret_Word;
thedisguised_Word = setDisguisedWord(thesecret_Word);
totalGuess = 0;
total_Wrong_Guess = 0;
}
  
  
//Disguising the word
//Before thsi method we had complete word but after this we have ?????? only
public String setDisguisedWord(String input_Word)
{
input_Word = input_Word.toLowerCase();
input_Word = input_Word.replace('a', '?');
input_Word = input_Word.replace('b', '?');
input_Word = input_Word.replace('c', '?');
input_Word = input_Word.replace('d', '?');
input_Word = input_Word.replace('e', '?');
input_Word = input_Word.replace('f', '?');
input_Word = input_Word.replace('g', '?');
input_Word = input_Word.replace('h', '?');
input_Word = input_Word.replace('i', '?');
input_Word = input_Word.replace('j', '?');
input_Word = input_Word.replace('k', '?');
input_Word = input_Word.replace('l', '?');
input_Word = input_Word.replace('m', '?');
input_Word = input_Word.replace('n', '?');
input_Word = input_Word.replace('o', '?');
input_Word = input_Word.replace('p', '?');
input_Word = input_Word.replace('q', '?');
input_Word = input_Word.replace('r', '?');
input_Word = input_Word.replace('s', '?');
input_Word = input_Word.replace('t', '?');
input_Word = input_Word.replace('u', '?');
input_Word = input_Word.replace('v', '?');
input_Word = input_Word.replace('w', '?');
input_Word = input_Word.replace('x', '?');
input_Word = input_Word.replace('y', '?');
input_Word = input_Word.replace('z', '?');
  
return input_Word;
}
  
//function to make Guess
public void makeGuess(Character ch)
{
// check for letter
if(Character.isLetter(ch))
{
String userGuess = "" + ch;
userGuess = userGuess.toLowerCase();

// determine position
int characterPos = remaining_Letters.indexOf(userGuess);
boolean correctGuess = characterPos > -1;

while(characterPos > -1)
{
String word_Before = remaining_Letters.substring(0, characterPos);
String word_After = remaining_Letters.substring(characterPos+1);
remaining_Letters = word_Before + "#" + word_After;
  
word_Before = thedisguised_Word.substring(0, characterPos);
word_After = thedisguised_Word.substring(characterPos+1);
thedisguised_Word = word_Before + userGuess + word_After;
  
characterPos = remaining_Letters.indexOf(userGuess);
}
  
totalGuess++;
if(!correctGuess)
total_Wrong_Guess++;
} else
System.out.println("Invalid Input! Character should be from a to z");
}
  
//getDisguisedWord to check word
public String getDisguisedWord()
{
return thedisguised_Word;
}
  
public String getsecretWord()
{
return thesecret_Word;
}
  
//Count the guesses
public int getGuessCount()
{
return totalGuess;
}
  
//flag to check if the word is found or not
public boolean isFound()
{
return thesecret_Word.equals(thedisguised_Word);
}
  
//driver function
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Main hangmanGame = new Main("Abracadabra");

if(!hangmanGame.isFound())
{
while(!hangmanGame.isFound())
{
System.out.println("\nDisguised word: " + hangmanGame.thedisguised_Word);
System.out.print("Guess letter: ");
String guess = scan.next();
hangmanGame.makeGuess(new Character(guess.charAt(0)));
System.out.println("Total Guess: " + hangmanGame.totalGuess + "\tIncorrect Guess: " + hangmanGame.total_Wrong_Guess + "\n");
}
System.out.println("\nWell Done, You guessed the the secret word: " + hangmanGame.thesecret_Word);
}
  
  
}
  
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Consider a class that could be used to play a game of hangman. The class has...
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
  • URGENT submission 2/22/10:15 am :this is the third time im posting this question Hello need some...

    URGENT submission 2/22/10:15 am :this is the third time im posting this question Hello need some help doing this Question in JAVA The program should loop 3 times per player NO ARRAYS • The secret word or phrase (a String, which may include embedded white spaces and punctuation characters) • The disguised word, in which each unknown alphabetic letter in the secret word is replaced with a question mark (?). When an unknown letter is guessed correctly, it will replace...

  • In PYTHON 3- Implement a subclass (described below) of "Word Guess", a variant of the game...

    In PYTHON 3- Implement a subclass (described below) of "Word Guess", a variant of the game Hangman. In this game, a word is first randomly chosen. Initially, the letters in the word are displayed represented by "_”.   For example, if the random word is "yellow”, the game initially displays "_ _ _ _ _ _”. Then, each turn, the player guesses a single letter that has yet to be guessed. If the letter is in the secret word, then the...

  • In PYTHON 3- Implement a subclass (described below) of "Word Guess", a variant of the game...

    In PYTHON 3- Implement a subclass (described below) of "Word Guess", a variant of the game Hangman. In this game, a word is first randomly chosen. Initially, the letters in the word are displayed represented by "_”.   For example, if the random word is "yellow”, the game initially displays "_ _ _ _ _ _”. Then, each turn, the player guesses a single letter that has yet to be guessed. If the letter is in the secret word, then the...

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

  • Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The...

    Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The computer must select a word at random from the list of avail- able words that was provided in words.txt. The functions for loading the word list and selecting a random word have already been provided for you in ps3 hangman.py. 2. The game must be interactive; the flow of the game should go as follows: • At the start of the game, let the...

  • Write a Java program implementing a hangman game. This should be a multi file project, managed...

    Write a Java program implementing a hangman game. This should be a multi file project, managed with the directory structure discussed in class, compiled with the ant tool (using a build.xml file), and packaged as a JAR file, ready for distribution. Instructions on how to generate these files are included in our lectures. You can practice with the files ready to compile attached to Lecture 24 (the TicTacToe class example). Your project should include: 1. a text file for the...

  • Write a MATLAB code for the hangman game below you might break the problem down into:...

    Write a MATLAB code for the hangman game below you might break the problem down into: Selecting a word from a dictionary. ‘aardvark’ Reading a single letter from the user like ‘a’ Building a character array showing the letters matched so far like ‘aa---a--’ Keeping count of the number of guessed letters so far. Keeping count of the number of guesses so far. Writing conditional logic to see whether the game is finished or not.

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

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

  • For this assignment, you will create a command-line version of the game Hangman. For this assignment...

    For this assignment, you will create a command-line version of the game Hangman. For this assignment you will research on StringBuilder Class (Use this link) and use it in your code. Please finish the program in Java Language Your game should have a list of at least ten phrases of your choosing (appropriate to all audiences please!) The game chooses a random phrase from the list. This is the phrase the player tries to guess. (10) ● The phrase is...

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