Consider a class that could be used to play a game of hangma

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 has been 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?a?ab?a. The number of guesses made. The number of incorrect guesses. The class would have the following methods: makeGuess(char c) guesses that character c is in the word. getDisguisedWord () returns a String containing correctly guessed letters in the 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. Your task is to implement the class with the above variables and methods, then implement a \"game\" of hangman using this class. Your program doens\'t need to draw anything.

Solution

//Hangman.java

import java.util.Scanner;

public class Hangman
{
// varibales
private String thesecret_Word;
private String thedisguised_Word;
private String remaining_Letters;
private int total_Guess;
private int total_Wrong_Guess;
  
// constructor
public Hangman(String given_Word)
{
thesecret_Word = given_Word.toLowerCase().trim();
remaining_Letters = thesecret_Word;
thedisguised_Word = setDisguisedWord(thesecret_Word);
total_Guess = 0;
total_Wrong_Guess = 0;
}
  
  
  
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;
}
  
public void makeGuess(Character ch)
{
   // check for letter
if(Character.isLetter(ch))
{
String user_Guess = \"\" + ch;
user_Guess = user_Guess.toLowerCase();

// determine position
int character_Position = remaining_Letters.indexOf(user_Guess);
boolean correct_Guess = character_Position > -1;

while(character_Position > -1)
{
String word_Before = remaining_Letters.substring(0, character_Position);
String word_After = remaining_Letters.substring(character_Position+1);
remaining_Letters = word_Before + \"#\" + word_After;
  
word_Before = thedisguised_Word.substring(0, character_Position);
word_After = thedisguised_Word.substring(character_Position+1);
thedisguised_Word = word_Before + user_Guess + word_After;
  
character_Position = remaining_Letters.indexOf(user_Guess);
}
  
total_Guess++;
if(!correct_Guess)
total_Wrong_Guess++;
}    else
   System.out.println(\"Invalid Input! Character should be from a to z\");
}
  
public String getDisguisedWord()
{
return thedisguised_Word;
}
  
public String getsecretWord()
{
return thesecret_Word;
}
  
  
public int getGuessCount()
{
return total_Guess;
}
  
public boolean isFound()
{
return thesecret_Word.equals(thedisguised_Word);
}
  

public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Hangman hangmanGame = new Hangman(\"armageddon\");

if(!hangmanGame.isFound())
{
while(!hangmanGame.isFound())
{
System.out.println(\"\ Disguised 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.total_Guess + \"\\tIncorrect Guess: \" + hangmanGame.total_Wrong_Guess + \"\ \");
}
System.out.println(\"\ Well Done, You guessed the the secret word: \" + hangmanGame.thesecret_Word);
}
  
  
}
  
}

/*
output:

Disguised word: ??????????
Guess letter: a
Total Guess: 1   Incorrect Guess: 0


Disguised word: a??a??????
Guess letter: p
Total Guess: 2   Incorrect Guess: 1


Disguised word: a??a??????
Guess letter: m
Total Guess: 3   Incorrect Guess: 1


Disguised word: a?ma??????
Guess letter: r
Total Guess: 4   Incorrect Guess: 1


Disguised word: arma??????
Guess letter: t
Total Guess: 5   Incorrect Guess: 2


Disguised word: arma??????
Guess letter: f
Total Guess: 6   Incorrect Guess: 3


Disguised word: arma??????
Guess letter: l
Total Guess: 7   Incorrect Guess: 4


Disguised word: arma??????
Guess letter: g
Total Guess: 8   Incorrect Guess: 4


Disguised word: armag?????
Guess letter: n
Total Guess: 9   Incorrect Guess: 4


Disguised word: armag????n
Guess letter: g
Total Guess: 10   Incorrect Guess: 5


Disguised word: armag????n
Guess letter: d
Total Guess: 11   Incorrect Guess: 5


Disguised word: armag?dd?n
Guess letter: e
Total Guess: 12   Incorrect Guess: 5


Disguised word: armagedd?n
Guess letter: o
Total Guess: 13   Incorrect Guess: 5

Well Done, You guessed the the secret word: armageddon

*/

 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 unknow
 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 unknow
 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 unknow
 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 unknow

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site