Question 2:
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.
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.
Write the complete HiddenWord class, including any necessary instance variables, its constructor, and the method, getHint, described above. You may assume that the length of the guess is the same as the length of the hidden word.
Question Type:
- Classes
Overall Objectives:
Create a wordle like game that has the following:
- A main
HiddenWord
class - Create the variables and constructors tha will allow for the class to work
- Create a method called
getHint
that uses the rules given above
public class HiddenWord { // A main `HiddenWord` class
private final String hiddenWord = "HARPS"; // Create the variables and constructors tha will allow for the class to work
public String getHint(String guess) { // Create a method called `getHint` that uses the rules given above
StringBuilder hint = new StringBuilder();
for (int i = 0; i < hiddenWord.length(); i++) {
char guessChar = guess.charAt(i);
char hiddenChar = hiddenWord.charAt(i);
if (guessChar == hiddenChar) {
hint.append(hiddenChar); // Correct letter at correct position
} else if (hiddenWord.indexOf(guessChar) != -1) {
hint.append("+"); // Correct letter but at the wrong position
} else {
hint.append("*"); // Incorrect letter
}
}
return hint.toString();
}
public static void main(String[] args) { // Test data
HiddenWord puzzle = new HiddenWord();
System.out.println("Guess: AAAAA, Output: " + puzzle.getHint("AAAAA")); // Guesses
System.out.println("Guess: HELLO, Output: " + puzzle.getHint("HELLO"));
System.out.println("Guess: HEART, Output: " + puzzle.getHint("HEART"));
System.out.println("Guess: HARPS, Output: " + puzzle.getHint("HARPS"));
}
}
HiddenWord.main(null)
Guess: AAAAA, Output: +A+++
Guess: HELLO, Output: H****
Guess: HEART, Output: H*++*
Guess: HARPS, Output: HARPS
Reflection:
- This frq was honestly really fun to work with because I felt that it was kind of fun seeing and creating the algorithm that is used for wordle
- This frq was pretty easy to understand, you just use the stringbuilder and a loop to append and create the outputs and overall it was really more of a fun frq and didn’t feel like I was doing a college board frq