Class: Shithead::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/shithead/stack.rb

Direct Known Subclasses

SortedStack

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStack

Returns a new instance of Stack.



4
5
6
# File 'lib/shithead/stack.rb', line 4

def initialize
  @sets = []
end

Instance Attribute Details

#setsObject (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

#cardsObject



16
17
18
# File 'lib/shithead/stack.rb', line 16

def cards
  sets.collect(&:cards).flatten
end

#clearable?Boolean

Returns:

  • (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

Returns:

  • (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

#sizeObject



39
40
41
# File 'lib/shithead/stack.rb', line 39

def size
  sets.sum &:size
end

#to_sObject



47
48
49
# File 'lib/shithead/stack.rb', line 47

def to_s
  sets.first && sets.first.to_s
end

#topObject



43
44
45
# File 'lib/shithead/stack.rb', line 43

def top
  sets.first
end