Class: Gambler::Deck
- Inherits:
-
Object
- Object
- Gambler::Deck
- 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
-
#cards ⇒ Object
readonly
Returns the value of attribute cards.
-
#shuffled ⇒ Object
readonly
Returns the value of attribute shuffled.
Instance Method Summary collapse
-
#deal_to(player) ⇒ Object
Deals one Card to the given
player
. -
#initialize(options = {}) ⇒ Deck
constructor
A new instance of Deck.
-
#shuffle! ⇒ Object
Shuffles the Deck; changes
@cards
to reflect the shuffle. -
#size ⇒ Object
Size of the current Deck (number of Cards remaining).
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( = {}) [:cards] ||= Card.all [:cards].each do |card| raise Exceptions::InvalidDeck unless card.is_a? Card end @cards = [:cards] @shuffled = false end |
Instance Attribute Details
#cards ⇒ Object (readonly)
Returns the value of attribute cards.
11 12 13 |
# File 'lib/gambler/deck.rb', line 11 def cards @cards end |
#shuffled ⇒ Object (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 |
#size ⇒ Object
Size of the current Deck (number of Cards remaining).
42 43 44 |
# File 'lib/gambler/deck.rb', line 42 def size @cards.size end |