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)
toString()
getRank()
getSuit()
getCardImage()
Then, write a class named Deck
with the following constructor and methods:
Deck()
shuffle(int numberOfCardPulls)
shuffle()
dealTopCard()
toString()
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:
ArrayList
class (in java.util
) -- especially with regard to the methods add(E e)
, get(int index)
, and remove(int index)
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
)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.)this.getGCanvas().setBackground(new Color(71,113,72));