Class: Blackjack::Player

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

Direct Known Subclasses

Dealer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game, amount, options = {}) ⇒ Player

Returns a new instance of Player.



10
11
12
13
14
15
16
17
18
19
# File 'lib/blackjack/player.rb', line 10

def initialize(game, amount, options={})
  self.game = game
  self.cards = []
  self.finished = false
  self.amount = amount
  self.splitted = options.fetch(:splitted, false)
  if splitted?
    game.balance -= amount
  end
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



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

def amount
  @amount
end

#cardsObject

Returns the value of attribute cards.



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

def cards
  @cards
end

#finishedObject

Returns the value of attribute finished.



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

def finished
  @finished
end

#gameObject

Returns the value of attribute game.



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

def game
  @game
end

#positionObject

Returns the value of attribute position.



7
8
9
# File 'lib/blackjack/player.rb', line 7

def position
  @position
end

#splittedObject

Returns the value of attribute splitted.



3
4
5
# File 'lib/blackjack/player.rb', line 3

def splitted
  @splitted
end

Instance Method Details

#blackjack?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/blackjack/player.rb', line 57

def blackjack?
  cards.count == 2 && points == 21 && !splitted?
end

#busted?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/blackjack/player.rb', line 49

def busted?
  points > 21
end

#can_double?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/blackjack/player.rb', line 73

def can_double?
  !splitted_aces? && !finished? && cards.count == 2 && [9,10,11].include?(points) && enough_balance_available?
end

#can_hit?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/blackjack/player.rb', line 61

def can_hit?
  his_turn? && !finished? && points <= 20
end

#can_split?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/blackjack/player.rb', line 69

def can_split?
  !splitted? && !finished? && cards.count == 2 && cards.first.points == cards.last.points && enough_balance_available?
end

#can_stand?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/blackjack/player.rb', line 65

def can_stand?
  !finished?
end

#double!Object



146
147
148
149
150
151
152
# File 'lib/blackjack/player.rb', line 146

def double!
  raise "can't double" unless can_double?
  game.balance -= amount
  self.amount += amount
  hit! 
  stand! unless finished?
end

#enough_balance_available?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/blackjack/player.rb', line 89

def enough_balance_available?
  game.balance >= amount
end

#finish!Object



35
36
37
38
39
# File 'lib/blackjack/player.rb', line 35

def finish!
  raise "already finished" if finished?
  self.finished = true
  points # do we need this?
end

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  finished
end

#his_turn?Boolean

:nocov:

Returns:

  • (Boolean)


27
28
29
# File 'lib/blackjack/player.rb', line 27

def his_turn?
  previous_players.all?(&:finished?)
end

#hit!Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/blackjack/player.rb', line 103

def hit!
  raise "waiting for previous player to finish" unless his_turn?
  if can_hit?
    card = game.deck.draw
    self.cards << card
    if blackjack? || points==21 || busted?
      finish! 
    else
      points
    end        
  else
    raise "can't hit anymore"
  end
end

#inspectObject

:nocov:



22
23
24
# File 'lib/blackjack/player.rb', line 22

def inspect
  "<Player##{position} (#{points}): #{cards.collect(&:inspect).join(" ")} possible_actions: #{possible_actions}>"
end

#must_hit?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/blackjack/player.rb', line 53

def must_hit?
  cards.count == 1 || points <= 8 && !can_split?
end

#playing?Boolean

Returns:

  • (Boolean)


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

def playing?
  !finished?
end

#pointsObject



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/blackjack/player.rb', line 122

def points
  total = cards.sum(&:points)
  if total > 21 && cards.any?(&:ace?)
    # check if we can count some aces as a 1 to be still good

    cards.count(&:ace?).times do
      total -= 10
      return total if total <= 21
    end
  end
  total
end

#possible_actionsObject



135
136
137
138
139
140
141
142
143
144
# File 'lib/blackjack/player.rb', line 135

def possible_actions
  candidates = []
  candidates << :split! if can_split?
  candidates << :double! if can_double?
  if can_hit?
    candidates << :hit! 
    candidates << :stand!
  end
  candidates
end

#previous_playersObject



31
32
33
# File 'lib/blackjack/player.rb', line 31

def previous_players
  game.players.select{|other| other.position < position}
end

#resultObject



93
94
# File 'lib/blackjack/player.rb', line 93

def result
end

#split!Object



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/blackjack/player.rb', line 154

def split!
  raise "can't split" unless can_split?
  # take the second card and make it a new hand
  # and reindex_players all the other hands
  new_player = Player.new(game, amount, splitted: true)
  new_player.cards << cards.pop
  # insert in the hand behind him
  game.players.insert(position+1, new_player)
  game.reindex_players!
  hit!
  self.splitted = true
end

#splitted?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/blackjack/player.rb', line 77

def splitted?
  splitted
end

#splitted_aces?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/blackjack/player.rb', line 81

def splitted_aces?
  splitted? && cards.first.ace?
end

#stand!Object



118
119
120
# File 'lib/blackjack/player.rb', line 118

def stand!
  finish!
end

#statusObject

:nocov:



168
169
170
171
172
173
174
175
176
# File 'lib/blackjack/player.rb', line 168

def status
  return :playing if playing?
  return :busted if busted?
  return :blackjack if won_with_blackjack?
  return :waiting_for_dealer unless game.dealer.finished?
  return :push if points == game.dealer.points
  return :win if points > game.dealer.points || game.dealer.busted?
  :lose
end

#take_card!Object



96
97
98
99
100
101
# File 'lib/blackjack/player.rb', line 96

def take_card!
  raise "must hit! instead of taking a card" if cards.count >= 2
  # the initial card round
  self.cards << game.deck.draw
  finish!  if blackjack?
end

#won_with_blackjack?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/blackjack/player.rb', line 85

def won_with_blackjack?
  blackjack? && !game.dealer.blackjack?
end