Implementation of the HiddenWord class to represent the hidden word in the guessing game:

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 guessChar = guess.charAt(i);
            char hiddenChar = hiddenWord.charAt(i);
            if (guessChar == hiddenChar) {
                hint.append(hiddenChar);  // matching letter
            } else if (hiddenWord.indexOf(guessChar) >= 0) {
                hint.append("+");  // letter in hidden word, but different position
            } else {
                hint.append("*");  // letter not in hidden word
            }
        }
        return hint.toString();
    }
}

Explanation

Instance Variable:

hiddenWord: This is a private instance variable that holds the hidden word for the game.


Constructor:

HiddenWord(String word): This is the constructor that initializes the hiddenWord with the given word.


Method:

getHint(String guess): This method takes a guess as input and produces a hint based on the comparison between the hidden word and the guess.

  • It iterates through each character in the hiddenWord and compares it with the corresponding character in the guess.
  • If the characters match, it appends the character to the hint.
  • If the character is in the hiddenWord but at a different position, it appends a ‘+’ to the hint.
  • If the character is not in the hiddenWord, it appends a ‘*’ to the hint.
  • Finally, it returns the hint as a String.

Blog: Key Algorithm and Matching FRQ Type

Method and Control Structure:

  • The getHint method uses if-else statements to determine the character for each position in the hint.
  • This control structure is crucial for providing the correct hint based on the comparison between the guess and the hidden word.

Classes:

  • The HiddenWord class encapsulates the functionality related to the hidden word and hint generation.
  • It follows the principles of OOP (Object-Oriented Programming) by having a constructor and a method to operate on the object’s state.

Full Implemenation with User Input

import java.util.Scanner;

public class HiddenWordGame {
    private String hiddenWord;

    public HiddenWordGame(String hiddenWord) {
        this.hiddenWord = hiddenWord;
    }

    public String getHint(String guess) {
        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);
            } else if (hiddenWord.indexOf(guessChar) >= 0) {
                hint.append("+");
            } else {
                hint.append("*");
            }
        }

        return hint.toString();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the hidden word: ");
        String hiddenWord = scanner.next().toUpperCase();
        HiddenWordGame puzzle = new HiddenWordGame(hiddenWord);

        while (true) {
            System.out.print("Enter your guess (same length as hidden word): ");
            String guess = scanner.next().toUpperCase();

            if (guess.length() != hiddenWord.length()) {
                System.out.println("Please enter a guess of the same length as the hidden word.");
                continue;
            }

            String hint = puzzle.getHint(guess);
            System.out.println("Hint: " + hint);

            if (hint.equals(hiddenWord)) {
                System.out.println("Congratulations! You guessed the word.");
                break;
            }
        }

        scanner.close();
    }
}
HiddenWordGame.main(null)
Enter the hidden word: Enter your guess (same length as hidden word): Hint: ***+*
Enter your guess (same length as hidden word): Hint: HELL*
Enter your guess (same length as hidden word): Hint: HELLO
Congratulations! You guessed the word.

Explanation

  • The HiddenWordGame class now includes both the getHint method and the main method for user input and testing.
  • The getHint method is used to generate hints based on the comparison between the guess and the hidden word.
  • The main method takes user input for the hidden word and enters a loop to take guesses and display hints.
  • It ensures that the guess is of the same length as the hidden word and continues until the user correctly guesses the hidden word.