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. Guess Letter Hint Character In same position and also in hidden word Matching letter In hidden word, but in different position “*” Not in hidden word ”+” Instructions: 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. Example: Suppose the variable puzzle is declared as follows:

The following table shows several guesses and the hints that would be produced:

Call to getHint String returned puzzle.getHint(“AAAAA”) “+A+++” puzzle.getHint(“HELLO”) “H*” puzzle.getHint(“HEART”) “H++” puzzle.getHint(“HARMS”) “HARS” puzzle.getHint(“HARPS”) “HARPS” This table demonstrates the correspondence between the guess and the hint produced by the getHint method based on the hidden word.

Reflection

This was somewhat difficult of an FRQ question but since it was only one part it made it a lot easier to manage and spend my time wisely on. After getting the getHint function to work I was able to much easily get the rest of the code working as I wanted.

public class HiddenWord {
    private String hiddenWord;

    public HiddenWord(String word) {
        this.hiddenWord = word;
    }

    public String getHint(String guess) {
        StringBuilder hint = new StringBuilder();
        for (int i = 0; i < hiddenWord.length(); i++) {
            if (guess.charAt(i) == hiddenWord.charAt(i)) {
                hint.append(hiddenWord.charAt(i));
            } else if (hiddenWord.indexOf(guess.charAt(i)) != -1) {
                hint.append("+");
            } else {
                hint.append("*");
            }
        }
        return hint.toString();
    }
    public static void main(String[] args){
        HiddenWord harps = new HiddenWord("PARAV");

        System.out.println(harps.getHint("PLANS"));
        System.out.println(harps.getHint("PARIS"));
        System.out.println(harps.getHint("PARAV"));
    }

}
HiddenWord.main(null);
P*+**
PAR**
PARAV

Explanation:

This Java code defines a class called HiddenWord, which stores a hidden word to be guessed. The getHint method compares a guessed word to the hidden word, providing hints with “+” indicating correct letters in incorrect positions, the letter itself for correct guesses, and “*” for incorrect guesses. The main method demonstrates the functionality by creating an instance of HiddenWord with the word “PARAV” and printing the hints for various guesses.