Question: 2. a) Write a method isValidGuess that takes as input a character representing the guess made by the player. The method returns true if such
2. a) Write a method isValidGuess that takes as input a character representing the guess made by the player. The method returns true if such character is a lower-case letter of the English alphabet, false otherwise. For example:
isValidGuess(g) returns true
isValidGuess(B) returns false
isValidGuess(!) returns false
b) write a method generateArrayOfGuesses() that takes as input a String representing the word to be guessed. The method returns an array of integers that the program will use to keep track of which character of the String has already been guessed by the player. At the very beginning none of the characters were guessed. To do so the method returns an array that has as many elements as the number of characters in the specified String. To indicate that none of the characters has been guessed yet, all the elements are initialized with value equal to 0. So, for example, generateArrayOfGuesses("pineapple") returns the array {0, 0, 0, 0, 0, 0, 0, 0, 0}.
c) write a method checkAndUpdate() that takes as input a String representing the secret word, an array of integers representing the guesses made by the player, and a char representing the last guess made by the player. For the purpose of this method, you can assume that the guess is valid (i.e. it is a lower case letter of the English alphabet). The method should check if the guess provided is indeed a character contained in the secret word. If so, the method updates the array by assigning the value 1 to the element corresponding to the position of the given character inside the secret word. In this case, the method should also return true confirming that the guess was indeed a good one. Otherwise, if the character provided is not contained in the secret word, the method simply returns false.
For example, consider the following array:
int[] guesses = {0, 0, 0, 1, 0, 0, 0, 0, 1}; Note that the array indicates that the character e has already been guessed by the player. Then:
checkAndUpdate("pineapple",guesses,t) returns false and leaves the array unchanged.
checkAndUpdate("pineapple",guesses,i) returns true and updates the value of the second element with 1.
checkAndUpdate("pineapple",guesses,p) returns true and updates the values of the first, the sixth, and the seventh element with 1.
checkAndUpdate("pineapple",guesses,e) returns true (even if the character had already been guessed!) and updates the values of the fourth, and ninth element with 1.
d) write a method getWordToDisplay that takes as input a String representing the secret word, an array of integers representing the guesses made by the player, and a char representing the last guess made by the player. For the purpose of this method, you can assume that the guess is valid (i.e. it is a lower case letter of the English alphabet). The method returns a String representing the sequence of characters that the player can now see after the last guess has been made. Such String is built by going through all the characters of the secret word and:
If the character matches with the last guess made by the player, then it is replaced by the same letter but in upper case.
If the character does not match with the last guess, but it has been previously guessed by the player, then it is kept as is.
Finally, if the character has not been guessed by the player yet, then it should be replaced by the hyphen - character.
For example:
int[] guesses = {0, 0, 0, 1, 0, 0, 0, 0, 1};
Then:
getWordToDisplay("pineapple",guesses,t) returns "---e----e".
getWordToDisplay("pineapple",guesses,i) returns "-I-e----e".
getWordToDisplay("pineapple",guesses,p) returns "P--e-PP-e".
getWordToDisplay("pineapple",guesses,e) returns "---E----E".
e) Write a method isWordGuessed() which takes as input an array of integers (representing the guesses) and returns true if all the elements are equal to 1, false otherwise.
f) Write a method play() which takes an integer as input and simulates a game of Guess the word!. The method creates an object of type Scanner to retrieve the inputs from the user (remember that to use Scanner you should add the appropriate import statement at the beginning of your file). The method should then do the following:
1. Generate a random secret word to be guessed using getRandomWord().
2. Generate the corresponding array of guesses.
3. Display a welcome message to the player letting them know how many characters the secret word has and that they have 10 lives to guess the entire word.
4. Display a message letting the player know how many lives they have left and asking for their next guess.
5. If the player provides a String that contains more than one character, then an error message is displayed. The player should not lose a life for doing this.
6. If the guess is a single character, but it is not valid, then a different error message should be displayed. Once again, the player should not lose a life for doing this.
7. If the guess is a valid, then the method checks whether it is correct or not (and the array of guesses gets updated). In both cases, a word is displayed containing the characters that the player has already guessed or hyphens otherwise. If the last guess was incorrect, the player loses 1 life.
8. Go back to step 4 and ask for a new guess until the game is completed or until the player has lost all their lives.
9. If the game ends because the player has guessed the word, then a congratulatory message should be displayed. Otherwise, a message letting them know that they have no lives left should be displayed and the secret word should be revealed.
Note that, to get full marks, your method must use all of the methods described above. To complete the program, simply call play() from the main method providing an integer of your choice as input. Note that the word generated depends on that integer. If you want to test your program with different words you should change the integer provided as input.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
