Class: Cardlike::Deck

Inherits:
Array
  • Object
show all
Defined in:
lib/cardlike/deck.rb

Overview

Represents a game deck. Best used with the Card and Deck DSL. See Cardlike.

Direct Known Subclasses

Hand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Deck

Create a new Deck. Options should always include :name and may optionally include :cards, which is an array of Card objects (ideally) to keep in this deck. It’s better to use Cardlike.deck to create decks, though.



12
13
14
15
# File 'lib/cardlike/deck.rb', line 12

def initialize(options={})
  self.name = options[:name]
  options[:cards].each { |c| self << c } if options[:cards]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/cardlike/deck.rb', line 5

def name
  @name
end

Instance Method Details

#+(ary) ⇒ Object

Append an array (or Deck) of cards to this Deck.



35
36
37
38
# File 'lib/cardlike/deck.rb', line 35

def +(ary)
  ary.each { |a| self << a }
  self
end

#card(name, &block) ⇒ Object

DSL method to create a new card inside this deck. Also check out the new_ methods created by Cardlike.type_of_card. This works just like Cardlike.card except that it automatically adds the card to this deck.

Cardlike.deck "Options Deck" do
  card "Magic Spell"
  card "Arcane Mark"
  card "Simple Attack"
end


86
87
88
89
90
# File 'lib/cardlike/deck.rb', line 86

def card(name, &block)
  c = Cardlike.card(name, &block)
  self << c
  c
end

#copy_card(name) ⇒ Object

DSL method to add a duplicate of a pre-defined card to this Deck.

Cardlike.game do
  card "Super Strike"

  deck "My Deck" do
    4.times { copy_card "Super Strike" }
  end
end


69
70
71
72
73
# File 'lib/cardlike/deck.rb', line 69

def copy_card(name)
  raise "Card '#{name}' not found." unless card = Cardlike.the_card(name)
  copy = card.dup
  self << copy
end

#drawObject

Draw the top card from this deck and return it.



20
21
22
# File 'lib/cardlike/deck.rb', line 20

def draw
  self.pop
end

#draw_into(deck) ⇒ Object

Like Deck#draw except that it draws a card and then adds it to a Deck or Hand. Also returns the card drawn.

Cardlike.the_deck("Poker Deck").draw_into(Cardlike.the_hand("Player 1"))


98
99
100
101
102
# File 'lib/cardlike/deck.rb', line 98

def draw_into(deck)
  card = draw
  deck << card
  card
end

#include_card(name) ⇒ Object

DSL method to add a pre-defined card to this Deck. This inserts the unique card into the deck. If you want to put a copy into the deck (or several copies), use Deck#copy_card instead.

Cardlike.game do
  card "Super Strike"

  deck "My Deck" do
    include_card "Super Strike"
  end
end


53
54
55
56
# File 'lib/cardlike/deck.rb', line 53

def include_card(name)
  raise "Card '#{name}' not found." unless card = Cardlike.the_card(name)
  self << card
end

#shuffleObject

Shuffle this deck and return a copy Deck. Probably more useful is Deck#shuffle! which will shuffle this deck in place.



28
29
30
# File 'lib/cardlike/deck.rb', line 28

def shuffle
  self.dup.shuffle!
end