Class: Blackjack::Player
- Inherits:
-
Object
- Object
- Blackjack::Player
- Defined in:
- lib/blackjack/player.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#amount ⇒ Object
Returns the value of attribute amount.
-
#cards ⇒ Object
Returns the value of attribute cards.
-
#finished ⇒ Object
Returns the value of attribute finished.
-
#game ⇒ Object
Returns the value of attribute game.
-
#position ⇒ Object
Returns the value of attribute position.
-
#splitted ⇒ Object
Returns the value of attribute splitted.
Instance Method Summary collapse
- #blackjack? ⇒ Boolean
- #busted? ⇒ Boolean
- #can_double? ⇒ Boolean
- #can_hit? ⇒ Boolean
- #can_split? ⇒ Boolean
- #can_stand? ⇒ Boolean
- #double! ⇒ Object
- #enough_balance_available? ⇒ Boolean
- #finish! ⇒ Object
- #finished? ⇒ Boolean
-
#his_turn? ⇒ Boolean
:nocov:.
- #hit! ⇒ Object
-
#initialize(game, amount, options = {}) ⇒ Player
constructor
A new instance of Player.
-
#inspect ⇒ Object
:nocov:.
- #must_hit? ⇒ Boolean
- #playing? ⇒ Boolean
- #points ⇒ Object
- #possible_actions ⇒ Object
- #previous_players ⇒ Object
- #result ⇒ Object
- #split! ⇒ Object
- #splitted? ⇒ Boolean
- #splitted_aces? ⇒ Boolean
- #stand! ⇒ Object
-
#status ⇒ Object
:nocov:.
- #take_card! ⇒ Object
- #won_with_blackjack? ⇒ Boolean
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, ={}) self.game = game self.cards = [] self.finished = false self.amount = amount self.splitted = .fetch(:splitted, false) if splitted? game.balance -= amount end end |
Instance Attribute Details
#amount ⇒ Object
Returns the value of attribute amount.
8 9 10 |
# File 'lib/blackjack/player.rb', line 8 def amount @amount end |
#cards ⇒ Object
Returns the value of attribute cards.
5 6 7 |
# File 'lib/blackjack/player.rb', line 5 def cards @cards end |
#finished ⇒ Object
Returns the value of attribute finished.
6 7 8 |
# File 'lib/blackjack/player.rb', line 6 def finished @finished end |
#game ⇒ Object
Returns the value of attribute game.
4 5 6 |
# File 'lib/blackjack/player.rb', line 4 def game @game end |
#position ⇒ Object
Returns the value of attribute position.
7 8 9 |
# File 'lib/blackjack/player.rb', line 7 def position @position end |
#splitted ⇒ Object
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
57 58 59 |
# File 'lib/blackjack/player.rb', line 57 def blackjack? cards.count == 2 && points == 21 && !splitted? end |
#busted? ⇒ Boolean
49 50 51 |
# File 'lib/blackjack/player.rb', line 49 def busted? points > 21 end |
#can_double? ⇒ 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
61 62 63 |
# File 'lib/blackjack/player.rb', line 61 def can_hit? his_turn? && !finished? && points <= 20 end |
#can_split? ⇒ 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
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
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
45 46 47 |
# File 'lib/blackjack/player.rb', line 45 def finished? finished end |
#his_turn? ⇒ Boolean
:nocov:
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 |
#inspect ⇒ Object
: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
53 54 55 |
# File 'lib/blackjack/player.rb', line 53 def must_hit? cards.count == 1 || points <= 8 && !can_split? end |
#playing? ⇒ Boolean
41 42 43 |
# File 'lib/blackjack/player.rb', line 41 def !finished? end |
#points ⇒ Object
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_actions ⇒ Object
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_players ⇒ Object
31 32 33 |
# File 'lib/blackjack/player.rb', line 31 def previous_players game.players.select{|other| other.position < position} end |
#result ⇒ Object
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
77 78 79 |
# File 'lib/blackjack/player.rb', line 77 def splitted? splitted end |
#splitted_aces? ⇒ 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 |
#status ⇒ Object
:nocov:
168 169 170 171 172 173 174 175 176 |
# File 'lib/blackjack/player.rb', line 168 def status return :playing if 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
85 86 87 |
# File 'lib/blackjack/player.rb', line 85 def won_with_blackjack? blackjack? && !game.dealer.blackjack? end |