Class: CARD::C

Inherits:
Object
  • Object
show all
Defined in:
lib/toychest/card.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(k) ⇒ C

Returns a new instance of C.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/toychest/card.rb', line 41

def initialize k
	@id = k
	@shoe = 0
	@type = :blank
	@deck = []
	@hand = []
	@burn = []
	if @deck.length == 0
		shoe
	end
end

Instance Attribute Details

#burnObject (readonly)

Returns the value of attribute burn.



39
40
41
# File 'lib/toychest/card.rb', line 39

def burn
  @burn
end

#deckObject

Returns the value of attribute deck.



40
41
42
# File 'lib/toychest/card.rb', line 40

def deck
  @deck
end

#handObject (readonly)

Returns the value of attribute hand.



39
40
41
# File 'lib/toychest/card.rb', line 39

def hand
  @hand
end

#typeObject

Returns the value of attribute type.



40
41
42
# File 'lib/toychest/card.rb', line 40

def type
  @type
end

Instance Method Details

#[](k) ⇒ Object



96
97
98
# File 'lib/toychest/card.rb', line 96

def [] k
	draw k
end

#card(h = {}) ⇒ Object



61
62
63
# File 'lib/toychest/card.rb', line 61

def card h={}
	@deck << CARD.card({ id: @deck.length }.merge(h))
end

#clear!Object



52
53
54
# File 'lib/toychest/card.rb', line 52

def clear!
	@deck.clear
end

#draw(*n) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/toychest/card.rb', line 89

def draw *n
	@hand.each { |e| e[:burn].call(self, @hand, e); @burn << e }
	@hand.clear
	(n[0] || 1).times { if @deck.length == 0; next; end; @hand << @deck.shift; }
	@hand.each { |e| e[:draw].call(self, @hand, e) }
	return @hand			
end

#shake!Object



58
59
60
# File 'lib/toychest/card.rb', line 58

def shake!
	@hand.shuffle!
end

#shoe(*t, &b) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/toychest/card.rb', line 64

def shoe *t, &b
	@shoe += 1
	d = CARD.deck[t[0] || @type]
	d[:suits].each_pair do |suit, color|
		d[:face].each_pair do |card, points|
			card({ name: %[#{card} of #{suit}], suit: suit, color: color, card: card, points: points,
			draw: lambda { |s, a, e| puts %[#{t[0] || @type} Face card #{e[:name]}] },
			burn: lambda { |s, a, e| puts %[#{t[0] || @type} face card #{e[:name]}] }
			 })
		end
		d[:numbers].each do |card|
			card({ name: %[#{card.humanize} of #{suit}], suit: suit, color: color, card: card, points: card,
			draw: lambda { |s, a, e| puts %[#{t[0] || @type} Number card #{e[:name]}] },
			burn: lambda { |s, a, e| puts %[#{t[0] || @type} number card #{e[:name]}] } })
		end	
	end
	d[:special].each_pair do |card, points|
		card({ name: %[#{card.to_s.gsub("_"," ")} #{t[0] || @type} card], suit: :special, color: :none, card: card, points: points, 
		draw: lambda { |s, a, e| puts %[#{t[0] || @type} Special card #{e[:name]}] },
		burn: lambda { |s, a, e| puts %[#{t[0] || @type} special card #{e[:name]}] } })
	end
	if block_given?
		b.call(self)
	end
end

#shuffle!Object



55
56
57
# File 'lib/toychest/card.rb', line 55

def shuffle!
	@deck.shuffle!
end