Can someone please help me fix this code. I provided the requirement down below
ID: 3759976 • Letter: C
Question
Can someone please help me fix this code. I provided the requirement down below the code. Thanks alot
public class HangMan {
private final String secret_word;
private char[] disguised_word;
private int guessCount;
private int missesCount;
private int MAX_MISSES_ALLOWED;
private char[] missesMarkers;
public int gamesPlayed;
public HangMan(String word)
{
int length=0;
guessCount=0;
missesCount=0;
MAX_MISSES_ALLOWED=7;
secret_word="big bang";
length=secret_word.length();
for(int i=0;i<length;i++)
{
if(secret_word.charAt(i)==' ')
{
disguised_word[i]=' ';
}
else
{
disguised_word[i]='';
}
}
for (int i=0; i<secret_word.length(); i++)
{
disguised_word = new char[secret_word.length()];
disguised_word[i]='?';
}
for(int i=0; i<MAX_MISSES_ALLOWED;i++)
{
missesMarkers[i]='o';
}
gamesPlayed=gamesPlayed+1;
}
public boolean guessCharacter(char c)
{
for(int i=0;i<secret_word.length(); i++)
{
if (secret_word.charAt(i)==c)
{
disguised_word[i]=c;
guessCount=guessCount+1;
return true;
}
}
guessCount=guessCount+1;
missesCount=missesCount+1;
for(int i=0;i<MAX_MISSES_ALLOWED;i++)
{
if(missesMarkers[missesCount]=='o')
missesMarkers[missesCount]='X';
}
return false;
}
public boolean isGameOver()
{
int c=0;
if (isFound())
{
c=1;
}
for(int i=0;i<MAX_MISSES_ALLOWED;i++)
{
c=1;
}
if(c==1)
{
return true;
}
else
{
return false;
}
}
public boolean isFound()
{
String word="";
if(secret_word.equals(word))
{
return true;
}
else
{
return false;
}
}
public String getDisguisedWord()
{
String word="";
return word;
}
public int getGuessCount()
{
return guessCount;
}
public int getMissesCount()
{
return missesCount;
}
public String getMissedMargker()
{
String missesguessMarkers=String.valueOf(missesMarkers);
return missesguessMarkers;
}
public int getGamesPlayed()
{
return gamesPlayed;
}
}
import java.util.Scanner;
public class GameDriver {
public static void main(String[]args)
{
Scanner myScan=new Scanner(System.in);
String word="";
System.out.println("Welcome to the Hangman Game");
System.out.println("----------------------------");
System.out.println();
HangMan obj=new HangMan(word);
String yesNo="";
int noofgames=0;
do
{
int count=1;
do
{
System.out.println(" Guess this:"+ obj.getDisguisedWord());
System.out.println(" guesses so far:"+obj.getGuessCount());
String missed;
missed=obj.getMissedMargker();
System.out.println(" Misses:"+ missed);
for(int i=0;i<obj.getMissedMargker().length();i++)
{
if(missed.charAt(i)=='o')
count=count+1;
}
System.out.println(" Enter your guess Character:");
char c=myScan.next().charAt(0);
if(!obj.guessCharacter(c))
{
System.out.println(c+" is not in the word.Death draws closer");
}
else
{
System.out.println(c+" is in the secret word.");
}
}while(count>1);
System.out.println("Game Over!");
if(obj.isFound())
{
System.out.println("Congratulations! You guessed the secret word:"+word+"in"+obj.getGuessCount());
}
else if(!obj.isGameOver() )
{
System.out.println(" You died. Next Time, guess as if your life depended on it");
}
HangMan newobj=new HangMan(word);
noofgames=obj.getGamesPlayed();
System.out.println("Do you want to play again? (yes/no):");
yesNo=myScan.nextLine();
System.out.println(" Input a new secret word:");
word=myScan.nextLine();
}while(yesNo.equals("yes" )||yesNo.equals("Yes"));
if(yesNo=="no")
{
String str="Thanks for playing";
System.out.println(str.toString());
System.out.print(noofgames);
System.out.print("game(s) of Hang Man!");
}
}
}
1.) The class HangMan should have following instance variables:
secretWord: The word that needs to be guessed. access modifier: private; type: String; Additional modifers: final (this string cannot be changed)
disguisedWord: The word which in which question marks are replaced with correct guesses. (initially it will be all question marks). access modifier: private; type: char[] (length determined by secretWord)
NOTE if there were spaces in the original string (See "big bang" example below) you display a space, ' ', in the disguisedWord
guessCount: counter to keep track of the total number of guesses. access modifier: private; type: int
missesCount: counter to keep track of the number of incorrect guesses. access modifier: private; type: int
MAX_MISSES_ALLOWED: constant representing the max number of guesses allowed. Set to 7. access modifier: private; type: int
missedMarkers: Series of characters representing the current number of misses by the user. access modifier: private; type: char[] (length is equal the MAX_MISSES_ALLOWED value)
gamesPlayed: Static variable that keeps track of the number games of hangman that have been played (in other words, the number of HangMan instances created)
All of the instance variables must be private.
2.) This class should have a constructor to initialize all the above instance variables.
secretWord should be initialized to a secret word passed in as a parameter. This will be the secret word for the first time the game is played.
disguisedWord to "???????" (length of disguisedWord should be the same as the length of secretWord)
missedMarkers to "OOOOOOO"
Initialize guessCount to 0 and missesCount to 0
Initialize MAX_MISSES_ALLOWED to 7
Increment the number of gamesPlayed by 1 (NOTE: you must initialize this variable outside the constructor)
3.) This class should have following public methods. Only these methods should be public.
boolean guessCharacter(char c): Guesses that character c is in the secret word. Updates all instance variables accordingly, and returns true if c is present in the secret word.
boolean isGameOver(): Returns true is the user is out of guesses or the word was found
boolean isFound(): Returns true if the secret word was discovered (in other words, all characters in the secret word have been guessed)
String getDisguisedWord(): Returns Disguised word, which contains correctly guessed letters in their correct positions and unknown letters as ?.
NOTE: this is returning a String not a char[]
For help with this, know that you can pass in char arrays as a parameter to String constructors
int getGuessCount(): Returns the number of guesses made.
int getMissesCount(): Returns the number of incorrect guesses made.
String getMissedMarker(): Returns a string representing the missed guess markers
NOTE: this is returning a String not a char[]
For help with this, know that you can pass in char arrays as a parameter to String constructors
int getGamesPlayed() returns the number of instances of HangMan that have been constructed
Hint: String methods charAt and toLowerCase may be useful in this homework.
4.) For each method write preconditions and postconditions.
5.) Include at least one private method, to assist your public methods. For example, you might include a private method to reset the game to the start state. The choice of the private methods is up to you.
6.) No methods of the HangMan class should output text to the screen, or ask the user for input. All of the user interface should be encoded in the GameDriver class. This division of labor is useful if, for example, you later wanted to write a new user interface for your hangman game that allowed it to run on the tablets, using a graphical user interface. The underlying hangman game logic could remain the same; only the user interface code would need to be replaced. In software architecture, GameDriver is what is known as the front end and the HangMan class is considered the back end.
7.) Now implement this class by writing GameDriver.java. The sample output of this implementation is shown below.
After the game is complete, ask the user if they would like to play again. Any form of the word yes (e.g. "yes", "YES", "Yes", "yEs", etc.) keeps the game going. Any other input quits the games.
If so, allow them to enter a new secret word.
Then, output 100 blank lines so that the secret word is not visible, and begin play again using the new secret word. This way, you can set up a game that a friend can play.
8) When the user quits, tell them how many games of hangman were played. Do this by calling the getGamesPlayed() method from your HangMan class.
Sample Program Output 1
(secret word: "big bang")
Sample Program Output2
(secret word: "big bang")
After "clearing" the screen (putting in 100 blank lines), game play continues as before, using the new instance of HangMan class with "spiders" passed into the constructor.
Explanation / Answer
public class HangMan {
private final String secret_word;
private char[] disguised_word;
private int guessCount;
private int missesCount;
private int MAX_MISSES_ALLOWED;
private char[] missesMarkers;
public int gamesPlayed;
public HangMan(String word)
{
int length=0;
guessCount=0;
missesCount=0;
MAX_MISSES_ALLOWED=7;
secret_word="big bang";
length=secret_word.length();
for(int i=0;i<length;i++)
{
if(secret_word.charAt(i)==' ')
{
disguised_word[i]=' ';
}
else
{
disguised_word[i]='';
}
}
for (int i=0; i<secret_word.length(); i++)
{
disguised_word = new char[secret_word.length()];
disguised_word[i]='?';
}
for(int i=0; i<MAX_MISSES_ALLOWED;i++)
{
missesMarkers[i]='o';
}
gamesPlayed=gamesPlayed+1;
}
public boolean guessCharacter(char c)
{
for(int i=0;i<secret_word.length(); i++)
{
if (secret_word.charAt(i)==c)
{
disguised_word[i]=c;
guessCount=guessCount+1;
return true;
}
}
guessCount=guessCount+1;
missesCount=missesCount+1;
for(int i=0;i<MAX_MISSES_ALLOWED;i++)
{
if(missesMarkers[missesCount]=='o')
missesMarkers[missesCount]='X';
}
return false;
}
public boolean isGameOver()
{
int c=0;
if (isFound())
{
c=1;
}
for(int i=0;i<MAX_MISSES_ALLOWED;i++)
{
c=1;
}
if(c==1)
{
return true;
}
else
{
return false;
}
}
public boolean isFound()
{
String word="";
if(secret_word.equals(word))
{
return true;
}
else
{
return false;
}
}
public String getDisguisedWord()
{
String word="";
return word;
}
public int getGuessCount()
{
return guessCount;
}
public int getMissesCount()
{
return missesCount;
}
public String getMissedMargker()
{
String missesguessMarkers=String.valueOf(missesMarkers);
return missesguessMarkers;
}
public int getGamesPlayed()
{
return gamesPlayed;
}
}
import java.util.Scanner;
public class GameDriver {
public static void main(String[]args)
{
Scanner myScan=new Scanner(System.in);
String word="";
System.out.println("Welcome to the Hangman Game");
System.out.println("----------------------------");
System.out.println();
HangMan obj=new HangMan(word);
String yesNo="";
int noofgames=0;
do
{
int count=1;
do
{
System.out.println(" Guess this:"+ obj.getDisguisedWord());
System.out.println(" guesses so far:"+obj.getGuessCount());
String missed;
missed=obj.getMissedMargker();
System.out.println(" Misses:"+ missed);
for(int i=0;i<obj.getMissedMargker().length();i++)
{
if(missed.charAt(i)=='o')
count=count+1;
}
System.out.println(" Enter your guess Character:");
char c=myScan.next().charAt(0);
if(!obj.guessCharacter(c))
{
System.out.println(c+" is not in the word.Death draws closer");
}
else
{
System.out.println(c+" is in the secret word.");
}
}while(count>1);
System.out.println("Game Over!");
if(obj.isFound())
{
System.out.println("Congratulations! You guessed the secret word:"+word+"in"+obj.getGuessCount());
}
else if(!obj.isGameOver() )
{
System.out.println(" You died. Next Time, guess as if your life depended on it");
}
HangMan newobj=new HangMan(word);
noofgames=obj.getGamesPlayed();
System.out.println("Do you want to play again? (yes/no):");
yesNo=myScan.nextLine();
System.out.println(" Input a new secret word:");
word=myScan.nextLine();
}while(yesNo.equals("yes" )||yesNo.equals("Yes"));
if(yesNo=="no")
{
String str="Thanks for playing";
System.out.println(str.toString());
System.out.print(noofgames);
System.out.print("game(s) of Hang Man!");
}
}
}