2009 AP CSA FRQ 4: TileGame - Solution

2009 AP CSA FRQ 4: TileGame

Topic: 2D Arrays & Game Logic

Skills: 2D array manipulation, object movement, game state management

Unit: Unit 4 (Data Collections) (2025-2026 AP CSA)

Points: 9 | Time: ~22 minutes

Part (a)

Write the getEmptyLocation method that finds the empty position in the grid.

Try First! Attempt before viewing solution.

Solution

public Location getEmptyLocation()
{
    for (int r = 0; r < board.length; r++)
    {
        for (int c = 0; c < board[0].length; c++)
        {
            if (board[r][c] == 0)
            {
                return new Location(r, c);
            }
        }
    }
    return null;
}

Part (b)

Write the slideDown method that moves a tile down if the space below is empty.

Solution

public boolean slideDown(int row, int col)
{
    if (row + 1 >= board.length || board[row + 1][col] != 0)
    {
        return false;
    }
    
    board[row + 1][col] = board[row][col];
    board[row][col] = 0;
    return true;
}

Key Concepts

✓ Standard row-column 2D array traversal
✓ Bounds checking before accessing adjacent cells
✓ Swap pattern for moving elements
✓ Return early on boundary violations

Common Mistakes

ArrayIndexOutOfBoundsException from not checking boundaries
Not checking if destination is empty
Forgetting to set source to 0 after move
Wrong order of row/column indices
Practice Similar: 2019 FRQ 4 | 2022 FRQ 4 | 2023 FRQ 4

Official Resources

Author: Tanner Crow - AP CS Teacher, 11+ years

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]