Class: TwentyOne::Player
- Inherits:
-
Object
- Object
- TwentyOne::Player
- Defined in:
- lib/twenty_one/player.rb
Direct Known Subclasses
Constant Summary collapse
- @@STARTING_CHIPS =
100
Instance Attribute Summary collapse
-
#bet ⇒ Object
readonly
Returns the value of attribute bet.
-
#busts ⇒ Object
readonly
Returns the value of attribute busts.
-
#chips ⇒ Object
readonly
Returns the value of attribute chips.
-
#choice ⇒ Object
Returns the value of attribute choice.
-
#hand ⇒ Object
readonly
Returns the value of attribute hand.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#pushes ⇒ Object
readonly
Returns the value of attribute pushes.
-
#twenty_ones ⇒ Object
readonly
Returns the value of attribute twenty_ones.
-
#wins ⇒ Object
readonly
Returns the value of attribute wins.
Instance Method Summary collapse
- #deal_bet(result) ⇒ Object
-
#initialize(name) ⇒ Player
constructor
A new instance of Player.
- #make_bet(amount) ⇒ Object
Constructor Details
#initialize(name) ⇒ Player
Returns a new instance of Player.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/twenty_one/player.rb', line 11 def initialize(name) @name = name @twenty_ones = 0 @wins = 0 @busts = 0 @pushes = 0 @bet = Bet.new @hand = Hand.new @chips = Chip.generate_chips(:white, 50) .concat Chip.generate_chips(:red, 25) .concat Chip.generate_chips(:green, 15) .concat Chip.generate_chips(:black, 10) end |
Instance Attribute Details
#bet ⇒ Object (readonly)
Returns the value of attribute bet.
8 9 10 |
# File 'lib/twenty_one/player.rb', line 8 def bet @bet end |
#busts ⇒ Object (readonly)
Returns the value of attribute busts.
8 9 10 |
# File 'lib/twenty_one/player.rb', line 8 def busts @busts end |
#chips ⇒ Object (readonly)
Returns the value of attribute chips.
8 9 10 |
# File 'lib/twenty_one/player.rb', line 8 def chips @chips end |
#choice ⇒ Object
Returns the value of attribute choice.
9 10 11 |
# File 'lib/twenty_one/player.rb', line 9 def choice @choice end |
#hand ⇒ Object (readonly)
Returns the value of attribute hand.
8 9 10 |
# File 'lib/twenty_one/player.rb', line 8 def hand @hand end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/twenty_one/player.rb', line 8 def name @name end |
#pushes ⇒ Object (readonly)
Returns the value of attribute pushes.
8 9 10 |
# File 'lib/twenty_one/player.rb', line 8 def pushes @pushes end |
#twenty_ones ⇒ Object (readonly)
Returns the value of attribute twenty_ones.
8 9 10 |
# File 'lib/twenty_one/player.rb', line 8 def twenty_ones @twenty_ones end |
#wins ⇒ Object (readonly)
Returns the value of attribute wins.
8 9 10 |
# File 'lib/twenty_one/player.rb', line 8 def wins @wins end |
Instance Method Details
#deal_bet(result) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/twenty_one/player.rb', line 49 def deal_bet(result) case result when :twenty_one @wins += 1 @twenty_ones += 1 @chips.concat @bet.payout(:twenty_one) when :win @wins += 1 @chips.concat @bet.payout(:win) when :bust @busts += 1 when :push @pushes += 1 @chips.concat @bet.payout(:push) end @hand.clear @bet.clear end |
#make_bet(amount) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/twenty_one/player.rb', line 25 def make_bet(amount) while @bet.value < amount distance_to_total = amount - @bet.value if distance_to_total >= 100 chip = take_chip :black elsif distance_to_total >= 25 chip = take_chip :green elsif distance_to_total >= 5 chip = take_chip :red else chip = take_chip :white end if chip.nil? return false end @bet.chips.push chip end true end |