Class: Player

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

Instance Method Summary collapse

Constructor Details

#initializePlayer

Returns a new instance of Player.



5
6
7
# File 'lib/blackjack/player.rb', line 5

def initialize
  @cards = []
end

Instance Method Details

#card_sum(ace = :high) ⇒ Object



17
18
19
20
21
# File 'lib/blackjack/player.rb', line 17

def card_sum(ace=:high)
  sum = 0
  @cards.each { |c| sum += c.value(ace) }
  sum
end

#cardsObject



40
41
42
# File 'lib/blackjack/player.rb', line 40

def cards
  @cards
end

#cards=(new_hand) ⇒ Object

for debug



45
46
47
# File 'lib/blackjack/player.rb', line 45

def cards=(new_hand)
  @cards = new_hand
end

#deal(deck) ⇒ Object



13
14
15
# File 'lib/blackjack/player.rb', line 13

def deal(deck)
  2.times { self.hit(deck) }
end

#hit(deck) ⇒ Object



9
10
11
# File 'lib/blackjack/player.rb', line 9

def hit(deck)
  @cards.push(deck.draw)
end

#hit_or_stay?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
# 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

#scoreObject

returns a best-possible score invariable of aces



24
25
26
27
28
29
30
31
# 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