Shuffling Cards

When playing just about any kind of card game, one has to shuffle the deck. In the real world -- if you have good manual dexterity and have practiced a bit -- you might use the fancy riffle shuffle to accomplish this task. In the realm of computer programming, however, a much simpler technique can be emulated: Starting with a deck whose cards are in order by suit first, and then rank, randomly select a card and move it to the bottom of the deck. Doing this just once doesn't do much as far as shuffling the deck -- but what if we did this 1000 times? The result would certainly be just as good as the ole' riffle shuffle any day of the week!

Write a collection of classes that will both model and allow us to visualize this method of shuffling as well as draw cards from the top of the deck, as seen in the following video:

 

First, write a class named Card with the following constructor and methods:

Card(int rank, int suit)
a constructor, where rank and sum agree with their counterparts in the GCardImage class
toString()
a method which returns a string of the form "Ace of Spades", "7 of Hearts", "Queen of Clubs", etc…
getRank()
a method which returns the rank of the card in question as an integer in a manner consistent with GCardImage
getSuit()
a method which returns the suit of the card in question as an integer in a manner consistent with GCardImage
getCardImage()
a method which returns the GCardImage for this card with an appropriate rank and suit shown on the image. Note the GCardImage for a given card should be unique. That is to say, repeated calls to getCardImage() should return the SAME GCardImage object.

Then, write a class named Deck with the following constructor and methods:

Deck()
a constructor, that produces a deck whose cards are in the following orders by suit (Spades, Hearts, Clubs, Diamonds) and then by rank (A,2,3,4,5,6,7,8,9,10,J,Q,K). Thus, all of the cards in any one suit are grouped together.
shuffle(int numberOfCardPulls)
provided there are 2 or more cards in the deck, this method shuffles the deck by repeatedly randomly pulling a card and putting it on the bottom of the deck (as indicated by numberOfCardPulls)
shuffle()
a method which shuffles the deck as in the other method shuffle, but where numberOfCardPulls is 1000
dealTopCard()
a method with removes the top card from the deck and returns a reference to that removed card
toString()
a method which returns a string giving the contents of the entire deck with appropriate return characters, spaces, and tabs contained in the string so that when it is printed, it prints as a table of 4 columns with the top card being the first card printed, and with a row of asterisks on a separate line at the bottom.

Finally, write a class named Shuffler that displays the cards of the deck face up in the central graphics area of a TwoButtonBreadboard.

When the left button labeled "Shuffle" is clicked, the text in the input text field is examined. If there is a positive integer present, then the deck shown should be shuffled by using this number of random "pulls" (as described previously). If no such number is present, the deck shown should be shuffled using 1000 such "pulls" by default. If there are no cards left in the deck, the text area should instead display "Sorry, there is nothing to shuffle." and nothing else should happen.

When the right button labeled "Draw" is clicked, the top card should be removed from the deck shown, and the text area should update to say which card was drawn. (e.g. "The Ace of Spades was just drawn.") If there are no cards left in the deck, the text area should instead display "Sorry, there are no more cards in the deck.", and nothing else should happen.

Additionally, whenever the deck is altered (via shuffling or drawing the first card), the deck contents should be printed to the console window.

As you attempt to do all of the above, you will probably find it useful to familiarize yourself with the following:

  • The ArrayList class (in java.util) -- especially with regard to the methods add(E e), get(int index), and remove(int index)
  • The GCardImage class (in com.oxfordmathcenter.breadboards.jar) -- especially with regard to its constructors, and the setLocation() method of its super-class GImage (in acm.jar)
  • The GraphicsProgram class (in acm.jar) -- especially with regard to the methods add(), remove(), and removeAll(). (Note: when you call this.add(), you are using the add() method of this class.)
  • The way in which the central canvas area's background can be given a "poker-felt" green with the following statement:
    this.getGCanvas().setBackground(new Color(71,113,72));