日期:2014-05-20  浏览次数:20818 次

java 猜单词
用户需要猜一个单词,没猜对一个字母,这个字母会显示出来。直到用户猜错数达到7次本游戏结束,然后问用户是否还要在进行新的游戏。如果要进行新的游戏,那么让用户输入一个新的单词, 之后让另一个用户猜。
程序要求如下:
1.) The class HangMan should have following instance variables:
Secret_word: The word that needs to be guessed. access modifier: private; type: String
Disguised_word: The word which in which question marks are replaced with correct guesses. (initially it will be all question marks). access modifier: private; type: String
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
All of the instance variables must be private.
2.) This class should have a constructor to initialize all the above instance variables.
Secret_word should be initialized to a secret word of your choosing. This will be the secret word for the first time the game is played.
Disguised_word to "???????" (length of Disguised_word should be the same as the length of Secret_word)
Initialize GuessCount to 0 and MissesCount to 0
3.) This class should have following public methods. Only these methods should be public.
void setSecretWord(String newWord): Sets Secret_word. Also sets Disguised_word (to have the same length as Secret_word). If a game is in progress, calling this method should reset the game (set the number of guesses and misses to zero).
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 isFound(): Returns true if the secret word was discovered.
String getDisguisedWord(): Returns Disguised word, which contains correctly guessed letters in their correct positions and unknown letters as ?.
int getGuessCount(): Returns the number of guesses made.
int getMissesCount(): Returns the number of incorrect guesses made.
Hint: String methods charAt and substring and other String methods may be useful to you.
4.) For each method write comments clearly.
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 HangManGame 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, HangManGame is what is known as the front end and the HangMan class is considered the back end.
http://en.wikipedia.org/wiki/Front_and_back_ends
7.) Now implement this class by writing HangManGame.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. 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.
打印样例如下:

(secret word: "Bigbang")
******HangMan Game Starts*********** 

Guess This: ??????? 
Enter your guess character: a
Number of guesses so far: 1
Number of Misses so far: 0

Guess this: ????a?? 
Enter your guess character: e
Number of guesses so far: 2
Number of Misses so far: 1

Guess this: ????a??
Enter your guess character: i
Number of guesses so far: 3
Number of Misses so far: 1

Guess this: ?i??a?? 
Enter your guess character: b
Number of guesses so far: 4
Number of Misses so far: 1