Token Pass Solution

The Token Pass free response on the 2013 AP computer science exam has you working with an array of int values to play a game.

This one, probably more than most, you probably should head over to the College Board website and view the PDF for this problem. When we do this FRQ in class most of my students don’t have problems figuring out the code, once they’ve figured out what it’s asking. So, go ahead and follow that link, read the problem description, and come back. We’ll still be here.

You are given a shell of the class with methods to implement. It looks like this.

public class TokenPass {
	private int[] board;
	private int currentPlayer;
	public TokenPass(int playerCount) {
		// Part A
	public void distributeCurrentPlayerTokens() {
		// Part B

Part A

First you are to implement the constructor for TokenPass. You’re given a number of players. board needs to be instantiated and sized to that size. And then you fill in the array with random numbers from 1 to 10, inclusive.

Mine looked like this.

public TokenPass( int playerCount ) {
    /* To be completed in Part A */
    board = new int[playerCount];
    for ( int i=0; i<playerCount; i++) {
        board[i] = 1 + (int)(10 * Math.random()); 
    currentPlayer = (int)(playerCount * Math.random()); 

The first line sizes board to the right size.

Next, the code loops through all of the elements in board and fill it with a random number from 1 to 10.

And last we set currentPlayer to a random player.

Part B

This is the one where students got confused. And it wasn’t the problem. They tend to have trouble understanding what the problem is asking.

What you’re supposed to do is pick up all the tokens at the current player’s position. That is, you set that position to zero and store that number in a variable.

Then, starting at the next position, you leave one of the picked up tokens in each position. You decrement your variable and increment the value at that position. When you get to the end of the array you loop around.

When you’re done, the current player is the last position you dropped a token.

Here’s a working solution for part B.

public void distributeCurrentPlayerTokens() {
    int nextPlayer = currentPlayer;
    int numToDistribute = board[currentPlayer];
    board[currentPlayer] = 0;
    while (numToDistribute > 0) {
        nextPlayer = (nextPlayer + 1) % board.length;

The first line stores the value in currentPlayer in another variable nextPlayer so that I can change it.

Lines 2 and 3 pick up the tokens from the current player.

Then, the code loops as long as there are tokens to hand out.

I used % as a way to stay within the index boundaries of board so that I didn’t have to worry about using if / else statements. Most students use if / else, and that’s fine.

One thing that caught me a little off guard is that currentPlayer doesn’t change as part of this method. It seems like it should. But it’s in the postconditions that it shouldn’t. Newsletter

Want to stay in touch and keep up to date with the latest posts @

Leave a Reply

Your email address will not be published. Required fields are marked *