Class: Shithead::Stack
- Inherits:
-
Object
- Object
- Shithead::Stack
- Defined in:
- lib/shithead/stack.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#sets ⇒ Object
readonly
Returns the value of attribute sets.
Instance Method Summary collapse
- #add(card) ⇒ Object
- #cards ⇒ Object
- #clearable? ⇒ Boolean
- #delete(card) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Stack
constructor
A new instance of Stack.
- #release_to(stack) ⇒ Object
- #size ⇒ Object
- #to_s ⇒ Object
- #top ⇒ Object
Constructor Details
#initialize ⇒ Stack
Returns a new instance of Stack.
4 5 6 |
# File 'lib/shithead/stack.rb', line 4 def initialize @sets = [] end |
Instance Attribute Details
#sets ⇒ Object (readonly)
Returns the value of attribute sets.
2 3 4 |
# File 'lib/shithead/stack.rb', line 2 def sets @sets end |
Instance Method Details
#add(card) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/shithead/stack.rb', line 8 def add(card) if empty? || top.value != card.value sets.unshift Shithead::Set.new([card]) else top.add card end end |
#cards ⇒ Object
16 17 18 |
# File 'lib/shithead/stack.rb', line 16 def cards sets.collect(&:cards).flatten end |
#clearable? ⇒ Boolean
20 21 22 23 24 |
# File 'lib/shithead/stack.rb', line 20 def clearable? return false if empty? top.value == "10" || top.size >= 4 end |
#delete(card) ⇒ Object
26 27 28 |
# File 'lib/shithead/stack.rb', line 26 def delete(card) sets.each { |set| set.delete card } end |
#empty? ⇒ Boolean
30 31 32 |
# File 'lib/shithead/stack.rb', line 30 def empty? sets.empty? end |
#release_to(stack) ⇒ Object
34 35 36 37 |
# File 'lib/shithead/stack.rb', line 34 def release_to(stack) sets.each { |set| set.release_to stack } sets.clear end |
#size ⇒ Object
39 40 41 |
# File 'lib/shithead/stack.rb', line 39 def size sets.sum &:size end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/shithead/stack.rb', line 47 def to_s sets.first && sets.first.to_s end |
#top ⇒ Object
43 44 45 |
# File 'lib/shithead/stack.rb', line 43 def top sets.first end |