For third assignment, you are to write a program that plays a simple card trick, the "21 Card Trick." For details on the trick itself, please see Lance Burton's web page. (I use columns instead of rows, because that's how I learned the trick.)
Your program must be able to represent a full deck of fifty-two (52) playing cards, the 7-card columns needed for the trick itself, and each playing card. The deck may be represented by a vector, as the program has to be able to shuffle (randomize) the deck, and pick out one of the cards from the middle of the deck at the end of the trick. However, a column must be represented by either a stack, queue, or dequeue ADT (you decide which is correct), and the only operations done on a column should be the operations of the appropriate ADT. You may use the code from class, the book, or an appropriate Java API for the column code. Below is a sample run of the program. Note that the columns are printed out as lines, for ease of printing.
java Trick 1: [ Q Spades, 5 Spades, 2 Diamonds, 5 Hearts, 3 Clubs, 4 Hearts, J Clubs] 2: [ A Spades, K Spades, J Hearts, 8 Spades, 2 Spades, 6 Spades, K Clubs] 3: [ 4 Diamonds, 10 Diamonds, 5 Diamonds, 4 Clubs, 8 Hearts, 9 Hearts, 8 Clubs] Which column is your card in? 1 1: [ 4 Diamonds, 4 Clubs, 8 Clubs, 2 Diamonds, 4 Hearts, K Spades, 2 Spades] 2: [10 Diamonds, 8 Hearts, Q Spades, 5 Hearts, J Clubs, J Hearts, 6 Spades] 3: [ 5 Diamonds, 9 Hearts, 5 Spades, 3 Clubs, A Spades, 8 Spades, K Clubs] Which column is your card in? 2 1: [ 5 Diamonds, 3 Clubs, K Clubs, Q Spades, J Hearts, 4 Clubs, 4 Hearts] 2: [ 9 Hearts, A Spades, 10 Diamonds, 5 Hearts, 6 Spades, 8 Clubs, K Spades] 3: [ 5 Spades, 8 Spades, 8 Hearts, J Clubs, 4 Diamonds, 2 Diamonds, 2 Spades] Which column is your card in? 1 Your card is: Q Spades
One hint: To shuffle the deck, just swap each card with a randomly chosen card that comes at or before that card in the deck. e.g., swap card i with a randomly chosen card from the range 0 through i. You might want to use the Math.random() method.
If you have questions or comments, email me at simon@mathcs.duq.edu