/// <summary> /// Perform update logic related to the component. /// </summary> /// <param name="gameTime">Time elapsed since the last call to /// this method.</param> public override void Update(GameTime gameTime) { if (players.Count > 0) { // If betting is possible if (((BlackjackCardGame)cardGame).State == BlackjackGameState.Betting && !((BlackjackPlayer)players[players.Count - 1]).IsDoneBetting) { int playerIndex = GetCurrentPlayer(); BlackjackPlayer player = (BlackjackPlayer)players[playerIndex]; // If the player is an AI player, have it bet if (player is BlackjackAIPlayer) { ShowAndEnableButtons(false); int bet = ((BlackjackAIPlayer)player).AIBet(); if (bet == 0) { Bet_Click(this, EventArgs.Empty); } else { AddChip(playerIndex, bet, false); } } else { // Reveal the input buttons for a human player and handle input // remember that buttons handle their own imput, so we only check // for input on the chip buttons ShowAndEnableButtons(true); HandleInput(Mouse.GetState()); } } // Once all players are done betting, advance the game to the dealing stage if (((BlackjackPlayer)players[players.Count - 1]).IsDoneBetting) { BlackjackCardGame blackjackGame = ((BlackjackCardGame)cardGame); if (!blackjackGame.CheckForRunningAnimations <AnimatedGameComponent>()) { ShowAndEnableButtons(false); blackjackGame.State = BlackjackGameState.Dealing; Enabled = false; } } } base.Update(gameTime); }
/// <summary> /// Updates the balance of all players in light of their bets and the dealer's /// hand. /// </summary> /// <param name="dealerPlayer">Player object representing the dealer.</param> public void CalculateBalance(BlackjackPlayer dealerPlayer) { for (int playerIndex = 0; playerIndex < players.Count; playerIndex++) { BlackjackPlayer player = (BlackjackPlayer)players[playerIndex]; // Calculate first factor, which represents the amount of the first // hand bet which returns to the player float factor = CalculateFactorForHand(dealerPlayer, player, HandTypes.First); if (player.IsSplit) { // Calculate the return factor for the second hand float factor2 = CalculateFactorForHand(dealerPlayer, player, HandTypes.Second); // Calculate the initial bet performed by the player float initialBet = player.BetAmount / ((player.Double ? 2f : 1f) + (player.SecondDouble ? 2f : 1f)); float bet1 = initialBet * (player.Double ? 2f : 1f); float bet2 = initialBet * (player.SecondDouble ? 2f : 1f); // Update the balance in light of the bets and results player.Balance += bet1 * factor + bet2 * factor2; if (player.IsInsurance && dealerPlayer.BlackJack) { player.Balance += initialBet; } } else { if (player.IsInsurance && dealerPlayer.BlackJack) { player.Balance += player.BetAmount; } // Update the balance in light of the bets and results player.Balance += player.BetAmount * factor; } player.ClearBet(); } }
/// <summary> /// Returns a factor which determines how much of a bet a player should get /// back, according to the outcome of the round. /// </summary> /// <param name="dealerPlayer">The player representing the dealer.</param> /// <param name="player">The player for whom we calculate the factor.</param> /// <param name="currentHand">The hand to calculate the factor for.</param> /// <returns></returns> private float CalculateFactorForHand(BlackjackPlayer dealerPlayer, BlackjackPlayer player, HandTypes currentHand) { float factor; bool blackjack, bust, considerAce; int playerValue; player.CalculateValues(); // Get some player status information according to the desired hand switch (currentHand) { case HandTypes.First: blackjack = player.BlackJack; bust = player.Bust; playerValue = player.FirstValue; considerAce = player.FirstValueConsiderAce; break; case HandTypes.Second: blackjack = player.SecondBlackJack; bust = player.SecondBust; playerValue = player.SecondValue; considerAce = player.SecondValueConsiderAce; break; default: throw new Exception( "Player has an unsupported hand type."); } if (considerAce) { playerValue += 10; } if (bust) { factor = -1; // Bust } else if (dealerPlayer.Bust) { if (blackjack) { factor = 1.5f; // Win BlackJack } else { factor = 1; // Win } } else if (dealerPlayer.BlackJack) { if (blackjack) { factor = 0; // Push BlackJack } else { factor = -1; // Lose BlackJack } } else if (blackjack) { factor = 1.5f; } else { int dealerValue = dealerPlayer.FirstValue; if (dealerPlayer.FirstValueConsiderAce) { dealerValue += 10; } if (playerValue > dealerValue) { factor = 1; // Win } else if (playerValue < dealerValue) { factor = -1; // Lose } else { factor = 0; // Push } } return(factor); }
/// <summary> /// Returns a factor which determines how much of a bet a player should get /// back, according to the outcome of the round. /// </summary> /// <param name="dealerPlayer">The player representing the dealer.</param> /// <param name="player">The player for whom we calculate the factor.</param> /// <param name="currentHand">The hand to calculate the factor for.</param> /// <returns></returns> private float CalculateFactorForHand(BlackjackPlayer dealerPlayer, BlackjackPlayer player, HandTypes currentHand) { float factor; bool blackjack, bust, considerAce; int playerValue; player.CalculateValues(); // Get some player status information according to the desired hand switch (currentHand) { case HandTypes.First: blackjack = player.BlackJack; bust = player.Bust; playerValue = player.FirstValue; considerAce = player.FirstValueConsiderAce; break; case HandTypes.Second: blackjack = player.SecondBlackJack; bust = player.SecondBust; playerValue = player.SecondValue; considerAce = player.SecondValueConsiderAce; break; default: throw new Exception( "Player has an unsupported hand type."); } if (considerAce) { playerValue += 10; } if (bust) { factor = -1; // Bust } else if (dealerPlayer.Bust) { if (blackjack) { factor = 1.5f; // Win BlackJack } else { factor = 1; // Win } } else if (dealerPlayer.BlackJack) { if (blackjack) { factor = 0; // Push BlackJack } else { factor = -1; // Lose BlackJack } } else if (blackjack) { factor = 1.5f; } else { int dealerValue = dealerPlayer.FirstValue; if (dealerPlayer.FirstValueConsiderAce) { dealerValue += 10; } if (playerValue > dealerValue) { factor = 1; // Win } else if (playerValue < dealerValue) { factor = -1; // Lose } else { factor = 0; // Push } } return factor; }