Class: TwentyOne::Player

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

Direct Known Subclasses

Dealer

Constant Summary collapse

@@STARTING_CHIPS =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#betObject (readonly)

Returns the value of attribute bet.



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

def bet
  @bet
end

#bustsObject (readonly)

Returns the value of attribute busts.



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

def busts
  @busts
end

#chipsObject (readonly)

Returns the value of attribute chips.



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

def chips
  @chips
end

#choiceObject

Returns the value of attribute choice.



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

def choice
  @choice
end

#handObject (readonly)

Returns the value of attribute hand.



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

def hand
  @hand
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pushesObject (readonly)

Returns the value of attribute pushes.



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

def pushes
  @pushes
end

#twenty_onesObject (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

#winsObject (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