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
9
10
11
12
13
# File 'lib/cards_lib/deck.rb', line 3

def initialize(options = {})
  cards = options.fetch(:cards) { Standard::PLAYING_CARDS }
  ranker = options.fetch(:ranker){ Ranker }
  @seed = options.fetch(:seed) { Random.new.seed }
  @top = 0
  @cards = if cards.all? {|c| c.is_a? Card }
             cards
           else
             cards.map {|c| Card.new(c, ranker) }
           end.shuffle(random: Random.new(@seed)).to_enum
end

Instance Method Details

#cardsObject



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

def cards
  @cards
end

#countObject



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

def count
  size
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  !peak
end

#face_upObject



53
54
55
# File 'lib/cards_lib/deck.rb', line 53

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

#inspectObject



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

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

#peakObject



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

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

#pluckObject



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

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)


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

def present?
  !empty?
end

#return_cardObject



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

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

#sizeObject



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

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