The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 399 Weeks Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
In this question, you are required to construct a class that could be used to play a game of hangman. For those of you who havenA????1t played this game, check out web resources. There is an animation of the game at http://www.proprofs.com/games/word-games/hangman/animation/
We will simplify the game a bit. Rather than randomly generating a secret word from a large dictionary, we will use the words from a much smaller dictionary of 25 words. These will be the names of the first 25 Pokemon characters. These 25 words will be stored in a String array, and each time the game is played, a word is randomly selected from this array. The words are the following:
String[] secretWordDictionary = {A????1bulbusaurA????1, A????1ivysaurA????1, A????1venusaurA????1, A????1charmanderA????1, A????1charmeleonA????1, A????1charizardA????1, A????1squirtleA????1, A????1wartotleA????1, A????1blastoiseA????1, A????1caterpieA????1, A????1metapodA????1, A????1butterfreeA????1, A????1weedleA????1, A????1kakunaA????1, A????1beedrillA????1, A????1pidgeeA????1, A????1pidgeottoA????1, A????1pidgeotA????1,A????1rattataA????1, A????1raticateA????1, A????1spearowA????1, A????1fearowA????1, A????1ekansA????1, A????1arbokA????1, A????1pikachuA????1};
The class has the following attributes:
a. b. c.
d. e.
It will have the
A????1 A????1
A????1
A????1
A????1 A????1
The secretWordDictionary: the array that has been given above.
The secret word: private char[] word (that is, a char array)
The disguised word (another array of char), in which each unknown letter in the secret word is replaced with a question mark (?). For example, if the secret word is abracadabra and the letters a and b have been guessed, the disguised word would be ab?a?a?ab?a.
The number of guesses made.
The number of incorrect guesses.
following methods:
No-args constructor: creates an empty Hangman object
Get methods for the disguised word, number of guesses made and number of incorrect guesses. (DO NOT add public get methods for the secretWordDictionary or the secretword since they must not be accessible from outside. If you need these get methods in your program, make them private).
Set methods for secret word, disguised word, number of guesses made and number of incorrect guesses. You donA????1t need a set method for the secretWordDictionary since it is hard-coded into the program.
generateSecretWord(): Generate a random number from 0 to 24 and select the appropriate word from the secretWordDictionary. Convert it into a char array and store in char[] word. makeGuess(char c) guesses that character c is in the word..
isFound returns true if the hidden word has been discovered.
HereA????1s the skeleton outline for the program Hangman.java.
public class Hangman {
}
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"};
private char[] secretWord; private char[] disguisedWord; private int numGuesses; private int numIncorrect;
//continue the rest of the code
Test the above class to ensure that all the methods are functional.
Next implement a demo class by writing a demo program Hangmandemo.java and simulate the game of hangman.
Notes:
Keep a limit of six on the number of incorrect guesses, and quit the game if the user doesnA????1t get the word right within the limit.
You can use two char arrays to keep track of the letters that were guessed, one for correct guesses and another for incorrect guesses. You can use this to prompt the user that they cannot pick a letter that was already guessed.
Note the conversions between a char array and a String.
String s = new String(word);
will convert a char array word into a String s.
Similarly,
char[] word = s.toCharArray();
will convert a String s into a char array word. Here are two sample screen dialogs:
Welcome to the Hangman game.
I have a secret Pokemon character name. You have to guess it. You are allowed only six incorrect guesses.
Secret word: ??????? Enter the guess: a Correct.
Secret word: ?a??a?a
Enter the guess: s
Incorrect. You have 5 incorrect guesses left.
Secret word: ?a??a?a Enter the guess: t Correct.
Secret word: ?attata
Enter the guess: g
Incorrect. You have 4 incorrect guesses left.
Secret word: ?attata
Enter the guess: a
You already guessed a. Choose another letter.
Secret word: ?attata Enter the guess: r Correct.
Secret word: rattata You win!
Another sample screen dialog:
Welcome to the Hangman game.
I have a secret Pokemon character name. You have to guess it. You are allowed only six incorrect guesses.
Secret word: ?????
Enter the guess: t
Incorrect. You have 5 incorrect guesses left.
Secret word: ????? Enter the guess: s Correct.
Secret word: ????s
Enter the guess: m
Incorrect. You have 4 incorrect guesses left.
Secret word: ????s
Enter the guess: g
Incorrect. You have 3 incorrect guesses left.
Secret word: ????s
Enter the guess: z
Incorrect. You have 2 incorrect guesses left.
Secret word: ????s
Enter the guess: p
Incorrect. You have 1 incorrect guesses left.
Secret word: ????s
Enter the guess: b
Incorrect. You have 0 incorrect guesses left.
The secret word was ekans. You lose!
Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n.P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll