Class: Deck

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDeck

Returns a new instance of Deck.



320
321
322
323
324
325
326
# File 'lib/cardtable.rb', line 320

def initialize
  # Create an array of 52 shuffled cards to be the deck
  self.cards = (0..51).to_a.shuffle.collect { |index| Card.new(index) }

  # Set the first card to be dealt as the card at index 0 in the deck
  self.nextcard = 0
end

Instance Attribute Details

#cardsObject

Returns the value of attribute cards.



318
319
320
# File 'lib/cardtable.rb', line 318

def cards
  @cards
end

#nextcardObject

Returns the value of attribute nextcard.



318
319
320
# File 'lib/cardtable.rb', line 318

def nextcard
  @nextcard
end

Instance Method Details

#deal(player) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/cardtable.rb', line 328

def deal(player)
  # Add the next card in the deck to the player's hand
  # We need to copy the card object from the deck before appending to the hand
  # as it will be referenced otherwise. This can cause the deck card to be 'faceup = false'
  # when we set this attribute for the dealer's second card. This will mean that when the
  # deck loops around on itself, that card will be dealt facedown which is wrong!
  player.hand << self.cards[self.nextcard].dup
  player.score << self.cards[self.nextcard].score.to_i

  # And then increment the position in the deck of the next card to be dealt
  self.nextcard = (self.nextcard + 1) % 52
end

#shuffleObject



341
342
343
344
345
346
# File 'lib/cardtable.rb', line 341

def shuffle
  # Shuffle the cards in the deck and set the next card to be dealt
  # as the card at index 0
  self.cards = self.cards.shuffle
  self.nextcard = 0
end