Class: PlayerHand

Inherits:
Hand
  • Object
show all
Includes:
PlayerHandActions, PlayerHandDraw
Defined in:
lib/blackjack/player_hand.rb

Constant Summary collapse

MAX_PLAYER_HANDS =
7

Instance Attribute Summary collapse

Attributes inherited from Hand

#played

Instance Method Summary collapse

Methods included from PlayerHandActions

#can_dbl?, #can_hit?, #can_split?, #can_stand?, #dbl, #hit, #stand

Methods included from PlayerHandDraw

#draw, #draw_cards, #draw_money, #draw_status

Methods inherited from Hand

#blackjack?, #busted?, #deal_card, #value

Constructor Details

#initialize(blackjack, bet) ⇒ PlayerHand

Returns a new instance of PlayerHand.



15
16
17
18
19
20
21
# File 'lib/blackjack/player_hand.rb', line 15

def initialize(blackjack, bet)
  super(blackjack)
  @bet = bet
  @status = :unknown
  @paid = false
  @stood = false
end

Instance Attribute Details

#betObject

Returns the value of attribute bet.



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

def bet
  @bet
end

#blackjackObject

Returns the value of attribute blackjack.



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

def blackjack
  @blackjack
end

#cardsObject

Returns the value of attribute cards.



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

def cards
  @cards
end

Returns the value of attribute paid.



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

def paid
  @paid
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#stoodObject

Returns the value of attribute stood.



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

def stood
  @stood
end

Instance Method Details

#action?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/blackjack/player_hand.rb', line 83

def action?
  draw_actions
  c = Blackjack.getc($stdin)
  case c
  when 'h'
    hit
  when 's'
    stand
  when 'p'
    blackjack.split_current_hand
  when 'd'
    dbl
  else
    clear_draw_hands_action
  end
end

#clear_draw_hands_actionObject



100
101
102
103
104
# File 'lib/blackjack/player_hand.rb', line 100

def clear_draw_hands_action
  blackjack.clear
  blackjack.draw_hands
  action?
end

#collect_busted_handObject



65
66
67
68
69
# File 'lib/blackjack/player_hand.rb', line 65

def collect_busted_hand
  self.paid = true
  self.status = :lost
  blackjack.money -= bet
end

#collect_lost_handObject



46
47
48
49
# File 'lib/blackjack/player_hand.rb', line 46

def collect_lost_hand
  blackjack.money -= bet
  self.status = :lost
end

#done?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/blackjack/player_hand.rb', line 57

def done?
  return false unless no_more_actions?

  self.played = true
  collect_busted_hand if !paid && busted?
  true
end

#draw_actionsObject



106
107
108
109
110
111
112
113
# File 'lib/blackjack/player_hand.rb', line 106

def draw_actions
  actions = []
  actions << '(H) Hit'
  actions << '(S) Stand'
  actions << '(P) Split' if can_split?
  actions << '(D) Double' if can_dbl?
  puts " #{actions.join('  ')}"
end

#no_more_actions?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/blackjack/player_hand.rb', line 71

def no_more_actions?
  played || stood || blackjack? || busted? || value(:soft) == 21 || value(:hard) == 21
end

#pay(dealer_hand_value, dealer_busted) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/blackjack/player_hand.rb', line 23

def pay(dealer_hand_value, dealer_busted)
  return if paid

  self.paid = true
  player_hand_value = value(:soft)

  if player_hand_won?(dealer_busted, dealer_hand_value, player_hand_value)
    pay_won_hand
  elsif player_hand_lost?(dealer_hand_value, player_hand_value)
    collect_lost_hand
  else
    self.status = :push
  end
end

#pay_won_handObject



51
52
53
54
55
# File 'lib/blackjack/player_hand.rb', line 51

def pay_won_hand
  self.bet *= 1.5 if blackjack?
  blackjack.money += bet
  self.status = :won
end

#player_hand_lost?(dealer_hand_value, player_hand_value) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/blackjack/player_hand.rb', line 38

def player_hand_lost?(dealer_hand_value, player_hand_value)
  player_hand_value < dealer_hand_value
end

#player_hand_won?(dealer_busted, dealer_hand_value, player_hand_value) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/blackjack/player_hand.rb', line 42

def player_hand_won?(dealer_busted, dealer_hand_value, player_hand_value)
  dealer_busted || player_hand_value > dealer_hand_value
end

#processObject



75
76
77
78
79
80
81
# File 'lib/blackjack/player_hand.rb', line 75

def process
  if blackjack.more_hands_to_play?
    blackjack.play_more_hands
  else
    blackjack.play_dealer_hand
  end
end