Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Fall 2014
Project #3
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

Due by the start of class on Friday, 10/17/2014

A standard playing card deck contains 52 cards, 13 values in each of four suits. The values are named 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace. The suits are named Clubs, Diamonds, Hearts, Spades. A particular card in the deck can be uniquely identified by its value and suit, typically denoted value of suit. For example, "9 of Hearts" or "King of Spades". Traditionally a new deck is ordered first alphabetically by suit, then by value in the order given above.

The Big City has many Casinos. In one such casino the dealer is a bit crooked. She has perfected several shuffles; each shuffle rearranges the cards in exactly the same way whenever it is used. A very simple example is the "bottom card" shuffle which removes the bottom card and places it at the top. By using various combinations of these known shuffles, the crooked dealer can arrange to stack the cards in just about any particular order.

You have been retained by the security manager to track this dealer. You are given a list of all the shuffles performed by the dealer, along with visual cues that allow you to determine which shuffle she uses at any particular time. Your job is to write a program that will predict the order of the cards after a sequence of shuffles.

### Input Format

Input consists of an integer n ≤ 100, the number of shuffles that the dealer knows. Following this line are n lines, each one with 52 integers, defining the "shuffles". Each shuffle will list all the integers from 1 to 52 in some order. Within each set of 52 integers, i in position j means that the shuffle moves the ith card in the deck to position j.

Following these is a line with a single integer, m, indicating the number of shuffles to be performed.

After this, m lines follow; each containing an integer k between 1 and n indicating that you have observed the dealer applying the kth shuffle given in the input.

### Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

Assume the dealer starts with a new deck ordered as described above. After all the shuffles had been performed, give the names of the cards in the deck, in the new order.

#### Sample Input

For readability, the two "shuffle definition" lines have been broken into a total of four lines (the first two lines make up the first line of real input, while the third and fourth comprise the second line in the input file):

```2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
2
1
2
```

#### Output for Sample Input

Be sure you understand why this output must result from the input above!

```King of Spades
2 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
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
3 of Clubs
```

### Handling user interaction

You will run this program by using I/O redirection to pipe the contents of an input file to the program. Output will simply be printed to the screen, rather than written to a file:

```John-Lasseter:~ jlasseter\$  java Shuffles  < shufs.dat
2 of Clubs
4 of Clubs

... (deleted for brevity) ...
```

# Turn In

The source code for your program (your choice of name), in a subfolder of your turn-in directory called "hw3". Please also provide a paper copy of your work.

### Formatting Requirements

See the Style Guide, available from the General Notes section of our course web site. Beginning with this assignment, all elements of beautiful, clear code style described there will be expected.

### General

• Your code must be syntactically and semantically correct, which means that it has to compile successfully. Any program that cannot be successfully compiled with javac will receive no credit.

• Naturally, your code must also be behaviorally correct, which means that it should print the right output in the right format. However, partial credit will be given for partial solutions. At a minimum, be sure to test on the sample input, above

### Assessment

I will test your code using, at a minimum, the sample input given above. I may—and probably will—test it with other inputs (always in the correct, agreed-upon format above). Input will be given to standard input, so you'll need to read it with a Scanner. Having solved Problem 1 of Lab #6, you already know how to do this.

#### credits

This problem is adapted from one that appeared in a qualifying round of the ACM Intercollegiate Programming Competition. I have lost the attribution for the original (more complicated) version.

John H. E. Lasseter