class Player

Public Class Methods

new() click to toggle source
# File lib/blackjack/player.rb, line 5
def initialize
  @cards = []
end

Public Instance Methods

card_sum(ace=:high) click to toggle source
# File lib/blackjack/player.rb, line 17
def card_sum(ace=:high)
  sum = 0
  @cards.each { |c| sum += c.value(ace) }
  sum
end
cards() click to toggle source
# File lib/blackjack/player.rb, line 40
def cards
  @cards
end
cards=(new_hand) click to toggle source

for debug

# File lib/blackjack/player.rb, line 45
def cards=(new_hand)
  @cards = new_hand
end
deal(deck) click to toggle source
# File lib/blackjack/player.rb, line 13
def deal(deck)
  2.times { self.hit(deck) }
end
hit(deck) click to toggle source
# File lib/blackjack/player.rb, line 9
def hit(deck)
  @cards.push(deck.draw)
end
hit_or_stay?() click to toggle source
# File lib/blackjack/player.rb, line 33
def hit_or_stay?
  if self.card_sum(:high) < 11 or self.card_sum(:low) < 11
    return true
  end
  return false
end
score() click to toggle source

returns a best-possible score invariable of aces

# File lib/blackjack/player.rb, line 24
def score
  score = self.card_sum(ace=:high)
  self.send(:aces).times do
    score -= 10
    return score if score <= 21
  end
  return score
end

Private Instance Methods

aces() click to toggle source
# File lib/blackjack/player.rb, line 51
def aces
  @cards.count { |x| x.value(:low) == 1 }
end