Chapter 5, PP 6 Java Project Name: IC15_Hangman Implement a class named Hangman
ID: 3810674 • 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:
must have two classes : Hangman and HangmanDemo
Explanation / Answer
Program Code:
//main class
public class Hangman
{
private final int NUM_GUESSES = 7;
private char[] secretWord;
private char[] disguisedWord;
private char[] lettersSeen;
private int numGuesses;
String word ;
private int numIncorrect;
private String[] secretWordDictionary = {"bulbusaur","ivysaur",
"charmander","charmeleon", "charizard", "squirtle", "wartotle",
"blastoise", "caterpie", "metapod", "butterfree", "weedle", "kakuna",
"beedrill", "pidgee", "pidgeotto", "pidgeot","rattata", "raticate",
"spearow", "fearow", "ekans", "arbok", "pikachu"};
// Selects a random word and initializes the Hangman object.
public Hangman()
{
generateSecreteWord();
setLettersSeen();
setGuessRemaining();
}
public void setLettersSeen()
{
// initializes the lettersSeen array to be full of dashes
lettersSeen = new char[26];
for (int i = 0; i < 26; i++)
{
lettersSeen[i] = '?';
}
}
public void setGuessRemaining()
{
// sets the number of guesses
numGuesses = NUM_GUESSES;
}
public void setIncorrectGuesses()
{
numIncorrect=0;
}
public int IncorrectGuesses()
{
return numIncorrect;
}
public void generateSecreteWord()
{
// gets a random word from the word list
// gets a random word from the word list
word= secretWordDictionary[(int)(Math.random()*secretWordDictionary.length)];
// initializes the secretWord and guess arrays to hold the word
// and the guesses
setSecreteWord(word);
setDisguisedWord(word);
}
public void setSecreteWord(String word)
{
// initializes the secretWord arrays to hold the word
secretWord = new char[word.length()];
for (int i = 0; i < word.length(); i++)
{
secretWord[i] = word.charAt(i);
}
}
public void setDisguisedWord(String word)
{
// initializes the guess arrays to hold the word
disguisedWord = new char[word.length()];
// fills in guess
for (int i = 0; i < word.length(); i++)
{
disguisedWord[i] = '?';
}
}
public String getWord()
{
return word;
}
public char[] getDisguisedWord()
{
return disguisedWord;
}
// Returns true if the letter has been used before
// and false if the letter is new.
public boolean makeGuess(char letter)
{
String alphabet = "abcdefghijklmnopqrstuuvwxyz";
// gets the index of this letter in the alphabet
int index = alphabet.indexOf(letter);
// The letter has not been seen before if the alphabet at
// index is still '?'. Otherwise, the letter has been seen.
boolean seen = lettersSeen[index] != '?';
// sets that place in the array to the letter
lettersSeen[index] = letter;
return seen;
}
// Returns true if the letter is in the secretWord word and false otherwise
public boolean isFound(char letter)
{
boolean guessCorrect = false;
// goes through each letter in the secretWord word
// and checks it with the guessed letter
// Replaces the dash in guess with letter wherever
// letter appears in secret word
for (int i = 0; i < disguisedWord.length; i++)
{
// if it is a good guess
// it sets the guess array to this letter
if (secretWord[i] == letter)
{
disguisedWord[i] = letter;
guessCorrect = true;
}
}
// if it was a bad guess takes away from numGuesses
if (!guessCorrect)
{
numGuesses--;
numIncorrect++;
}
return guessCorrect;
}
// Returns true if the player has guessed all the letters in the
// secret word and false if not.
public boolean hasWon()
{
boolean won = true; // Start by assuming player guessed all the letters
// If find a letter not guessed then set to false
// checks if they won by looking at the guess array
// if there was any dashes left, it was not completed
for (int i = 0; i < disguisedWord.length && won; i++)
{
if (disguisedWord[i] == '?') won = false;
}
return won;
}
// Returns the number of guesses the player has remaining
public int getGuessesRemaining()
{
return numGuesses;
}
// Returns a string summarizing the number of guesses remaining,
// the partially filled word, and the letters previously used,
public String toString()
{
String s = "You have " + numGuesses + " guesses remaining.";
s+=" Secrete word: ";
// goes through adding the letters in the guess array
for (int i = 0; i < disguisedWord.length; i++)
{
s += disguisedWord[i];
}
return s;
}
}
//Implementation class
import java.util.Scanner;
public class HangmanGame
{
public static void main(String[] args)
{
// first creates the Scanner to get input
Scanner keyboard = new Scanner(System.in);
System.out.println("WELCOME TO THE GAME OF HANGMAN! ");
System.out.println("I have a secret Pokemon character name. You have to guess it. You are allowed" +
" only six incorrect guesses.");
Hangman game = new Hangman();
// will loop around until there are no more guesses
// or they win the game
do {
// starts by printing out the current game
System.out.println(game);
// a variable for whether or not we have seen the guess
boolean seenGuess;
char guess;
// will loop around until they get a new guess
do {
System.out.print("Enter a guess: ");
guess = keyboard.nextLine().toLowerCase().charAt(0);
// checks if the guess has been seen
seenGuess = game.makeGuess(guess);
// if it has, prints an error message
if (seenGuess)
{
System.out.println("You have used " + guess + " already");
}
} while (seenGuess);
// prints out whether or not it was a good guess
if (game.isFound(guess))
{
System.out.println("Correct.");
}
else
{
System.out.println("Incorrect.");
}
System.out.println();
} while (game.getGuessesRemaining() > 0 && !game.hasWon());
// prints out whether or not you won
if (game.hasWon())
{
System.out.println("Screte Word: "+game.getWord() );
System.out.println("YOU WIN!");
}
else
{
System.out.println("The screte word was "+game.getWord() );
System.out.println("Sorry, you lose.");
}
}
}
Sample output:
WELCOME TO THE GAME OF HANGMAN!
I have a secret Pokemon character name. You have to guess it. You are allowed only six incorrect guesses.
You have 7 guesses remaining.
Secrete word: ?????????
Enter a guess: p
Correct.
You have 7 guesses remaining.
Secrete word: p????????
Enter a guess: c
Incorrect.
You have 6 guesses remaining.
Secrete word: p????????
Enter a guess: a
Incorrect.
You have 5 guesses remaining.
Secrete word: p????????
Enter a guess: ka
Incorrect.
You have 4 guesses remaining.
Secrete word: p????????
Enter a guess: c
You have used c already
Enter a guess: h
Incorrect.
You have 3 guesses remaining.
Secrete word: p????????
Enter a guess: i
Correct.
You have 3 guesses remaining.
Secrete word: pi???????
Enter a guess: t
Correct.
You have 3 guesses remaining.
Secrete word: pi????tt?
Enter a guess: y
Incorrect.
You have 2 guesses remaining.
Secrete word: pi????tt?
Enter a guess: s
Incorrect.
You have 1 guesses remaining.
Secrete word: pi????tt?
Enter a guess: r
Incorrect.
The screte word was pidgeotto
Sorry, you lose.
WELCOME TO THE GAME OF HANGMAN!
I have a secret Pokemon character name. You have to guess it. You are allowed only six incorrect guesses.
You have 7 guesses remaining.
Secrete word: ??????
Enter a guess: p
Incorrect.
You have 6 guesses remaining.
Secrete word: ??????
Enter a guess: e
Correct.
You have 6 guesses remaining.
Secrete word: ?e????
Enter a guess: a
Correct.
You have 6 guesses remaining.
Secrete word: ?ea???
Enter a guess: r
Correct.
You have 6 guesses remaining.
Secrete word: ?ear??
Enter a guess: o
Correct.
You have 6 guesses remaining.
Secrete word: ?earo?
Enter a guess: w
Correct.
You have 6 guesses remaining.
Secrete word: ?earow
Enter a guess: f
Correct.
Screte Word: fearow
YOU WIN!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.