Class: Gloomhaven::Deck

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDeck

Creates a basic 20-card attack deck



8
9
10
11
12
# File 'lib/gloomhaven/deck.rb', line 8

def initialize
  @cards = []
  @drawn_cards = []
  build_base_deck!
end

Instance Attribute Details

#cardsObject (readonly)

Returns the value of attribute cards.



3
4
5
# File 'lib/gloomhaven/deck.rb', line 3

def cards
  @cards
end

#drawn_cardsObject (readonly)

Returns the value of attribute drawn_cards.



4
5
6
# File 'lib/gloomhaven/deck.rb', line 4

def drawn_cards
  @drawn_cards
end

Instance Method Details

#add!(card) ⇒ Object

Adds a card to the current deck. Useful for updating decks based on character-specific perks. Ex:

deck = Deck.new card = Card.new('Attack +3') deck.add!(card)

Raises:

  • (TypeError)


22
23
24
25
26
27
# File 'lib/gloomhaven/deck.rb', line 22

def add!(card)
  raise TypeError.new('Card must be a Gloomhaven::Card') unless card.is_a?(Gloomhaven::Card)

  @cards << card
  @cards
end

#bless!Object

Adds a bless card to the current deck, then soft shuffles



31
32
33
34
# File 'lib/gloomhaven/deck.rb', line 31

def bless!
  @cards << Card.new('Bless')
  shuffle!(soft_shuffle: true)
end

#curse!Object

Adds a curse card to the current deck, then soft shuffles



38
39
40
41
# File 'lib/gloomhaven/deck.rb', line 38

def curse!
  @cards << Card.new('Curse')
  shuffle!(soft_shuffle: true)
end

#drawObject

Shuffles the deck if trying to draw without any cards left in pile. Removes the top card from the deck, adds it to the @drawn_cards array (unless it's a bless/curse) Returns the drawn card. Ex: Deck.new.draw => #



49
50
51
52
53
54
55
# File 'lib/gloomhaven/deck.rb', line 49

def draw
  shuffle! if @cards.empty?

  card = @cards.shift
  @drawn_cards << card unless card.bless? || card.curse?
  card
end

#remove!(card) ⇒ Object

Removes a card from the current deck. Useful for updating decks based on character-specific perks. Ex:

deck = Deck.new card = Card.new('Attack -1') deck.remove!(card)

Raises:

  • (TypeError)


65
66
67
68
69
70
71
72
73
# File 'lib/gloomhaven/deck.rb', line 65

def remove!(card)
  raise TypeError.new('Card must be a Gloomhaven::Card') unless card.is_a?(Gloomhaven::Card)

  index = cards.index { |c| c.name == card.name }
  raise ArgumentError.new("Card: '#{card.name}' not found in deck") unless index

  @cards.delete_at(index)
  @cards
end

#shuffle!(options = {}) ⇒ Object

Randomizes the order of #cards By default, will shuffle all cards together.

Can override with options = { soft_shuffle: true } to only shuffle cards not yet drawn (useful for blesses/curses)



81
82
83
84
85
86
87
88
# File 'lib/gloomhaven/deck.rb', line 81

def shuffle!(options = {})
  options[:soft_shuffle] ||= false
  return @cards.shuffle! if options[:soft_shuffle]

  @cards = (@cards + @drawn_cards).shuffle
  @drawn_cards = []
  @cards
end