Class: CardsLib::Deck

Inherits:
Object
  • Object
show all
Defined in:
lib/cards_lib/deck.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Deck

Returns a new instance of Deck.



3
4
5
6
7
8
# File 'lib/cards_lib/deck.rb', line 3

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

Instance Method Details

#cardsObject



14
15
16
# File 'lib/cards_lib/deck.rb', line 14

def cards
  @cards
end

#countObject



44
45
46
# File 'lib/cards_lib/deck.rb', line 44

def count
  size
end

#empty?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/cards_lib/deck.rb', line 32

def empty?
  !peak
end

#face_upObject



48
49
50
# File 'lib/cards_lib/deck.rb', line 48

def face_up
  @cards.map(&:face)
end

#inspectObject



10
11
12
# File 'lib/cards_lib/deck.rb', line 10

def inspect
  "<Deck: #{size} Cards - Seed##{@seed}>"
end

#peakObject



18
19
20
# File 'lib/cards_lib/deck.rb', line 18

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

#pluckObject



22
23
24
25
26
# File 'lib/cards_lib/deck.rb', line 22

def pluck
  card = peak
  @top += 1 unless @top == _cards.size
  card || card.tap {|i| i.define_singleton_method(:face) { nil } }
end

#present?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/cards_lib/deck.rb', line 36

def present?
  !empty?
end

#return_cardObject



28
29
30
# File 'lib/cards_lib/deck.rb', line 28

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

#sizeObject



40
41
42
# File 'lib/cards_lib/deck.rb', line 40

def size
  _cards[@top..-1].size
end