Class: CardsLib::Deck
- Inherits:
-
Object
- Object
- CardsLib::Deck
- Defined in:
- lib/cards_lib/deck.rb
Instance Method Summary collapse
- #cards ⇒ Object
- #count ⇒ Object
- #empty? ⇒ Boolean
- #face_up ⇒ Object
-
#initialize(options = {}) ⇒ Deck
constructor
A new instance of Deck.
- #inspect ⇒ Object
- #peak ⇒ Object
- #pluck ⇒ Object
- #present? ⇒ Boolean
- #return_card ⇒ Object
- #size ⇒ Object
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( = {}) cards = .fetch(:cards) { Standard::PLAYING_CARDS } ranker = .fetch(:ranker){ Ranker } @seed = .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
#cards ⇒ Object
19 20 21 |
# File 'lib/cards_lib/deck.rb', line 19 def cards @cards end |
#count ⇒ Object
49 50 51 |
# File 'lib/cards_lib/deck.rb', line 49 def count size end |
#empty? ⇒ Boolean
37 38 39 |
# File 'lib/cards_lib/deck.rb', line 37 def empty? !peak end |
#face_up ⇒ Object
53 54 55 |
# File 'lib/cards_lib/deck.rb', line 53 def face_up @cards.map(&:face) end |
#inspect ⇒ Object
15 16 17 |
# File 'lib/cards_lib/deck.rb', line 15 def inspect "<Deck: #{size} Cards - Seed##{@seed}>" end |
#peak ⇒ Object
23 24 25 |
# File 'lib/cards_lib/deck.rb', line 23 def peak Array(_cards[@top..@top]).first end |
#pluck ⇒ Object
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
41 42 43 |
# File 'lib/cards_lib/deck.rb', line 41 def present? !empty? end |
#return_card ⇒ Object
33 34 35 |
# File 'lib/cards_lib/deck.rb', line 33 def return_card @top -= 1 unless @top == 0 end |
#size ⇒ Object
45 46 47 |
# File 'lib/cards_lib/deck.rb', line 45 def size _cards[@top..-1].size end |