Class Deck

java.lang.Object
  |
  +--StackOfCards
        |
        +--Deck

public class Deck
extends StackOfCards

In order to have a card game at all, we must have the Deck class, which you must complete according to the API below. The Deck is an abstract data type, so as with StackOfCards, it can be implemented in many ways. Our implementation of the Deck will extend StackOfCards.

The following conditions are required of any implementation of Deck: When the deck is constructed, the cards in it must be in the following order, from the top of the stack down: Ace of Spades, Ace of Hearts, Ace of Diamonds, Ace of Clubs, King of Spades, Hearts, Diamonds, Clubs, etc. The bottom 4 cards should be Two of Spades, Two of Hearts, Two of Diamonds, Two of Clubs.


Constructor Summary
Deck()
          Creates a non-shuffled 52-card deck
 
Method Summary
 Card getBottom()
          Returns (does not delete) the bottom card in the deck.
 Card getNthFromTop(int n)
          Returns the Nth card from the top of the deck.
 Card getTop()
          Equivalent to peek.
 void perfectShuffle()
          Performs one pass of the Perfect Shuffle algorithm.
 Card pop()
          Pops a card from the top of the deck
 void push(Card c)
          Adds a Card to the top of the deck
 Card removeBottom()
          Removes from the deck and returns the bottom card.
 Card removeNthFromTop(int n)
          Removes from the deck and returns the nth card from the top.
 Card removeTop()
          Removes from the deck and returns the top card.
 void selectionShuffle()
          Performs one pass of the "Selection Shuffle" algorithm.
 
Methods inherited from class StackOfCards
isEmpty, peek, toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Deck

public Deck()
Creates a non-shuffled 52-card deck
Method Detail

push

public void push(Card c)
Adds a Card to the top of the deck
Overrides:
push in class StackOfCards

pop

public Card pop()
Pops a card from the top of the deck
Overrides:
pop in class StackOfCards

getTop

public Card getTop()
Equivalent to peek.

getBottom

public Card getBottom()
Returns (does not delete) the bottom card in the deck.

getNthFromTop

public Card getNthFromTop(int n)
Returns the Nth card from the top of the deck. The card is not removed. The top card is 0 away from the top.

removeTop

public Card removeTop()
Removes from the deck and returns the top card. Equivalent to pop()

removeBottom

public Card removeBottom()
Removes from the deck and returns the bottom card.

removeNthFromTop

public Card removeNthFromTop(int n)
Removes from the deck and returns the nth card from the top. The top card is 0 away from the top.

perfectShuffle

public void perfectShuffle()
Performs one pass of the Perfect Shuffle algorithm. This works as follows: The deck is split into two equal halves, then reconstructed by interleaving the two halves. Specifically, the deck is cut equally into two halves. One card is taken from the first half (the half that used to be on top of the deck) and pushed onto the deck, then one is taken from the second half and pushed on, then one from the first and so on until the deck is fully reconstructed. About twenty passes of this algorithm produce a well-randomized deck. Even more specifically, let's assume the deck holds cards (3, 4, 5, 6, 7, 8). The first half would contain (3, 4, 5) and the second half would contain (6, 7, 8) in that order. Then a 3 would be taken from the first half, and pushed onto the now-empty deck. Then the 6 would be chosen. The final product should be a deck that holds (8, 5, 7, 4, 6, 3). If the number of cards is odd, let the second half contain the extra card.

selectionShuffle

public void selectionShuffle()
Performs one pass of the "Selection Shuffle" algorithm. It worksworks as follows: A random card is chosen and removed from the deck. This card is placed on top of the deck. Another randomly chosen card, but one that has not yet been touched by the algorithm, is removed and placed on top. This process is run until all cards have been touched by the algorithm. The result is a fully randomized deck.

by Sergey Klibanov and Nate Bayless