Reference no: EM131880940
Programming Assignment
1. Getting Started
In this assignment, you will be writing a word game that makes use of arrays and static methods. A class called WordList has been posted along with this assignment. You will be using a method in this class for generating a random word. Download WordList.java to the same folder as your code. You may need to refresh the folder for the file to appear in Eclipse: select the folder and either press the F5 function key or select File > Refresh from the menu. Do NOT make any changes to this class and do NOT submit it. Only submit your Hangman program.
2. Programming Assignment
Hangman
In this game, one player (Player A) picks a word and lets the other player (Player B) know how many letters are in it by drawing a template in which each letter is represented by a dash. Player B then guesses one letter at a time. If the letter is in the word, Player A writes it into the template in however many places that it appears. Player B continues to guess until either the number of allowed guesses runs out or the correct word has been guessed.
In the version you will be writing, the program will display a template in which all letters in the word to be guessed originally appear as dashes ('-'). After a correct guess by the player, the program will update the template by filling in all occurrences of that letter. If the guess is incorrect, a message stating "Incorrect" will be printed. The program will also list, in alphabetic order, all incorrect guesses so far if there are any. This will be followed by the template being printed. Prompting for another letter will continue until (1) the player has guessed the word correctly, in which case a message including the word "congratulations" will be displayed, or (2) the player has used up the allowed maximum of six incorrect guesses, in which case a message that includes the words "game over" will be displayed along with the correct word. In the following sample interactions, player input appears in bold:
Be sure to read through the entire set of requirements assignment before beginning.
Basic code requirements:
1. The player is allowed a maximum of six incorrect guesses. Store this value to a named constant. If the player has used up all incorrect guesses, the player has lost and the game ends. The player wins if the word is guessed prior to having amassed six incorrect guesses.
2. You must use a minimum of two arrays of type char in completing this assignment. One will store the letters in the word being guessed (wordToGuess below). The other will store the letters that the player has guessed correctly (playerGuess below).
• To generate a word for the player to guess, you will be invoking the static getWord() method from the WordList class, which will return a pseudorandomly selected word as an array of type char. Initially, it is recommended that you do not call this method from your code. Instead, initialize the array yourself so that you can test repeatedly with the same word until the code is working correctly. For example, you could start with:
char[] wordToGuess = {'e','f','f','e','c','t'};
When you are satisfied that your code is correct, replace the hardcoded array with the call to the the getWord() method from the WordList class. DO NOT copy this method into your code.
• The playerGuess array must be initialized with dashes (i.e., char values of '--').
• After prompting the player to enter a letter, use the charAt() method in conjunction with the next() method for getting the first character in the player's input.
• If the player has correctly guessed a letter, replace the dash(es) in the corresponding position(s) in the playerGuess array with the lowercase value of that letter. For example, given the letters stored to the wordToGuess array shown above, if the player's first guess is the letter ‘f', then the playerGuess array should be updated to contain {'--', 'f', 'f', '--', '--', '--'}.
• After an incorrect guess, display a message including the word "incorrect" (see additional requirements for listing all incorrect guesses and verifying input; it is recommended that you get this basic functionality working first).
• Display the contents of the player's array, regardless of whether the template has been updated.
• If the player has won, display a message including the word "congratulations." If the player has lost, include the word "over" in your message and print the word being guessed.
Additional requirements: static methods and input verification. Note: you are not limited to defining and using only the two methods specified below; this is a minimum requirement.
After getting the basic logic described above to work, add the following methods and functionality:
1. Define a static method with the following return type and signature:
char[] fillArray(char, int)
This method returns a new char array whose elements are set to the value of the first parameter and whose size is set to the value of the second parameter. For example, if passed the char '?' and the integer 5, the new array to be returned by this method would contain: {'?','?','?','?','?'}.
Use this method to create the player's array and initialize its contents to dashes.
2. Verify that the player has entered a letter (see the Character methods in section 4.3.4 of the textbook). If the input is not a letter, inform the player of this, using the word "not" in your error message. Then reprompt until a letter has been entered. Invalid guesses do not count as incorrect. See sample output 3.
3. If the player has entered a letter, store it in lowercase (see section 4.3) to make your code case-- insensitive (note that all of the words in the WordList class contain only lowercase letters).
4. If a letter has already been guessed, the player must be informed of this, with the word "before" included in your message. A repeated guess does not count as incorrect. See sample output 3. An additional array would be helpful here, and an additional method may be as well.
5. Display the list of incorrect guesses after any valid input, with each letter except for the last one in the list followed by a comma and a space (", ") as shown in the sample outputs. If the guess is incorrect, the list must come after the message informing the user of this. For full credit, display the list in alphabetical order. You may want to write a method to provide this functionality.
6. Define a static method with the following return type and signature:
replaceChar boolean (char, char[], char[])
This method first checks if the two arrays passed as arguments are of the same size. If they are the same, it searches for each occurrence of the value passed to the first parameter in the array passed to the second parameter. Each time it is found, that value is added to the same position in the array passed to the third parameter. For example,
if passed: 'a', {'b','a','n','a','n','a'}, {'b','--','--','--','--','--'}
then the contents of the second array will be updated to: {'b','a','--','a','--','a'}
A value of true is returned if any replacements have been made. If the second array is unchanged, a value of false is returned (note that if the arrays are of different sizes, no replacements will be made and the value returned will be false). Use this method to check if a guess is correct and to update the player's array with the correctly guessed letter.
Attachment:- Wordlist.rar