Class: Deck
- Inherits:
-
Object
- Object
- Deck
- Defined in:
- lib/cardtrick/deck.rb
Instance Method Summary collapse
- #build! ⇒ Object
- #burn ⇒ Object
- #create! ⇒ Object
- #deck ⇒ Object
-
#initialize(h = {}) ⇒ Deck
constructor
A new instance of Deck.
- #left ⇒ Object
- #map(&b) ⇒ Object
- #shuffle! ⇒ Object
Constructor Details
#initialize(h = {}) ⇒ Deck
Returns a new instance of Deck.
2 3 4 5 6 7 |
# File 'lib/cardtrick/deck.rb', line 2 def initialize h={} @deck = h @d = [] @b = [] create! end |
Instance Method Details
#build! ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cardtrick/deck.rb', line 16 def build! @deck[:suits].each do |suit| # puts %[build: #{suit}] @deck[:faces].each_pair { |face, value| # puts %[build: #{suit} #{face} #{value}] @d << Card.new( { suit: suit, card: face, value: value } ) } ((@deck[:numbers][:low] || 1)..(@deck[:numbers][:high] || 10)).each { |number| # puts %[build: #{suit} #{number}] @d << Card.new( { suit: suit, card: number, value: number } ) } end if @deck.has_key? :specials [@deck[:specials]].flatten.each { |e| # puts %[build: #{e[:suit]} #{e[:face]} #{e[:value]}] @d << Card.new( { suit: e[:suit], card: e[:card], value: e[:value] } ) } end return nil end |
#burn ⇒ Object
56 57 58 |
# File 'lib/cardtrick/deck.rb', line 56 def burn @b end |
#create! ⇒ Object
9 10 11 12 13 14 |
# File 'lib/cardtrick/deck.rb', line 9 def create! (@deck[:decks] || 1).times { build! } if @deck[:shuffle] == true shuffle! end end |
#deck ⇒ Object
60 61 62 |
# File 'lib/cardtrick/deck.rb', line 60 def deck @deck[:deck] end |
#left ⇒ Object
52 53 54 |
# File 'lib/cardtrick/deck.rb', line 52 def left @d end |
#map(&b) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cardtrick/deck.rb', line 41 def map &b if @d.length < (@deck[:hand] || 1) puts %[Only #{@d.length} cards left...] create! puts %[Now we have #{@d.length}.] end a = [] (@deck[:hand] || 1).times { |t| x = @d.shift; @b << x; a << b.call(x) } return a end |
#shuffle! ⇒ Object
37 38 39 |
# File 'lib/cardtrick/deck.rb', line 37 def shuffle! @d.shuffle! end |