Options
All
  • Public
  • Public/Protected
  • All
Menu

Class CardDeck

A CardDeck simulates a deck of cards.

Hierarchy

  • CardDeck

Index

Constructors

constructor

  • Creates a new, empty deck.

    Returns CardDeck

Methods

discard

  • discard(card: any, copies: number): void
  • Adds a card to the bottom of the deck. It is not required that the card have previously been drawn from the deck. If copies is defined, then that many copies are added, all identical.

    Parameters

    • card: any

      the card to add to the deck

    • copies: number

      the optional number of copies to add to the deck (default is 1)

    Returns void

draw

  • draw(): any
  • Removes and returns the first card from the deck. To return the card to the bottom of the deck, discard it. If the deck is empty, returns null.

    Returns any

    the top card, which is removed from the deck, or null if the deck is empty

drawFirst

  • drawFirst(matchFunction: function): any
  • Searches the deck for the first card matched by the specified function. If found, it removes it from the deck and returns it. If no matching card is found, returns null.

    Parameters

    • matchFunction: function

      a function that takes a card and returns true if it is the type of card that should be drawn

        • (any: any): boolean
        • Parameters

          • any: any

          Returns boolean

    Returns any

    the first matching card, which is removed from the deck

peek

  • peek(index: number): any
  • Returns a card from the deck without removing it. If index is not defined, then the card at the top of the deck is returned. Otherwise, if index is 0 or positive, then the card at position index is returned, where a position of 0 is the top of the deck, 1 is the next card from the top, and so on. If index is negative, then the cards are counted from the bottom of the deck: -1 is the last card, -2 is the penultimate card, and so on.

    throws

    if the index is invalid

    Parameters

    • index: number

      the position of the card to peek at within the deck

    Returns any

    the card at the index, or the card at index size() + index if the index is negative

pull

  • pull(index: number): any
  • Pulls a card from the deck from any position. The card at the requested position is removed from the deck and returned. As with peek, a negative index may be used to count from the bottom of the deck. If called without an index argument, this method is equivalent to draw.

    throws

    if the index is invalid

    Parameters

    • index: number

      the optional position of the card to remove from the deck

    Returns any

    the removed card

shuffle

  • shuffle(): void
  • Shuffles the cards in a deck into a random order.

    Returns void

size

  • size(): number
  • Returns the number of cards currently in this deck.

    Returns number

toString

  • toString(): string
  • Returns a string consisting of a comma separated list of the cards in this card deck.

    Returns string

Static createFromArray

  • Creates a new deck, using items from an array to populate it.

    Parameters

    • arrayLikeObject: ArrayLike<any>

      an array or array-like object to copy cards from

    Returns CardDeck

    a card deck consisting of objects copied from the argument