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 |
# File 'lib/cards_lib/deck.rb', line 3 def initialize( = {}) cards = .fetch(:cards) { Standard::PLAYING_CARDS } @seed = .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
#cards ⇒ Object
14 15 16 |
# File 'lib/cards_lib/deck.rb', line 14 def cards @cards end |
#count ⇒ Object
44 45 46 |
# File 'lib/cards_lib/deck.rb', line 44 def count size end |
#empty? ⇒ Boolean
32 33 34 |
# File 'lib/cards_lib/deck.rb', line 32 def empty? !peak end |
#face_up ⇒ Object
48 49 50 |
# File 'lib/cards_lib/deck.rb', line 48 def face_up @cards.map(&:face) end |
#inspect ⇒ Object
10 11 12 |
# File 'lib/cards_lib/deck.rb', line 10 def inspect "<Deck: #{size} Cards - Seed##{@seed}>" end |
#peak ⇒ Object
18 19 20 |
# File 'lib/cards_lib/deck.rb', line 18 def peak Array(_cards[@top..@top]).first end |
#pluck ⇒ Object
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
36 37 38 |
# File 'lib/cards_lib/deck.rb', line 36 def present? !empty? end |
#return_card ⇒ Object
28 29 30 |
# File 'lib/cards_lib/deck.rb', line 28 def return_card @top -= 1 unless @top == 0 end |
#size ⇒ Object
40 41 42 |
# File 'lib/cards_lib/deck.rb', line 40 def size _cards[@top..-1].size end |