Home 2015 FRQ Question 2
Post
Cancel

2015 FRQ Question 2

With this FRQ, I realized that this could be made into a game. While I did complete the required tasks, I also added some features so that it could become a playable game, such as a random word generator from RapidAPI and also recurrence so that the user can continually play it until they guess the word. The responses are recorded in order for the user to see their progress towards the correct guess. I decided to make this game by first completing the original task, where I had to display a hint based on the words that were given vs the actual answer. I did this in the getHint method, where I check if the letters in the word match with the letters of the guessed word. There are three conditions, one where the placement is correct, one where the placement is off but the letter exists, and one where neither the placement or the letter is correct. I do this by iterating through each letter in the guessed word with a for loop and checking them against the actual word. I also make a containChar method which is used to check if the character is actually contained in the word at any place. I also added a fetch method, which uses code from RapidAPI and a transform method, which changes the JSON from the API into a string that can be used in the game.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.util.Scanner;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

class HiddenWord {
    private String randWord; // creating private variable for the random word to be fetched

    public HiddenWord(String solution) {
        this.randWord = solution; // setting this word uniquely to the instance
    }

    public static String fetchRandomWord() throws Exception { // fetching the random words using rapidAPI
        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://random-word-api.p.rapidapi.com/get_word"))
            .header("X-RapidAPI-Key", "701410bf7emshbaf1fa99b2e4e5bp1c0ee6jsn8f8f51602e3f")
            .header("X-RapidAPI-Host", "random-word-api.p.rapidapi.com")
            .method("GET", HttpRequest.BodyPublishers.noBody())
            .build();
        HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
        return response.body(); // returning JSON response
    }

    public static String transformRandomWord() throws Exception { // formatting the word from json into a string
        String jsonString = fetchRandomWord(); // fetching the random word
        int index = jsonString.indexOf("\"word\"") + "\"word\"".length() + 2; // removing the "word" portion of the JSON
        int endIndex = jsonString.indexOf("\"", index); // removing everything after the actual word in the JSON
        String solution = jsonString.substring(index, endIndex); // setting the word as the variable for the specific word in the instance
        return solution;
    }

    public boolean containChar(char letter) { // checking if the letter guessed is contained in the word
        for (int j = 0; j < this.randWord.length(); j++) { // looking through the random word that was pulled from the API
            if (letter == this.randWord.charAt(j)) { // comparing the letter from the guessed word to all the other words in the random word
                return true; // returns true of the letter exists
            }
        }
        return false;
    }

    public String getHint(String guess) { // method for getting the hint based on the word guessed
        while (guess.length() < randWord.length()) { // adds in fillers if the word guessed is less than the actual word length
            guess += "*";
        }
        
        String hint = ""; // sets what the hint will be
        for (int i = 0; i < randWord.length(); i++) { // looking through the random word one letter at a time
            char guessChar = guess.charAt(i); // matching up the index of the letters for both the guessed and solution words
            if (guessChar == randWord.charAt(i)) { // checks to see if they equal
                hint += guessChar; // if they do, the letter is set
            } else if (randWord.indexOf(guessChar) != -1) { // check to see if any letters match
                hint += "+"; // if theyre not in the right place, this symbol is placed
            } else {
                hint += "*"; // if no letters match
            }
        }
        return hint; // returns the hint
    }

    public static void main(String[] args) {
        try { // user for error handling
            String solution = transformRandomWord(); // the solution is generated
            HiddenWord game = new HiddenWord(solution); // creates a game instance
            Scanner scanner = new Scanner(System.in); // starts a scanner instance for user input
            int attempts = 0; // counting the attempts
            String initialHint = "*".repeat(solution.length()); // starts with the initial hint to show the length of the word
            System.out.println("Guess the word: " + initialHint); // asks for a guess
            
            while (true) { // loops forever
                attempts++; // adds in attempts
                System.out.println("Enter your guess: "); // asks for a guess
                String guess = scanner.nextLine();
                if (guess.equals(solution)) { // if the word is guessed
                    System.out.println("You guessed '" + solution + "' correctly in " + attempts + " attempts.");
                    break; // stops the code from continuing
                } else { // continues game
                    String hint = game.getHint(guess); // prints the guess and hint
                    System.out.println("You guessed: " + guess);
                    System.out.println("Hint: " + hint);
                }
            }
            
            scanner.close();
        } catch (Exception e) { // use for error handling
            e.printStackTrace();
        }
    }
}

HiddenWord.main(null);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{"word":"attitudinising"}

Guess the word: **************
Enter your guess: 
You guessed: daniel
Hint: +++i**********
Enter your guess: 
You guessed: atitude
Hint: at++++********
Enter your guess: 
You guessed: atitude
Hint: at++++********
Enter your guess: 
You guessed: atitudes
Hint: at++++*+******
Enter your guess: 
You guessed: attitudinsing
Hint: attitudin++++*
Enter your guess: 
You guessed 'attitudinising' correctly in 6 attempts.
This post is licensed under CC BY 4.0 by the author.

2015 FRQ Question 1

2015 FRQ Question 3