What It Is

2015 FRQ Class Question 2: Guessing Game

How it Works:

  • Create a guessing game where they try to guess a hidden word.
  • The hidden word contains only capital letters and has a lenght 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 given a hint that is based on a comparison between the hidden word and the guess.
  • Each position in the hint contians a character that corresponds to the letter in the same position in the guess. The following rules determine the character that appears in the hint.

What they Want you to Do:

  • Write the Complete HiddenWord Class
  • Include any neccessary instance variables, constructor, and the method(getHint)

Easy Way to Lose Points

1 point penalties:

  • Array/ArrayList Confusion
  • Extranneous Code that cuases side-effect
  • Local Vaiables that are used but not declared
  • Destruction of persistent data
  • Void Method or constructor that returns value

What they are looking for:

  • Uses Correct Class, constructor, and mehtod headers
  • Declares appropriate private instance variable
  • Initializes instance variable within constructor using paramter
  • Implement getHint
  • Access all letters in both guess and hidden word loop
  • Process letters within loop:
    • Extracts and compares corresponding single letters from guess and hidden word
    • Tests whether guess hidden letter is in same position in both guess
    • Tests whether guess letter occurs in hidden word but not in same position as guess
    • Adds correct character exactly once to the hint string based on the test result
  • Declares, initializes, and returns constructed hint string
public class HiddenWord {
// Defining the Class
private String secretWord;
// Private String variable
public HiddenWord(String word) {
    // Defining Class Constructor
    // Taking String parameter callled word
    // When an instance of HiddenWord is created, this constructor is called to initialize 
    // the secretWord with the provided word.
    secretWord = word;
}

public String takeHint(String guess) {
    // Creating a method "takeHint"
    // This is a method that takes a String parameter called guess 
    // and returns a String. It provides a hint based on the user's guess.
    StringBuilder hint = new StringBuilder();
    // This line creates a StringBuilder named hint. 
    // A StringBuilder is a more efficient way to concatenate strings, especially in a loop.
    for (int i = 0; i < guess.length(); i++) {
        // This loop iterates over each character in the guess string.
        char guessChar = guess.charAt(i);
        char secretChar = secretWord.charAt(i);
        //These lines retrieve the characters at the current position in both 
        // the guess and secretWord strings.
        if (guessChar == secretChar)
            hint.append(guessChar);
        else if (secretWord.indexOf(guessChar) != -1)
            hint.append("+");
        else
            hint.append("*");
    }
    // This checks if the character in guess matches the character in 
    // secretWord at the same position. If true, it appends the character to the hint.

    return hint.toString();
}
}

Class Definition:

public class HiddenWord { … }: This defines a class named HiddenWord. Instance Variable:

private String secretWord;: This declares a private instance variable secretWord of type String. This variable will store the hidden word that the player needs to guess. Constructor:

public HiddenWord(String word) { … }: This is the class constructor. It takes a String parameter called word. When an instance of HiddenWord is created, this constructor is called to initialize the secretWord with the provided word. Method - takeHint:

public String takeHint(String guess) { … }: This is a method that takes a String parameter called guess and returns a String. It provides a hint based on the user’s guess. Creating a StringBuilder:

StringBuilder hint = new StringBuilder();: This line creates a StringBuilder named hint. A StringBuilder is a more efficient way to concatenate strings, especially in a loop. Loop through the Guess:

for (int i = 0; i < guess.length(); i++) { … }: This loop iterates over each character in the guess string. Accessing Characters:

char guessChar = guess.charAt(i); and char secretChar = secretWord.charAt(i);: These lines retrieve the characters at the current position in both the guess and secretWord strings. Checking the Guess:

if (guessChar == secretChar) { … }: This checks if the character in guess matches the character in secretWord at the same position. If true, it appends the character to the hint.

`else if (secretWord.indexOf(guessChar) != -1) { … }: This checks if the