mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-12 15:50:20 -08:00
implement insurance bets
This commit is contained in:
@@ -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}"
|
||||
|
||||
|
||||
@@ -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?
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user