Class Deck


  • public class Deck
    extends java.lang.Object
    An object of type Deck represents a deck of playing cards. The deck is a regular poker deck that contains 52 regular cards and that can also optionally include two Jokers.
    • Constructor Summary

      Constructors 
      Constructor Description
      Deck()
      Constructs a regular 52-card poker deck.
      Deck​(boolean includeJokers)
      Constructs a poker deck of plaing cards, The deck contains the usual 52 cards and can optionally contain two Jokers in addition, for a total of 54 cards.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int cardsLeft()
      As cards are dealt from the deck, the number of cards left decreases.
      Card dealCard()
      Removes the next card from the deck and return it.
      boolean hasJokers()
      Test whether the deck contains Jokers.
      void shuffle()
      Put all the used cards back into the deck (if any), and shuffle the deck into a random order.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Deck

        public Deck()
        Constructs a regular 52-card poker deck. Initially, the cards are in a sorted order. The shuffle() method can be called to randomize the order. (Note that "new Deck()" is equivalent to "new Deck(false)".)
      • Deck

        public Deck​(boolean includeJokers)
        Constructs a poker deck of plaing cards, The deck contains the usual 52 cards and can optionally contain two Jokers in addition, for a total of 54 cards. Initially the cards are in a sorted order. The shuffle() method can be called to randomize the order.
        Parameters:
        includeJokers - if true, two Jokers are included in the deck; if false, there are no Jokers in the deck.
    • Method Detail

      • shuffle

        public void shuffle()
        Put all the used cards back into the deck (if any), and shuffle the deck into a random order.
      • cardsLeft

        public int cardsLeft()
        As cards are dealt from the deck, the number of cards left decreases. This function returns the number of cards that are still left in the deck. The return value would be 52 or 54 (depending on whether the deck includes Jokers) when the deck is first created or after the deck has been shuffled. It decreases by 1 each time the dealCard() method is called.
      • dealCard

        public Card dealCard()
        Removes the next card from the deck and return it. It is illegal to call this method if there are no more cards in the deck. You can check the number of cards remaining by calling the cardsLeft() function.
        Returns:
        the card which is removed from the deck.
        Throws:
        java.lang.IllegalStateException - if there are no cards left in the deck
      • hasJokers

        public boolean hasJokers()
        Test whether the deck contains Jokers.
        Returns:
        true, if this is a 54-card deck containing two jokers, or false if this is a 52 card deck that contains no jokers.