Sha256: 8e2dec06efaeb19f9cf395ab4c74d604c56d19c86aa30f721fcb77340ff117eb

Contents?: true

Size: 664 Bytes

Versions: 1

Compression:

Stored size: 664 Bytes

Contents

module CardsLib
  class Deck
    def initialize(options = {})
      cards = options.fetch(:cards) { Standard::PLAYING_CARDS }
      @seed = options.fetch(:seed)  { Random.new.seed         }
      @top = 0
      @cards = cards.map {|c| Card.new(c) }.shuffle(random: Random.new(@seed)).to_enum
    end

    def cards
      @cards
    end

    def peak
      Array(@cards.to_a[@top..@top]).first
    end

    def pluck
      card = Array(@cards.to_a[@top..@top]).first
      @top += 1 unless @top == cards.to_a.size
      card || card.tap {|i| i.define_singleton_method(:face) { nil } }
    end

    def return_card
      @top -= 1 unless @top == 0
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cards_lib-0.0.2 lib/cards_lib/deck.rb