Class: Cardlike::Hand

Inherits:
Deck
  • Object
show all
Defined in:
lib/cardlike/hand.rb

Overview

Represents a game hand. Best used with the Card and Deck DSL. See Cardlike and Cardlike::Deck, from which this inherits.

Instance Attribute Summary

Attributes inherited from Deck

#name

Instance Method Summary collapse

Methods inherited from Deck

#+, #card, #copy_card, #draw, #draw_into, #include_card, #initialize, #shuffle

Constructor Details

This class inherits a constructor from Cardlike::Deck

Instance Method Details

#remove_card(card_name) ⇒ Object

Remove and return a Card from this hand by name.



9
10
11
# File 'lib/cardlike/hand.rb', line 9

def remove_card(card_name)
  self.delete(self.find { |card| card.name == card_name })
end

#remove_card_at(index) ⇒ Object

Remove and return a Card from this hand at the index.



16
17
18
# File 'lib/cardlike/hand.rb', line 16

def remove_card_at(index)
  self.delete_at(index)
end

#remove_card_if(&block) ⇒ Object

Remove cards for which the block evaluates to true, returning removed cards in an Array. If no cards are found, returns an empty array.



36
37
38
39
# File 'lib/cardlike/hand.rb', line 36

def remove_card_if(&block)
  matches = self.select { |card| yield(card) }
  matches.collect { |match| self.delete(match) }
end

#remove_random_card(&block) ⇒ Object



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

def remove_random_card(&block)
  if block
    options = []
    self.each { |c| options << c if block.call(c) }
    c = options.sample
    return self.delete(c)
  else
    c = self.sample
    return self.delete(c)
  end
end

#to_sObject



41
42
43
44
45
# File 'lib/cardlike/hand.rb', line 41

def to_s
  cards = ["Hand: #{name}"]
  cards += self.collect { |c| "-> #{c}" }
  cards.join("\n")
end