mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-21 06:43:06 -08:00
Handle insurance
This commit is contained in:
@@ -70,29 +70,44 @@ public class Game {
|
|||||||
LinkedList<Card> dealerHand = new LinkedList<>();
|
LinkedList<Card> dealerHand = new LinkedList<>();
|
||||||
Player dealer = new Player(0); //Dealer is Player 0 - this can be converted into a dealer class later on
|
Player dealer = new Player(0); //Dealer is Player 0 - this can be converted into a dealer class later on
|
||||||
dealer.dealCard(deck.deal());
|
dealer.dealCard(deck.deal());
|
||||||
// TODO deal two cards to the dealer
|
dealer.dealCard(deck.deal());
|
||||||
|
|
||||||
// TODO handle 'insurance' if the dealer's card is an Ace.
|
|
||||||
|
|
||||||
printInitialDeal(players, dealer);
|
printInitialDeal(players, dealer);
|
||||||
|
|
||||||
// TODO if dealer has an ACE, prompt "ANY INSURANCE"
|
if(dealer.getHand().get(0).getValue() == 1) {
|
||||||
// if yes, print "INSURANCE BETS" and prompt each player with "# [x] ? " where X is player number
|
boolean isInsurance = userIo.promptBoolean("ANY INSURANCE");
|
||||||
// insurance bets must be equal or less than half the player's regular bet
|
if(isInsurance) {
|
||||||
|
userIo.println("INSURANCE BETS");
|
||||||
// TODO check for dealer blackjack
|
for(Player player : players) {
|
||||||
// if blackjack, print "DEALER HAS A [x] IN THE HOLE\nFOR BLACKJACK" and skip to evaluateRound
|
while(true) {
|
||||||
// pay 2x insurance bets (insurance bet of 5 pays 10) if applicable
|
double insuranceBet = userIo.promptDouble("# " + player.getPlayerNumber() + " ");
|
||||||
// if not, print "NO DEALER BLACKJACK"
|
// 0 indicates no insurance for that player.
|
||||||
// collect insurance bets if applicable
|
if(insuranceBet >= 0 && insuranceBet <= player.getCurrentBet()) {
|
||||||
|
player.setInsuranceBet(insuranceBet);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(ScoringUtils.scoreHand(dealer.getHand()) == 21) {
|
||||||
|
userIo.println("DEALER HAS A " + dealer.getHand().get(1).toString() + " IN THE HOLE");
|
||||||
|
userIo.println("FOR BLACKJACK");
|
||||||
|
} else {
|
||||||
|
Card dealerFirstCard = dealer.getHand().get(0);
|
||||||
|
if(dealerFirstCard.getValue() == 1 || dealerFirstCard.getValue() > 9) {
|
||||||
|
userIo.println("");
|
||||||
|
userIo.println("NO DEALER BLACKJACK.");
|
||||||
|
} // else dealer blackjack is imposible
|
||||||
for(Player player : players){
|
for(Player player : players){
|
||||||
play(player);
|
play(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
// only play the dealer if at least one player has not busted or gotten a natural blackjack (21 in the first two cards)
|
// TODO only play the dealer if at least one player has not busted or gotten a natural blackjack (21 in the first two cards)
|
||||||
// otherwise, just print the dealer's concealed card
|
// otherwise, just print the dealer's concealed card
|
||||||
dealerHand = playDealer(dealerHand, deck);
|
dealerHand = playDealer(dealerHand, deck);
|
||||||
|
}
|
||||||
|
|
||||||
evaluateRound(players, dealerHand);
|
evaluateRound(players, dealerHand);
|
||||||
}
|
}
|
||||||
@@ -236,6 +251,8 @@ public class Game {
|
|||||||
PLAYER 1 LOSES 100 TOTAL=-100
|
PLAYER 1 LOSES 100 TOTAL=-100
|
||||||
PLAYER 2 WINS 150 TOTAL= 150
|
PLAYER 2 WINS 150 TOTAL= 150
|
||||||
DEALER'S TOTAL= 200
|
DEALER'S TOTAL= 200
|
||||||
|
// In "WINS X TOTAL= Y" the value of 'X' combines normal and insurance bets. e.g. if you bet 100 and get 5 insurance,
|
||||||
|
// then win the hand but lose insurance, it will say "WINS 95"
|
||||||
*/
|
*/
|
||||||
// this should probably take in a "Dealer" instance instead of just the dealer hand so we can update the dealer's total.
|
// this should probably take in a "Dealer" instance instead of just the dealer hand so we can update the dealer's total.
|
||||||
// currentBets of each player are added/subtracted from the dealer total depending on whether they win/lose (accounting for doubling down, insurance etc.)
|
// currentBets of each player are added/subtracted from the dealer total depending on whether they win/lose (accounting for doubling down, insurance etc.)
|
||||||
|
|||||||
@@ -64,6 +64,17 @@ public class Player {
|
|||||||
total = total - currentBet;
|
total = total - currentBet;
|
||||||
currentBet = 0;
|
currentBet = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void recordInsuranceWin() {
|
||||||
|
total = total + (insuranceBet * 2);
|
||||||
|
insuranceBet = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void recordInsuranceLoss() {
|
||||||
|
total = total - insuranceBet;
|
||||||
|
insuranceBet = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the total of all bets won.
|
* Returns the total of all bets won.
|
||||||
* @return Total value
|
* @return Total value
|
||||||
|
|||||||
Reference in New Issue
Block a user