Program 4

Card Shuffling Simulation

due Thursday, April 7, 2005 by the start of your lab period

Lab Partners

Everyone is encouraged to work with one lab partner on this assignment. If you do work with a partner, submit one solution with both of your names on it. Also, please look at the class collaboration policy so that you know what is and what isn't allowed.

Introduction

A company named ShuffleMaster specializes in, among other things, making card shufflers that casinos in Las Vegas can use. In this program, we are going to simulate how one of these shufflers might work using software.

Program Inputs

When the program is run, it requires two inputs from a user:

  1. The shuffling mode for the program. The two legal modes are normal and random.
  2. The number of times to perform a shuffling operation. This number should be an integer that is 0 or larger.

The program may assume that the inputs will be legal ones. It is up to you to decide how to acquire the inputs (text input, command line arguments, a GUI, etc.)

Program Operation

Initially a queue containing 52 cards should be constructed. The initial ordering of the queue (from front to back) should be 2 of clubs, 3 of clubs, 4 of clubs, 5 of clubs, 6 of clubs, 7 of clubs, 8 of clubs, 9 of clubs, 10 of clubs, jack of clubs, queen of clubs, king of clubs, ace of clubs, 2 of diamonds ... ace of diamonds, 2 of hearts ... ace of hearts, 2 of spades ... ace of spades.

Each time that a shuffling operation should be performed, the program should perform the following actions.

  1. The 52 cards should be split into half such that the first 26 cards are in queue A and the latter 26 cards are in queue B. Within each queue, the original ordering of the 52 cards should be retained.
  2. The two queues of 26 cards should be merged back to form one queue of 52 cards. If the shuffling mode is normal, the merged queue should alternate taking cards first from queue A and then from queue B, placing them into the final queue. If the shuffling mode is random, a random number between 0.0 and 1.0 should be generated. If the number is <= 0.5, a card from queue A should be used, otherwise a card from queue B should be used. This process should continue until either queue A or queue B is empty. At this point, all of the cards from the remaining queue should be placed at the back of the resulting queue.

Program Output

At the end of the simulation, the program should print out the shuffling mode used (either normal or random), the number of shuffling passes made, and the final ordering of the 52 cards.

Requirements

You must use and implement the following queue interface in your solution:

public interface QueueInterface
{
    public void makeEmpty();
    public boolean empty();
    public void enqueue (Object data);
    public Object dequeue ();
    public Object peek();
}

You are not allowed to use any operations other than these to manipulate your queue.

Grading

What to Submit

E-mail your code to Mike Thiesen at mthiesen@gmail.com in one message before the due date. Zip your relevant BlueJ folder up so that you only need to send Mike one file. The subject of the message should be: CS221-xx program4 your-names. The xx is the number of your lab section. Late programs will not be accepted for credit. If you aren't finished by the deadline, submit whatever you have - partial credit is possible.