class TwentyOne::Player

Attributes

bet[R]
busts[R]
chips[R]
choice[RW]
hand[R]
name[R]
pushes[R]
twenty_ones[R]
wins[R]

Public Class Methods

new(name) click to toggle source
# 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

Public Instance Methods

deal_bet(result) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

take_chip(color) click to toggle source
# File lib/twenty_one/player.rb, line 71
def take_chip(color)
        idx = @chips.index { |chip| chip.color == color }
        
        if not idx.nil? then
                temp = @chips[idx].clone 
                @chips.delete_at idx

                return temp
        end

        nil
end