Skip to main content

AP CSA FRQ Guide

2007 FRQ 4: RandomPlayer and GameDriver

Write a player that randomly selects a valid move, then drive a complete game by repeatedly querying the current player, applying its move, and reporting the winner or a draw.

Legacy Inheritance and Game Simulation22 minute targetinheritanceinterfacesArrayListMath.randomsimulationlegacy

Skills Tested

What this FRQ is really practicing

inheritanceinterfacesArrayListMath.randomsimulationlegacy

Review these topics before attempting the problem.

Starter Approach

How to begin with your own plan

  1. 1Part A: extend Player, initialize the player name through the superclass constructor, and override getNextMove.
  2. 2Choose a random element from the valid-move list; return the exact string "no move" when the list is empty.
  3. 3Part B: print the initial game state, then repeatedly get the current player and next move, print the player name and move, and pass that move to makeMove.
  4. 4After the game ends, print the winner name followed by "wins", or "Game ends in a draw" when getWinner returns null.

Attempt the problem before opening the solution.

Common Mistakes

Mistakes to watch for while writing

Casting Math.random() before multiplying by the number of valid moves.
Indexing an empty move list or skipping the required no-move description.
Keeping the same current player for every turn or dereferencing a null winner.

Self Check

Review questions before you submit

Initializes the inherited Player state.

Pass the provided name to the superclass constructor.

Uses the valid-move list when selecting a random move.

Generate an index from 0 through the list size minus one.

Provides the required no-move result.

An empty valid-move list must not be indexed.

Uses the game-state operations to run and finish the game.

Query the current player on each turn and check the winner only after game over.

Practice Links

Where to go next