implement insurance bets

This commit is contained in:
Alaa Sarhan
2022-03-29 21:29:00 +02:00
parent f3399cd34b
commit 3130a3d74d
3 changed files with 40 additions and 2 deletions

View File

@@ -22,6 +22,7 @@ class Game
loop do
collect_bets_and_deal
play_players
check_for_insurance_bets
play_dealer
settle
end
@@ -48,6 +49,18 @@ class Game
end
end
def check_for_insurance_bets
return if @dealer_hand.cards[0].label != "A"
print "ANY INSURANCE? "
return if gets.strip != "Y"
@players.each_entry do |player|
print "PLAYER #{player.id} INSURANCE BET? "
player.bet_insurance(gets.to_i)
end
end
def play_dealer
puts "DEALER HAS A \t#{@dealer_hand.cards[1].label} CONCEALED FOR A TOTAL OF #{@dealer_hand.total}"

View File

@@ -29,6 +29,10 @@ class Hand
@state == HAND_STATE_STAND
end
def is_blackjack?
total == 21 && @cards.length == 2
end
def total(is_dealer: false)
return @total unless @total.nil?

View File

@@ -5,17 +5,21 @@ class Player
def initialize(id)
@id = id
@balance = 0
@original_bet = 0
@insurance = 0
@hand = nil
@split_hand = nil
end
attr_reader :id, :balance, :hand, :split_hand
attr_reader :id, :balance, :hand, :split_hand, :insurance
## Begining of hand dealing actions
def deal_initial_hand(hand)
@hand = hand
@split_hand = nil
@max_insurance = @hand.bet / 2
@insurance = 0
end
def has_split_hand?
@@ -32,7 +36,18 @@ class Player
@hand, @split_hand = @hand.split
end
def bet_insurance(insurance_bet)
def bet_insurance(bet)
if bet < 0
bet = 0
puts "NEGATIVE BET -- using 0 insurance bet"
end
if bet > @max_insurance
bet = @max_insurance
puts "TOO HIGH -- using max insurance bet of #{bet}"
end
@insurance = bet
end
## End of hand dealing actions
@@ -45,6 +60,12 @@ class Player
balance_update += get_balance_update(@split_hand, dealer_hand)
end
if dealer_hand.is_blackjack?
balance_update += 2 * @insurance
else
balance_update -= @insurance
end
@balance += balance_update
balance_update