Game Free Response Solution

The first free response question, Game, on the 2022 AP Computer Science exam has you working with a pair of classes to simulate a game. The goal of the question appears to be checking that you know how to work with multiple classes, so the logic isn’t all that rough compared to some FRQs.

If you haven’t seen the full problem, be sure to check out the PDF on the College Board website.

Level Class

First you’re given a class called Level that isn’t something you’ve seen before. Many of the FRQs do this and give you something new to work with to check that you understand how to work with classes, and not just how to work with specific classes.

Level is meant to represent a specific level in a game and has two methods that we’re concerned with.

public class Level {
    public boolean goalReached() {
        // Implementation not shown
    }
    public int getPoints() {
        // Implementation not show
    }
}

Both of these methods, goalReached and getPoints, are tagged with // Implementation not shown which means that they’re a bit of a black box and we don’t need to worry about how they work. We only care about what they represent.

goalReached tells us that some goal was reached in that particular level and getPoints tells us how many points the player received for that level.

Game Class

The second class is the Game class. This is the class that you’ll be working with and it has a few methods that you’ll need to implement.

public class Game {
    private Level levelOne;
    private Level levelTwo;
    private Level levelThree;

    public Game() {
        // Implementation not shown
    }

    public void isBonus() {
        // Implementation not shown
    }

    public void play() {
        // Implementation not shown
    }

    public void getScore() {
        // Part A
    }

    public int playManyTimes(int num) {
        // Part B
    }
}

And like most free response questions there may be instance variables, constructors, and other methods that aren’t shown. We’re generally given only what we need to solve the problem.

For this problem, we’ve got 2 methods that we need to implment, getScore and playManyTimes. We’ll talk more about those in a bit. But first, let’s look at the rest of the class.

First, we’ve got 3 instance variables, levelOne, levelTwo, and levelThree. These are all instances of the Level class that we saw earlier and we’ll be working with those. The important thing here is that they’re Level instances, which means that they have all of the methods and properties that we saw in the Level class earlier.

There’s also a no-arg constructor. We don’t know what is on the inside, but since we don’t know we apparently don’t need to worry about anything other than that it exists.

And then there’s a isBonus method tells us if they’re getting bonus points for this game. Again, it’s // Implementation not shown so we don’t need to worry about how it works, just that it it’s there. Very likely that we’ll be calling this method somewhere from our code though, like pretty much any time that there’s a method with // Implementation not shown in it. Same goes for the play method which does one round of the game.

getScore Method

getScore has you calculating the score for the current game, following these rules.

  • If only level 1 is reached, only level 1’s points are counted.
  • If level 1 and level 2 are reached, points from both levels are counted.
  • If level 1, level 2, and level 3 are reached, points from all levels are counted.
  • If the game is a bonus game, points are tripled.

So, what we want to build is a series of nested if statements. The way the problem is written, there’s no reason to check points on level 2 if level 1 wasn’t reached. Same with level 3, it’s dependent on level 2.

public int getScore() {
    int s = 0;
    if (levelOne.goalReached()) {
        s += levelOne.getPoints();
        if (levelTwo.goalReached()) {
            s += levelTwo.getPoints();
            if (levelThree.goalReached()) {
                s += levelThree.getPoints();
            }
        }
    }

    if (isBonus()) {
        s *= 3;
    }

    return s; 
}

First line create an int variable named s to hold the score as we go through and check whether or not the levels were reached. As they are, we add the points to s and then return whatever value s holds at the end of the method.

The nested if statements take care of adding the points from each level, but only if that level and all previous levels were reached.

And last, right before returning the value we multiply s by 3 if the game is a bonus game. This works even if no levels are reached because then we’d be multiplying 0 by 3, which is still 0.

playManyTimes Method

For part B we’re implementing the playManyTimes method which plays the game num times and returns the highest score. This is pretty close to labs you’ve likely done this year where you’ve found the largest value in an array or ArrayList. We’re going to keep track of the highest value we’ve found and compare that to every value after playing the game.

public int playManyTimes(int num) {
    int highestScore = 0;
    for (int i = 0; i < num; i++) {
        play();
        int score = getScore();
        if (score > highestScore) {
            highestScore = score;
        }
    }
    return highestScore;
}

First we create a variable to hold the highest score we’ve found so far. We set it to 0 because we know that the score will always be greater than 0.

Then we loop num times, playing the game and getting the score. If the score is greater than the highest score we’ve found so far, we set the highest score to the current score.

This site contains affiliate links. If you click an affiliate link and make a purchase we may get a small commission. It doesn't affect the price you pay, but it is something we must disclose.