GameBoard is #4 from the from the 2026 AP Computer Science A Free Response problems.

https://apcentral.collegeboard.org/media/pdf/ap26-frq-computer-science-a.pdf

getPointsForRow method

public int getPointsForRow(int targetRow)
{
    int points = 0;
    boolean allSameColor = true;

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

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

    if(allSameColor)
        points *= 2;

    return points;
}

Traversing a row requires holding the row index constant and varying the column index. (Traversing a column requires holding the column index constant and varying the row index.) See 2D array exercises and MatrixManipulator for additional practice.

Java files with test code

Space.java
GameBoard.java
FourTest.java

See Running JUnit 5 tests.

2026 AP CS Exam Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

See an error? Question? Please comment below.

Comment on GameBoard