Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I have a programming assignment where we have to write a hangman game usi

ID: 3631189 • Letter: H

Question

Hello, I have a programming assignment where we have to write a hangman game using OOP. Below is the code I have so far (my main method is the part in black).

Everything is mostly working except I am getting a couple of problems.

1st- The main loop for the game should stop and signal "GameOVer" if the disguised word equals the secret word i.e when you guess the word correctly. I wrote the isFound() method to return a boolean value but when I tried implementing this on my main method the loop would never pick up on it.

I know this is a lost of text and it might be confusing but any help would be appreciated.

Thank you for your time.



public class HangMan
{

private String secretWord = "bigbang", disguisedWord = "";
private int guessCount = 0, missCount = 0;



public void setSecretWord(String newWord)
{
secretWord = newWord;
guessCount = 0;
missCount = 0;

int wordLength = newWord.length();
while(wordLength > 0)
{
disguisedWord = disguisedWord + "?";
wordLength--;
}

}

public String getSecretWord()
{
return secretWord;
}




public boolean isFound()
{
return secretWord.equalsIgnoreCase(disguisedWord);

}


public String getDisguisedWord()
{
return disguisedWord;
}

public int getGuessCount()
{
return guessCount;
}

public int getMissesCount()
{
return missCount;
}

public void guessCharacter(char c)
{
// int position = secretWord.indexOf(c);
boolean got_it = false;
String updateDisguised="";

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

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

updateDisguised = updateDisguised + secretWord.charAt(i);

String checkDuplicate = updateDisguised.substring(0,i);
int duplicatePos = checkDuplicate.indexOf(c);
if(duplicatePos <0)
guessCount++;
got_it = true;


}
else
{
updateDisguised = updateDisguised + disguisedWord.charAt(i);
}

}
if(got_it == false)
{
missCount++;
guessCount++;
}


disguisedWord = updateDisguised;

}

}

import java.util.Scanner;

public class HangManGame {

public static void main(String[] args)

{

boolean retry= true;

String retry_ans;

Scanner kb = new Scanner(System.in);

HangMan newHangMan = new HangMan();

String word = newHangMan.getSecretWord();

String input;

char guess;

newHangMan.setSecretWord(word);

System.out.println("Hangman game starts:");

do{

System.out.println("Guess this: " + newHangMan.getDisguisedWord());

System.out.println("Enter your guess character: [guess]");

input = kb.next();

guess = input.charAt(0);

newHangMan.guessCharacter(guess);

System.out.println(newHangMan.getDisguisedWord());

System.out.println("Number of guesses so far : " + newHangMan.getGuessCount());

System.out.println("NUmber of misses so far: " + newHangMan.getMissesCount());

if((newHangMan.getMissesCount()==7) || (newHangMan.isFound()=true))

{

System.out.println("The game is over");

System.out.println("Would you like to try again?");

retry_ans = kb.next();

if(retry_ans.equalsIgnoreCase("yes"))

{

retry = true;

System.out.println("Please enter a new secret word:");

String newWord = kb.next();

newHangMan.setSecretWord(newWord);

}

else

{

retry =false;

}

}

} while(retry = true);

}

}

Explanation / Answer

There were some minor problems with syntax, logic operators and initialization in both files. That's all. Make sure that you use '==' for boolean operation instead of '='.

public class HangMan {
private String secretWord = "bigbang", disguisedWord = "";
private int guessCount = 0, missCount = 0;

public void setSecretWord(String newWord){
secretWord = newWord;
guessCount = 0;
missCount = 0;

int wordLength = newWord.length();
disguisedWord = "";
while(wordLength > 0) {
disguisedWord = disguisedWord + "?";
wordLength--;
}
}

public String getSecretWord(){
return secretWord;
}

public boolean isFound(){
return secretWord.equalsIgnoreCase(disguisedWord);
}

public String getDisguisedWord(){
return disguisedWord;
}

public int getGuessCount(){
return guessCount;
}

public int getMissesCount(){
return missCount;
}

public void guessCharacter(char c){
// int position = secretWord.indexOf(c);
boolean got_it = false;
String updateDisguised="";
for(int i=0; i < secretWord.length();i++){
if(c == secretWord.charAt(i)){
updateDisguised = updateDisguised + secretWord.charAt(i);
String checkDuplicate = updateDisguised.substring(0,i);
int duplicatePos = checkDuplicate.indexOf(c);
if(duplicatePos <0)
guessCount++;
got_it = true;
}
else
updateDisguised = updateDisguised + disguisedWord.charAt(i);
}//end for
if(got_it == false){
missCount++;
guessCount++;
}
disguisedWord = updateDisguised;
}
}

import java.util.Scanner;

public class HangManGame {
public static void main(String[] args) {
boolean retry= true;
String retry_ans;
Scanner kb = new Scanner(System.in);
HangMan newHangMan = new HangMan();
String word = newHangMan.getSecretWord();
String input;
char guess;
newHangMan.setSecretWord(word);
System.out.println("Hangman game starts:");
do {
System.out.println("Guess this: " + newHangMan.getDisguisedWord());
System.out.println("Enter your guess character: [guess]");
input = kb.next();
guess = input.charAt(0);
newHangMan.guessCharacter(guess);
System.out.println(newHangMan.getDisguisedWord());
System.out.println("Number of guesses so far : " + newHangMan.getGuessCount());
System.out.println("Number of misses so far: " + newHangMan.getMissesCount());
if((newHangMan.getMissesCount()==7) || (newHangMan.isFound()==true)){
System.out.println("The game is over");
System.out.println("Would you like to try again?");
retry_ans = kb.next();
if(retry_ans.equalsIgnoreCase("yes")){
retry = true;
System.out.println("Please enter a new secret word:");
String newWord = kb.next();
newHangMan.setSecretWord(newWord);
}
else
retry =false;
}
} while(retry == true);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote