class Deck

Public Class Methods

new(num_decks = 1) click to toggle source
# File lib/badcards/deck.rb, line 2
def initialize(num_decks = 1)
  @cards = []
  num_decks.times do
    Card.all_cards do |value, suit|
      @cards << Card.new(value, suit)
    end
  end
end

Public Instance Methods

cards() click to toggle source
# File lib/badcards/deck.rb, line 11
def cards
  @cards
end
draw(num_to_draw = 1, hand_for_draw = nil) click to toggle source
# File lib/badcards/deck.rb, line 40
def draw(num_to_draw = 1, hand_for_draw = nil)
  @drawn = []
  num_to_draw.times do
    if card = @cards.shift
      @drawn << card
    else
      break
    end
  end
  if hand_for_draw && hand_for_draw.is_a?(Hand)
    hand_for_draw.cards += @drawn
  else
    if @drawn.count <= 1
      return @drawn.first
    else
      return @drawn
    end
  end
end
peek(num_to_peek = 1) click to toggle source
# File lib/badcards/deck.rb, line 19
def peek(num_to_peek = 1)
  if num_to_peek == 1
    @cards.first
  else
    @cards[0..num_to_peek-1]
  end
end
shuffle(num_times = 7) click to toggle source

Defaults to seven ‘rounds’ of shuffling, determined to be the optimal value per Diaconis (en.wikipedia.org/wiki/Persi_Diaconis#Card_shuffling)

# File lib/badcards/deck.rb, line 29
def shuffle(num_times = 7)
  num_times.times do
    @cards.each_with_index do |card, index|
      switch = rand(size)
      @cards[index] = @cards[switch]
      @cards[switch] = card
    end
  end
  return
end
size() click to toggle source
# File lib/badcards/deck.rb, line 15
def size
  @cards.count
end