CompSci.rocks
FRQcsapa

GameBoard: 2026 FRQ 4

A step-by-step solution to the 2026 AP CSA FRQ 4 (GameBoard), covering summing a row of a 2D array of objects and detecting whether every value in it shares one property in Java.

Scoring a single row of a colorful game board — with a bonus for an all-one-color row — is the challenge in this AP Computer Science A free-response question, and like this year's Array/ArrayList question, it asks for just a single method rather than the usual two-part split.

What This FRQ Tests

  • AP CSA units: Unit 8 (2D Array) and Unit 4 (Iteration)
  • Core skill: looping across one row of a 2D array of objects to both accumulate a total and check a shared property
  • Secondary skill: tracking a running "has everything matched so far" flag across a loop, similar to checking whether a whole row shares one value
  • Official category: "2D Array" — this year's version asks for a single method rather than the usual two-part structure, but it's still always FRQ 4 on the AP CSA exam

The Setup

  • The given Space class (not modified) provides:
    • String getColor() — the space's color
    • int getPoints() — the space's point value
  • GameBoard holds:
    • private Space[][] board
  • You're asked to write one method:
    • int getPointsForRow(int targetRow) — sums the points in a row, doubling the total if every space in that row shares the same color

Writing getPointsForRow(int targetRow)

The Rule, Broken Down

  1. Add up the point value of every space in row targetRow.
  2. Separately, check whether every space in that row has the same color.
  3. If they're all the same color, the row's point value is double the sum.
  4. Otherwise, the row's point value is just the sum, unchanged.

Step-by-Step Approach

  1. Remember the color of the row's very first space — that's what every other space will be compared against.
  2. Start a running point total at 0, and a flag tracking "still all one color" starting at true.
  3. Loop across every column of targetRow.
  4. At each space, add its points to the running total.
  5. Also compare its color to the first space's color — if it ever differs, flip the flag to false.
  6. After the loop, return either double the total or the plain total, depending on the flag.

The Code

public int getPointsForRow(int targetRow)
{
    int sum = 0;
    boolean sameColor = true;
    String firstColor = board[targetRow][0].getColor();

    for (int c = 0; c < board[targetRow].length; c++)
    {
        sum = sum + board[targetRow][c].getPoints();

        if (!board[targetRow][c].getColor().equals(firstColor))
        {
            sameColor = false;
        }
    }

    if (sameColor)
    {
        return sum * 2;
    }
    else
    {
        return sum;
    }
}

Why Each Piece Matters

  • firstColor is read once, before the loop, from column 0 of targetRow — every other space's color gets compared against this fixed reference point, not against whatever the previous space happened to be.
  • sameColor starts true and can only flip to false — this is the standard pattern for "check that nothing in this sequence violates a rule." If the loop never finds a mismatch (including the case of a single-column row, where the loop body runs only once and trivially matches itself), the flag correctly stays true.
  • The sum accumulates on every iteration, regardless of color — summing and color-checking are two independent jobs happening in the same pass over the row, not two separate loops.
  • .equals() for comparing colors, never ==getColor() returns a String, and identical-looking color names aren't guaranteed to be the same object in memory.

Tracing the Example

Using the question's own 4×5 board:

  • Row 0: colors "orange", "red", "blue", "green", "red" — not all the same (the very second space already breaks the match) → sameColor ends false. Points: 100 + 100 + 500 + 500 + 100 = 1300. Since not all one color, the row's value is the plain sum: 1300 — matches the question's expected getPointsForRow(0).
  • Row 2: colors "red", "red", "red", "red", "red" — every space matches firstColor, so sameColor stays true for the whole loop. Points: 200 + 300 + 100 + 200 + 200 = 1000. Since all one color, the row's value is doubled: 1000 * 2 = 2000 — matches the question's expected getPointsForRow(2) exactly.

Common Mistakes to Avoid

  • Comparing each space's color to the previous space's color instead of a single fixed firstColor. This can incorrectly report "all the same" for a row like red, red, blue, blue (each space matches its immediate neighbor, but the row as a whole has two different colors).
  • Computing the sum and the color check in two separate loops instead of one combined pass — not incorrect, just unnecessary extra work when both can be done together.
  • Comparing colors with == instead of .equals(). Two String objects with identical text aren't guaranteed to be the same object in memory.
  • Doubling the sum unconditionally, or forgetting to double it at all — double check that the multiplication only happens inside the sameColor branch.

Key Takeaways

  • "Does every element share some property with the first one" is the same running-flag pattern as "is this sequence in increasing order" — start true, flip to false the moment a mismatch is found, never flip back.
  • Two independent computations over the same data (here, a running sum and a running color check) can usually share one loop instead of needing two separate passes.
  • Not every 2D array FRQ splits into two parts — some, like this one, ask for a single method that combines a couple of smaller ideas (summing, and checking a shared property) into one pass.

Related FRQs