APCS Free Response 2002 AB2 Java Version

Consider the following class declaration for representing cards to be used in a program that simulates a card game.
  public class Card implements Comparable
  {
      // other methods and data fields not shown
    
    /**
     * Return a value according to whether
     * this Card is greater, equal to, or less than
     * the card passed in as parameter o. The value
     * returned is consistent with the specifications
     * of the Comparable interface's compareTo method.
     */

      public int compareTo(Object o)
      {
	  // implementation not shown
      }
  }

The card game to be simulated is a two-player game called "Compete" that is played with cards. Cards can be compared to see which card is greater in value, or if the cards are equal. The object of the game is to win all of the cards.

Each player has a pile of cards and both piles start with the same number of cards. Play consists of a sequence of "rounds". Play continues until at the end of a round at least one player is out of cards. A discard pile is created and used during the rounds and is empty at the beginning of each round.

Part A

You will write code to move cards from one pile to another and to play one round. Each pile of cards will be represented by a queue.

A round is played by executing the following steps until the round ends.

  1. If exactly one player's pile is empty, the other player adds all the cards in the discard pile to the bottom of his or her pile without changing the order. The round ends.

  2. If both players' piles are empty at the same time, both players' piles remain empty. The round ends.

  3. Each player turns over the top card from his/her pile.

  4. If the cards are equal in value, both cards are added to the discard pile in either order and play returns to step 1.

  5. If the cards are not equal in value, the player whose card has the greater value adds any cards in the discard pile to the bottom of his/her pile without changing the order of the cards in the discard pile. He/she then adds both cards just played to the bottom of the his/her pile in either order. The round ends.

The class Compete is used to simulate the card game. Write private method appendQueue, which is described as follows. Method appendQueue should remove all the cards from parameter source and add them to parameter destination in the same order.

Complete method appendQueue below.


    /**
     * Remove all objects from source and put them
     * in destination in the order in which they're removed.
     */
    private void appendQueue(Queue destination, Queue source)
    {








    }

Part B

You will write the method oneRound. Method oneRound takes two players' piles of cards as parameters and carries out one round of the game. For your convenience the steps for one round are repeated here.

  1. If exactly one player's pile is empty, the other player adds all the cards in the discard pile to the bottom of his or her pile without changing the order. The round ends.

  2. If both players' piles are empty at the same time, both players' piles remain empty. The round ends.

  3. Each player turns over the top card from his/her pile.

  4. If the cards are equal in value, both cards are added to the discard pile in either order and play returns to step 1.

  5. If the cards are not equal in value, the player whose card has the greater value adds any cards in the discard pile to the bottom of his/her pile without changing the order of the cards in the discard pile. He/she then adds both cards just played to the bottom of the his/her pile in either order. The round ends.

The constructor and data field myDiscards for the class Compete are shown below. In writing oneRound you should use myDiscards as the discard pile. You may call method appendQueue from part (a). Assume appendQueue works as specified, regardless of what you wrote in part (a). Assume that the class Card correctly implements the Comparable interface for comparing cards to see whether one card is less than, equal to, or greater than another card.

public class Compete
{
    private Queue myDiscards;

    /**
     * Construct with empty discard pile
     */
    
    public Compete()
    {
	// implementation not shown
    }

    /**
     * Remove all objects from source and put them
     * in destination in the order in which they're removed.
     */
    private void appendQueue(Queue destination, Queue source)
    {
        // implementation not shown
    }
    
    /**
     * precondition: myDiscards is empty
     * Simulate one round of Compete.
     */

    public void oneRound(Queue pile1, Queue pile2)
    {
        // write this method





    }

Owen L. Astrachan
Last modified: Thu Jul 11 13:37:09 EDT 2002