Class: Decktet::Pile

Inherits:
Object
  • Object
show all
Defined in:
lib/decktet/pile.rb

Direct Known Subclasses

Deck

Instance Method Summary collapse

Constructor Details

#initialize(*cards) ⇒ Pile

Returns a new instance of Pile.



3
4
5
6
7
8
9
# File 'lib/decktet/pile.rb', line 3

def initialize(*cards)
  @cards = cards.flatten.compact

  unless cards.empty? || @cards.all? { |card| Decktet::Card === card }
    raise ArgumentError.new('A pile must be initialized empty or with an array of cards or card groups')
  end
end

Instance Method Details

#add(*new_cards) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/decktet/pile.rb', line 40

def add(*new_cards)
  new_cards = new_cards.flatten
  unless new_cards.all? { |card| Decktet::Card === card }
    raise ArgumentError, "Only Decktet::Card objects can be added to a #{self.class.name}"
  end
  @cards.unshift(*new_cards)
  self
end

#cardsObject



11
# File 'lib/decktet/pile.rb', line 11

def cards = @cards.dup

#cut(depth = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/decktet/pile.rb', line 20

def cut(depth=nil)
  return self if @cards.empty?

  unless depth.nil?
    raise ArgumentError.new('cut depth cannot be negative') if depth < 0
    raise ArgumentError.new('cut depth cannot be greater than the number of cards') if depth > @cards.count
  end

  depth ||= rand(1...@cards.count)
  @cards.rotate!(depth)
  self
end

#draw(count = 1) ⇒ Object



33
34
35
36
37
38
# File 'lib/decktet/pile.rb', line 33

def draw(count=1)
  unless count.between?(0,@cards.count)
    raise ArgumentError.new('draw amount cannot be negative or greater than the number of cards')
  end
  @cards.shift(count)
end

#shuffleObject



15
16
17
18
# File 'lib/decktet/pile.rb', line 15

def shuffle
  @cards.shuffle!
  self
end

#sizeObject



13
# File 'lib/decktet/pile.rb', line 13

def size = @cards.size