2007 AP CSA FRQ 4: GameState
Topic: Inheritance & Game Simulation
Skills Tested: Subclass creation, polymorphism, game loop implementation, ArrayList with random
Curriculum Alignment: Unit 3 (Class Creation) (2025-2026 AP CSA)
Points: 9 | Time Estimate: ~22 minutes
Part (a)
Write the complete RandomPlayer class that extends Player and selects moves randomly.
Solution
public class RandomPlayer extends Player
{
public RandomPlayer(String name)
{
super(name);
}
public String getNextMove(GameState state)
{
ArrayList moves = state.getCurrentMoves();
if (moves.size() == 0)
return "no move";
int index = (int) (Math.random() * moves.size());
return moves.get(index);
}
}
Part (b)
Write the play method that runs the game loop until completion, printing moves and announcing the winner.
Solution
public void play()
{
System.out.println(state);
while (!state.isGameOver())
{
Player player = state.getCurrentPlayer();
String move = player.getNextMove(state);
System.out.println(player.getName() + " " + move);
state.makeMove(move);
}
Player winner = state.getWinner();
if (winner != null)
System.out.println(winner.getName() + " wins");
else
System.out.println("Game ends in a draw");
}
Key Concepts
Common Mistakes to Avoid
Official College Board Resources
About the Author: Tanner Crow is a certified AP Computer Science teacher with 11+ years of experience.
Last updated: January 2026