The Problem & First Impressions

The problem involves creating a Java class called HiddenWord to represent a hidden word in a guessing game. The hidden word, initialized in the constructor, consists only of capital letters and is of a known length. The class features a method, getHint, designed to generate a hint based on the comparison between the hidden word and a player’s guess. The hint provides information about matching letters in the same position, matching letters in different positions, and letters not present in the hidden word. The task specifies example instances of the HiddenWord class and the expected hints corresponding to various guesses. The implementation involves handling these rules to produce accurate hints. Topics such as object-oriented programming, instance variables, and constructors are employed to represent and initialize the hidden word.

Prompt Summary

  • Implement a Java class, HiddenWord, for a guessing game. The class includes a method, getHint, which generates hints based on the comparison between the hidden word and a player’s guess.

My Solution:

  • Class Structure:
    • Created a class named HiddenWord.
    • Included a private attribute named solution to store the solution string.
  • Constructor:
    • Implemented a constructor to initialize the value of the solution attribute.
  • Helper Method:
    • Introduced a private method named isContainChar to test if the solution contains a certain character.
  • Main Method (getHint):
    • Implemented the required getHint method as per the problem statement.
    • Utilized a for-loop to sequentially compare each letter of the input and solution strings.
    • Checked for a match, and if not found, verified if the character existed elsewhere in the string.
    • If neither condition was met, added a “*” to denote the absence of the character within the string.
    • The final result was a solution string providing hints based on the comparison of the input and solution strings.
class HiddenWord {
    private String solution;
    
    HiddenWord(String word) {
        this.solution = word;
    }

    private boolean isContainChar(char character) {
        for (int i = 0; i<this.solution.length(); i++) {
            if (character == this.solution.charAt(i)) {
                return true;
            }
        }
        return false;
    }

    public String getHint(String input) {
        String output = "";
        for (int i = 0; i < input.length(); i++) {
            if (input.charAt(i)==this.solution.charAt(i)) {
                output = output + input.charAt(i);
            } else if (isContainChar(input.charAt(i))) {
                output = output + "+";
            } else {
                output = output + "*";
            }
        }
        return output;
    }
}

HiddenWord puzzle = new HiddenWord("GOOGLY");
String[] testCases = {"SHHOT", "BOOGY", "SHART", "OOOPE", "YEAAH"};
for (int i = 0; i < testCases.length; i++) {
    System.out.println("Hint for string \"" + testCases[i] + "\": " + puzzle.getHint(testCases[i]));
}

Hint for string "SHHOT": ***+*
Hint for string "BOOGY": *OOG+
Hint for string "SHART": *****
Hint for string "OOOPE": +OO**
Hint for string "YEAAH": +****