Class: Gambler::Deck

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

Overview

Handles a collection of Gambler::Card objects as a single Deck. options can be:

  • cards: An array of Cards this Deck will contain.

Examples:

@deck = Deck.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Deck

Returns a new instance of Deck.



13
14
15
16
17
18
19
20
21
# File 'lib/gambler/deck.rb', line 13

def initialize(options = {})
  options[:cards] ||= Card.all
  options[:cards].each do |card|
    raise Exceptions::InvalidDeck unless card.is_a? Card
  end

  @cards = options[:cards]
  @shuffled = false
end

Instance Attribute Details

#cardsObject (readonly)

Returns the value of attribute cards.



11
12
13
# File 'lib/gambler/deck.rb', line 11

def cards
  @cards
end

#shuffledObject (readonly)

Returns the value of attribute shuffled.



11
12
13
# File 'lib/gambler/deck.rb', line 11

def shuffled
  @shuffled
end

Instance Method Details

#deal_to(player) ⇒ Object

Deals one Card to the given player. Example:

@deck.deal_to(@player)


26
27
28
29
30
31
# File 'lib/gambler/deck.rb', line 26

def deal_to(player)
  card = @cards.first
  raise Exceptions::DeckEmpty if card.nil?
  player.hand << card
  @cards.delete(card)
end

#shuffle!Object

Shuffles the Deck; changes @cards to reflect the shuffle. Example:

@deck.shuffle!


36
37
38
39
# File 'lib/gambler/deck.rb', line 36

def shuffle!
  @cards = @cards.sort_by { rand }
  @shuffled = true
end

#sizeObject

Size of the current Deck (number of Cards remaining).



42
43
44
# File 'lib/gambler/deck.rb', line 42

def size
  @cards.size
end