Consider a guessing game in which a player tries to guess a hidden word. The hidden word contains only capital letters and has a length known to the player. A guess contains only capital letters and has the same length as the hidden word.
After a guess is made, the player is given a hint that is based on a comparison between the hidden word and the guess. Each position in the hint contains a character that corresponds to the letter in the same position in the guess. The following rules determine the characters that appear in the hint.
A table is shown with two columns titled If the letter in the guess is… on the left and the corresponding character in the hint is on the right. The first row reads also in the same position in the hidden word on the left, and the matching letter on the right. The second row reads also in the hidden word but in a different position on the left, and a plus sign on the right. The third row reads not in the hidden word on the left, and an asterisk on the right.
The HiddenWord class will be used to represent the hidden word in the game. The hidden word is passed to the constructor. The class contains a method, getHint, that takes a guess and produces a hint.
For example, suppose the variable puzzle is declared as follows.
HiddenWord puzzle = new HiddenWord(“HARPS”);
The following table shows several guesses and the hints that would be produced.
Solution
The code simulates a word guessing game where the player tries to guess a hidden word. The getHint method provides hints based on the player’s guesses, indicating correct letters in their correct positions, correct letters in incorrect positions, and incorrect letters. the getHint method compares each letter of the guessed word with the corresponding letter of the hidden word. Based on the comparison results, it constructs a hint string that indicates correct letters in their correct positions, correct letters in incorrect positions, and incorrect letters. Finally, it returns the hint string.
public class HiddenWord {
private String word;
public HiddenWord(String word) {
this.word = word;
}
public String getHint(String guess) {
// Initialize an empty hint string
String hint = "";
// Loop through each character position in the hidden word
for (int i = 0; i < word.length(); i++) {
// Get the guessed letter at the current position
char guessedLetter = guess.charAt(i);
// Get the actual letter from the hidden word at the current position
char actualLetter = word.charAt(i);
// Compare the guessed letter with the actual letter
if (guessedLetter == actualLetter) {
// If they match, append the actual letter to the hint
hint += actualLetter;
} else if (word.contains(Character.toString(guessedLetter))) {
// If the guessed letter is in the hidden word but not at the current position,
// append '+' to indicate it's in the word but not at the right position
hint += '+';
} else {
// If the guessed letter is not in the hidden word, append '*' to indicate a wrong guess
hint += '*';
}
}
// Return the hint string
return hint;
}
public static void main(String[] args) {
HiddenWord puzzle = new HiddenWord("HARPS");
System.out.println(puzzle.getHint("AAAAA")); // Output: *****
System.out.println(puzzle.getHint("HELLO")); // Output: H****
System.out.println(puzzle.getHint("HEART")); // Output: H*++*
System.out.println(puzzle.getHint("HARMS")); // Output: H***S
System.out.println(puzzle.getHint("HARPS")); // Output: HARPS
}
}
HiddenWord.main(null);
+A+++
H****
H*++*
HAR*S
HARPS
Reflections and Key Learning
I learned about encapsulation through the HiddenWord class, where I kept the word attribute private and provided a public method getHint() to interact with it. This approach promotes good object-oriented design by hiding internal implementation details and providing a clear interface for interacting with the class. Another skill which I developed was String manipulation, this exercise required significant string manipulation, including accessing characters in a string using the charAt() method, checking if a string contains a specific character with the contains() method, and building a new string using a StringBuilder. Thirdly, I improved my Conditional Logic abilities, in the getHint() method, I used conditional statements (if-else) to determine the hint for the guessed word. By checking each character of the guessed word against the corresponding character in the hidden word, I adjusted the hint accordingly. Lastly, I became more proficient at Debugging and Testing, the main method served as the test for the HiddenWord class, providing various input cases and verifying the correctness of the getHint() method’s output.