Class: Deck

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

Instance Method Summary collapse

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

#burnObject



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

#deckObject



60
61
62
# File 'lib/cardtrick/deck.rb', line 60

def deck
  @deck[:deck]
end

#leftObject



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