SumOrSameGame is #4 from the from the 2025 AP Computer Science A Free Response problems.

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

Part (a) SumOrSameGame constructor

public SumOrSameGame(int numRows, int numCols)
{
    puzzle = new int[numRows][numCols];

    for(int r = 0; r < puzzle.length; r++)
        for(int c = 0; c < puzzle[0].length; c++)
            puzzle[r][c] = (int) (Math.random() * 9) + 1;
}

Part (b) clearPair method

public boolean clearPair(int row, int col)
{
    for(int r = row; r < puzzle.length; r++)
    {
        for(int c = 0; c < puzzle[0].length; c++)
        {
            if(r != row || c != col)
            {
                if(puzzle[row][col] == puzzle[r][c] ||
                        puzzle[row][col] + puzzle[r][c] == 10)
                {
                    puzzle[row][col] = 0;
                    puzzle[r][c] = 0;
                    return true;
                }
            }
        }
    }

    return false;
}

Java files with test code

SumOrSameGame.java
FourTest.java

See Running JUnit 5 tests.

2025 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 SumOrSameGame