Question

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.

Code

public class hiddenWord{
    String secretWord;
    public hiddenWord(String secretWord){
        this.secretWord = secretWord;
    }
    public String getHint(String guess){

        String secretWord = this.secretWord;
        int wordLength = secretWord.length();
        String hint = "";
        for(int i = 0; i < wordLength; i++){
            if(guess.charAt(i) == secretWord.charAt(i)){
                hint += secretWord.charAt(i);
            }
            else if(secretWord.indexOf(guess.charAt(i)) != -1){
                hint += '+';
            } else {
                hint += '*';
            }
        }
        return hint;
    }
    public static void main(String args[]){
        hiddenWord puzzle = new hiddenWord("HARPS");
        System.out.println(puzzle.getHint("AAAAA"));
        System.out.println(puzzle.getHint("HELLO"));
        System.out.println(puzzle.getHint("HEART"));
        System.out.println(puzzle.getHint("HARMS"));
        System.out.println(puzzle.getHint("HARPS"));
    }
}

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

This question took me a long time and was quite challenging for a variety of different reasons. For one, I kept on trying to set hint as a string with no value, like secretWord, but I kept on getting errors, as hint isn’t inputted as a parameter, but rather a string which is added to, as seen in the for loop below. I had a couple of mistakes on anatomy too, as I would miss small things like the brackets next to “args” in the main tester method. I was also struggling with isolating the characters, so I did have to research to find the charAt() method used. This method works to essentially index a string, allowing programmers to work with a specific character in a string, which was infinitely helpful here. Overall, I need to perfect my knowledge of Java anatomy, as that was a big thing here, and I need to do more research on various different methods that may be useful.