Question 2: Classes

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.

public class HiddenWord {
    private String hiddenWord;

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

    public String getHint(String guess) {
        StringBuilder hint = new StringBuilder();

        for (int i = 0; i < hiddenWord.length(); i++) {
            char c = guess.charAt(i);
            if (c == hiddenWord.charAt(i)) {
                hint.append(c); // Letter matches position in the hidden word
            } else if (hiddenWord.indexOf(c) >= 0) {
                hint.append("+"); // Letter is in the hidden word but in a different position
            } else {
                hint.append("*"); // Letter is not in the hidden word at all
            }
        }

        return hint.toString();
    }
}

public class Game {
    public static void main(String[] args) {
        HiddenWord puzzle = new HiddenWord("HARPS");
        System.out.println(puzzle.getHint("AAAAA")); // Outputs: +A+++
        System.out.println(puzzle.getHint("HELLO")); // Outputs: H****
        System.out.println(puzzle.getHint("HEART")); // Outputs: H*++*
        System.out.println(puzzle.getHint("HARMS")); // Outputs: HAR*S
        System.out.println(puzzle.getHint("HARPS")); // Outputs: HARPS
    }
}

Game.main(null);
+A+++
H****
H*++*
HAR*S
HARPS

FRQ 2 Reflection

This frq was quite fun actually. It essentially mimics the game wordle, and I found myself playing it when I probably should have been working on the other frqs. I had a blast trying to get it to work. It reminded me of my early days in python where I would make games almost entirely out of if statements. One of the most memorable of these games (not for good reasons) for example was this rpg game where you would select different options and it would change the story. In my naivety, I didn’t know how to make the code ask the question again if you picked an answer that made you lose a life or that didn’t do anything, so I created every single possible path that they could’ve selected. This was the most tedious thing I’ve ever done it took me weeks and was over 2000 lines of code. Even though it took me weeks straight of work, I don’t remember ever having a better time coding than then, and I felt that again with this question. However, the difference being this time I can make the game in 3 if statements as oppose to 100. It felt amazing making a more complicated fully functional game in just a few lines of code.