Hangman Game, JavaCollection。
Requirement
This programming assignment will give you practice with the Java collections classes. You are going to write a class that keeps track of the state of a game of hangman. But this won’t be any ordinary game of hangman. Our hangman program is going to cheat.
If you aren’t familiar with the general rules of hangman, you should review the wikipedia entry for it: http://en.wikipedia.org/wiki/Hangman_%28game%29
In a normal game of hangman, the computer picks a word that the user is supposed to guess. In our game of hangman, the computer is going to delay picking a word until it is forced to. As a result, at any given point in time there will be a set of words that are currently being used by the computer. Each of those words will have the same pattern to be displayed to the user. A client program called HangmanMain has been written for you. It handles the user interaction. You are to write a class called HangmanManager that keeps track of the state of the game. You won’t be writing any code that prints information or reads information from the user because all of that code is in HangmanMain.
Your class should have the following public methods.
| Method |
Description |
| HangmanManager(List dictionary, int length, int max) |
Your constructor is passed a dictionary of words and a target word length and the maximum number of wrong guesses the player is allowed to make. It should use these values to initialize the state of the game. The set of words should initially be all words from the dictionary that are of the given length. The constructor should throw an IllegalArgumentException if length is less than 1 or if max is less than 0. |
| Set words() |
The client calls this method to get access to the current set of words being considered by the hangman manager. |
| int guessesLeft() |
The client calls this method to find out how many guesses the player has left. |
| SortedSet guesses() |
The client calls this method to find out the current set of letters that have been guessed by the user. |
| String pattern() |
This should return the current pattern to be displayed for the hangman game taking into account guesses that have been made. Letters that have not yet been guessed should be displayed as a dash and there should be spaces separating the letters. There should be no leading or trailing spaces. This method should throw an IllegalStateException if the set of words is empty. |
| int record(char guess) |
This is the method that does most of the work by recording the next guess made by the user. Using this guess, it should decide what set of words to use going forward. It should return the number of occurrences of the guessed letter in the new pattern and it should appropriately update the number of guesses left. This method should throw an IllegalStateException if the number of guesses left is not at least 1 or if the list of words is empty. It should throw an IllegalArgumentException if the list of words is nonempty and the character being guessed was guessed previously. |
There are two unusual aspects of the guesses method. It returns a SortedSet rather than a Set. SortedSet is a variation of the Set interface that requires that the values in the set are to appear in increasing order. We want this property for the set of guesses so that we can show the user the guesses in alphabetical order. The TreeSet class implements this interface, just as TreeSet implements the Set interface. Also notice that it is a set of Character values. We can’t make a set of char values. Character is the wrapper class for char values, which is why it is defined this way. But you can generally manipulate the set as if it were a set of simple char values (e.g., calling add or contains with a simple char value).
Notice that the pattern and record methods throw an exception when the list of words is empty. The only way this can happen is if the client requests a word length for which there are no matches in the dictionary. For example, the dictionary does not have any words of length 25.
As noted earlier, this version of hangman cheats. It doesn’t actually pick a word until it needs to. Suppose that the user has asked for a 5-letter word. Instead of picking a specific 5-letter word, it picks all 5-letter words from the dictionary. But then the user makes various guesses, and the program can’t completely lie. It has to somehow fool the user into thinking that it isn’t cheating. In other words, it has to cover its tracks. Your HangmanManager obje†⁹⁶††‾⸠›‾†⁷⁝‾䄼 ⁴††ⁱ⁅†⁵†䥩†Ɐ⁵ⁱ…⁵……⁏⁵㉲び†⁶⠠†††…⥳⸮†䥅⁷†⁴††㈦ぬ†ⱬ ‼ⁱ⁵㱣⽨㹩൳ਠ㱴㹥⁴††††䡩䵰⸠※†⁵⸠⁴†⁛⁝⁵†ㄭ㈠㜭†⁷䕳⁴ⁱ†䥩††⁛Ⱟ‾ ‾⁴††⁹†††㉹⸠†⁰⁷†䡹䵶††⁰⸠⁴⁴†䈠ⱬ⁴†⁴⁴㉲†⁰㥴†††⁷†㥦†††††††※…⁵䠻䍨䉵††⁴⸾䈊⁰⁹Ɐ‼⁰⁓⁹⁷㰠⽡㹷൲੯㱮㸠䭧 ‼⁴Ⱐ†⁰⁴⁹⁴⁴Ⱶ†♴㬠⁹⁵†⁷⁰‾ⁱ†⁴⡮⥧†⁵☼㬾 ‼⠠⥥⸠††⁶⁷⁵††⁴‼☍㬾ⸯ㱣⽯㹥ാ਼㰯㹲䥥⁰⁹⁵††⁵†††ⱥ††䩰 ⁵††♯㭮†⁴♲㭴†⁰⸠⁰♡㬠⁵⸠…䝔䅲⁵⁔†…※⁰⁉㩹⽬䍩⽳⽴䙯⼠䅡††…♲㭳䌠♮㭩※㩨㱥⼠㹡൭੩㱬㹬㱤㸭ⴠ㨦†㱷⽨㹔㰠⽡㹡൲ੳ㰠㹯⁷†䡯䵱⁵…⁵⁹☠㭦䡡♲㬦†⁶††㱳⽴㹦amily, and by picking it you’d maintain the word list you already had and you would count this as an incorrect answer.
To implement this strategy, you should use a map. The keys will be the different patterns for each word family. Those keys should map to a set of words that have that pattern. For each call on record, you will find all of the word families and pick the one that has the most elements. This will become the new set of words for the next round of the game. If there is a tie (two of the word families are of equal size), you should pick the one that occurs earlier in the map (i.e., the one whose key comes up first when you iterate over the key set).
You are expected to do some error checking, as outlined in the descriptions of the public methods. But you aren’t checking for all possible errors. You may assume that the list of words passed to your constructor is legal in that it will be a nonempty list of nonempty strings composed entirely of lowercase letters. You may assume that all guesses passed to your record method are lowercase letters.
You should use the TreeSet and TreeMap implementations for all of your sets and maps. You should use interfaces for all variables, fields and parameters. You sho