Class: Hand

Inherits:
Object show all
Includes:
HandInformation, HandIteration
Defined in:
tomes/components/deck_builder/hand.rb

Overview

A collection of Cards.

Direct Known Subclasses

BoxOfCards, Deck

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HandInformation

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

Methods included from HandIteration

#each, #index, #map, #select

Instance Attribute Details

#cardsObject

Returns the value of attribute cards.



33
34
35
# File 'tomes/components/deck_builder/hand.rb', line 33

def cards
  @cards
end

#sorting_patternObject

Returns the value of attribute sorting_pattern.



40
41
42
# File 'tomes/components/deck_builder/hand.rb', line 40

def sorting_pattern
  @sorting_pattern
end

Instance Method Details

#[](*indices) ⇒ Object

Reference Returns Cards based on their index in #cards. Multiple indices can be used in a single call.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'tomes/components/deck_builder/hand.rb', line 54

def [](*indices)
  see_cards = []

  indices.each do |index|
    card = cards[index]
    next if see_cards.include? card

    see_cards << card
  end

  see_cards.flatten
end

#all_typesArray

Lists all types present in #cards.

Returns:

  • (Array)


128
129
130
131
132
133
134
135
136
137
# File 'tomes/components/deck_builder/hand.rb', line 128

def all_types
  list = []

  each do |card|
    list += card.types unless card.types == list
    list.uniq!
  end

  list
end

#all_valuesArray

Lists all values present in #cards.

Returns:

  • (Array)


141
142
143
144
145
146
147
148
149
# File 'tomes/components/deck_builder/hand.rb', line 141

def all_values
  list = []

  each do |card|
    list += card.values
  end

  list
end

#discard(*these_cards) ⇒ Array Also known as: give

Removes cards from #cards.

Parameters:

  • these_cards (Cards)

Returns:

  • (Array)


96
97
98
99
100
101
102
103
104
105
# File 'tomes/components/deck_builder/hand.rb', line 96

def discard(*these_cards)
  removed_cards = []

  each do |card|
    (removed_cards << card) if these_cards.include? card
  end

  self.cards -= removed_cards
  removed_cards
end

#discard_index(*indices) ⇒ Array

Removes cards from #cards.

Returns:

  • (Array)


110
111
112
113
114
115
116
117
118
# File 'tomes/components/deck_builder/hand.rb', line 110

def discard_index(*indices)
  removed_cards =
    indices.map do |index|
      cards[index]
    end

  self.cards -= removed_cards
  removed_cards
end

#move(this_card, destination_index) ⇒ Void Also known as: mov

Moves the designated card to the provided index.

Parameters:

  • this_card (Integer, Card)

    Either the Card or its index.

Returns:

  • (Void)


80
81
82
83
84
# File 'tomes/components/deck_builder/hand.rb', line 80

def move(this_card, destination_index)
  source_index = this_card.is_a?(Integer) ? this_card : card.index(this_card)

  cards.insert(destination_index, cards.delete_at(source_index))
end

#pop(number_of_cards = 1) ⇒ Object



120
121
122
# File 'tomes/components/deck_builder/hand.rb', line 120

def pop(number_of_cards = 1)
  cards.pop(number_of_cards)
end

#receive(new_cards) ⇒ Object Also known as: take, draw

Adds the provided card into #cards.



46
47
48
# File 'tomes/components/deck_builder/hand.rb', line 46

def receive(new_cards)
  self.cards += new_cards
end

#shuffle!Object



87
88
89
# File 'tomes/components/deck_builder/hand.rb', line 87

def shuffle!
  cards.shuffle!
end

#sort!Object

Reorder (Sort, Shift, Shuffle) Sorts #cards based on the ‘sorting_pattern`. Will raise an exception if a pattern has not been assigned.

Raises:

  • (StandardError)


69
70
71
72
73
74
75
# File 'tomes/components/deck_builder/hand.rb', line 69

def sort!
  raise StandardError, 'You must first assign a sorting pattern before calling #sort!.' if sorting_pattern.nil?

  cards.sort_by do |card|
    card_sort(card)
  end
end