Chapter 5, PP 6 Java Project Name: IC15_Hangman Implement a class named Hangman
ID: 3810402 • Letter: C
Question
Chapter 5, PP 6
Java Project Name: IC15_Hangman
Implement a class named Hangman 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 "constructor" and the letters c, n, o and r have been guessed, the disguised word would be "con??r?c?or"
The number of guesses made
The number of incorrect guesses made
The Hangman class will have methods to:
Create a new game of Hangman (given a secret word only) [constructor]
[No copy constructor needed]
*** Add only accessors for:
getSecretWord
getDisguisedWord
getNumberOfGuesses
getIncorrectGuesses
makeGuess(char c) - guesses that character c is in the word. If so, increment number of guesses, change the disguised word to replace question marks ? with the correct character. If incorrect, increment both incorrect guesses and number of guesses.
isFound() - returns true if the secret word is discovered (e.g. there are no more question marks left in the disguised word)
[No toString() or equals() methods are needed]
Also, implement a driver class to play a game of Hangman!
Below is a class diagram to assist with the requirements of the Hangman class:
Explanation / Answer
Hangman.java:
import java.util.Scanner;
public class Hangman {
Scanner scanner = new Scanner(System.in);
int incorrectGuesses = 0;
int numberOfGuesses = 0;
String secretWord ; // This is the word to guess
String disguisedWord = "";
public Hangman(String string) {
secretWord = string;
for(int i=0; i<secretWord.length(); i++) {
disguisedWord += "?";
}
}
public void printCurrentState() {
System.out.println(" " + getDisguisedWord());
}
private char askUserToGuess() {
char guess;
// Step: Prompt the user for a letter
System.out.print("Guess a letter: ");
String response = scanner.nextLine().toUpperCase();
if (response.length() > 0) {
guess = response.charAt(0);
} else {
guess = ' ';
}
numberOfGuesses++;
return guess;
}
private boolean isLetterunknown(char guess) {
boolean isUnknownLetter = false;
for (int i = 0; i < disguisedWord.length(); i++) {
if (disguisedWord.charAt(i) == '?'
&& secretWord.charAt(i) == guess) {
isUnknownLetter = true;
}
}
return isUnknownLetter;
}
private void updatePositions(char guess) {
String prevUnknownPositions = disguisedWord;
disguisedWord = "";
for (int i = 0; i < secretWord.length(); i++) {
if (secretWord.charAt(i) == guess) {
disguisedWord += String.valueOf(guess);
} else {
disguisedWord += prevUnknownPositions.charAt(i);
}
}
}
public boolean isFound() {
for (int i = 0; i < disguisedWord.length(); i++) {
if (disguisedWord.charAt(i) == '?') {
return false;
}
}
return true;
}
public void printHangman(boolean missed) {
if(missed)
System.out.println("Miss count: " + incorrectGuesses);
else
System.out.println("Good job.. Go on!!");
}
public void playGame() {
char guess = ' ';
do {
printCurrentState();
guess = askUserToGuess();
// Step: Check if the letter is unknown
boolean isUnknownLetter;
isUnknownLetter = isLetterunknown(guess);
// If letter is unknown, update unknown positions
if (isUnknownLetter) {
// Update unknownPositions by building a new string, keeping the
// previous value for a particular position unless the
// position was unknown before AND it guess matches a position
// in the secret word.
updatePositions(guess);
if(isFound()) {
break;
}
printHangman(false);
} else {
// Letter was not unknown so increment missCount
incorrectGuesses++;
printHangman(true);
}
} while (incorrectGuesses < 5);
if(isFound()) {
System.out.println("Good Job. You guessed it right.");
} else {
System.out.println("Game Over. Better luck next time!!");
}
}
public static void main(String[] args) {
Hangman hangman = new Hangman("INDIA");
hangman.playGame();
}
public int getIncorrectGuesses() {
return incorrectGuesses;
}
public int getNumberOfGuesses() {
return numberOfGuesses;
}
public String getSecretWord() {
return secretWord;
}
public String getDisguisedWord() {
return disguisedWord;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.