Class: BoxOfCards

Inherits:
Hand show all
Defined in:
tomes/components/deck_builder/box_of_cards.rb

Overview

A Hand with the ability to generate a base set of Cards. Games should only use one box of cards.

Instance Attribute Summary

Attributes inherited from Hand

#cards, #sorting_pattern

Instance Method Summary collapse

Methods inherited from Hand

#[], #all_types, #all_values, #discard, #discard_index, #move, #pop, #receive, #shuffle!, #sort!

Methods included from HandInformation

#empty?, #full?, #include?, #length

Methods included from HandIteration

#each, #index, #map, #select

Instance Method Details

#assign_card_string(procedure) ⇒ Void

Assigns a #to_s value for each Card in #cards.

Parameters:

  • procedure (Proc, Lambda)

Returns:

  • (Void)


69
70
71
# File 'tomes/components/deck_builder/box_of_cards.rb', line 69

def assign_card_string(procedure)
  each { |card| card.to_s = procedure.call(card) }
end

#copy(dup_cards = self.cards, number_of_times = 1) ⇒ Array<Cards>

Returns a number of copies for the provided Card or Array of Cards.

Parameters:

  • dup_cards (Card, Array<Cards>) (defaults to: self.cards)

    Optional; defaults to #cards.

Returns:

  • (Array<Cards>)

    Optional; defaults to 1.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'tomes/components/deck_builder/box_of_cards.rb', line 46

def copy(dup_cards = self.cards, number_of_times = 1)
  dup_cards = [dup_cards] unless dup_cards.is_a? Array

  copies = []

  number_of_times.times do
    dup_cards.each do |original_card|
      copies << Card.new(original_card.types, original_card.values)
    end
  end

  copies
end

#copy_and_add(dup_cards = self.cards, number_of_times = 1) ⇒ Void Also known as: duplicate

Copies the provided Card or Array of Cards a number of times then adds it to #cards.

Parameters:

  • dup_cards (Card, Array<Cards>) (defaults to: self.cards)

    Optional; defaults to #cards.

Returns:

  • (Void)

    Optional; defaults to 1.



38
39
40
# File 'tomes/components/deck_builder/box_of_cards.rb', line 38

def copy_and_add(dup_cards = self.cards, number_of_times = 1)
  self.cards += copy(dup_cards, number_of_times)
end

#generate_deckDeck

Generates a Deck instance using #cards.

Returns:



62
63
64
# File 'tomes/components/deck_builder/box_of_cards.rb', line 62

def generate_deck
  Deck.new(cards)
end

#make_cards(quantity = 1) ⇒ Void

Creates the given number of default Cards and adds them to the list.

A block can be provided to immediately operate on each new Card.
This block is equivalent to calling #each on an array of all the Cards that were just generated.

Parameters:

  • quantity (Integer) (defaults to: 1)

    Optional; defaults to 1.

Returns:

  • (Void)


24
25
26
27
28
29
30
31
32
33
# File 'tomes/components/deck_builder/box_of_cards.rb', line 24

def make_cards(quantity = 1)
  new_cards = Array.new(quantity) { Card.new }

  new_cards.each_with_index do |card, index|
    yield(card, index) if block_given?
  end

  self.cards += new_cards
  new_cards
end