private static void GetHandInfo(Hand hand, out string cards, out int value, out int soft) { // Compute hand values. BlackjackRules.ValueOf(hand, out value, out soft); // Make string representing the whole hand. cards = string.Join(", ", hand.Select(s => s.ToString())); }
private void DealerPlay() { // Flag to keep track of blackjack, bust or preferred value. var playing = true; while (playing) { // Get possible out comes. var isBlackjack = BlackjackRules.IsBlackjack(dealer.Hand); var isBust = BlackjackRules.IsBusted(dealer.Hand); var shouldStay = BlackjackRules.ShouldStay(dealer.Hand); // Act according to outcome. if (isBlackjack) { console.WriteDealerInfo("blackjack!"); } else if (isBust) { console.WriteDealerInfo("bust!"); } else if (shouldStay) { console.WriteDealerInfo("staying at my current hand"); } else { // Deal self one card and reveal new hand. console.WriteDealerInfo("dealing myself one"); dealer.DealSelf(); RevealDealerHand(); } playing = !(isBlackjack || isBust || shouldStay); clock.Delay(DelayTime); } console.WriteDealerInfo("my turn is over"); }
private void PlayersPlay() { foreach (var player in playingPlayers) { console.WritePlayerInfo(player.Name, "it's my turn"); // Handle splitting for the player if he prefers to split. var aceSplit = false; if (BlackjackRules.CanSplit(player.PrimaryHand) && player.Wallet.Balance >= bets[player].First().Amount) { AskPlayerSplit(player, out aceSplit); } // Play the actual hands if not ace split. if (aceSplit) { continue; } // Determine hands. var hands = new List <Hand> { player.PrimaryHand }; if (player.IsSplit) { hands.Add(player.SecondaryHand); } // Play the actual hands. for (var i = 0; i < hands.Count; i++) { PlayerPlayHand(player, hands[i], i, hands.Count); } } }
private void EndRound() { // Determine the outcome of player bets. var dealerBlackjack = BlackjackRules.IsBlackjack(dealer.Hand); var dealerBust = BlackjackRules.IsBusted(dealer.Hand); BlackjackRules.ValueOf(dealer.Hand, out var dealerValue, out var dealerSoft); // Check all bets. foreach (var betPair in bets) { var player = betPair.Key; console.WriteDealerInfo($"checking outcome of your hands..."); for (var i = 0; i < betPair.Value.Count; i++) { var bet = betPair.Value[i]; var playerBust = BlackjackRules.IsBusted(bet.Hand); // Write hand before handling. GetHandInfo(bet.Hand, out var cards, out var playerValue, out var playerSoft); console.WriteDealerInfo( $"your hand {i + 1}/{betPair.Value.Count} has cards {cards} " + $"with value {playerValue}/{playerSoft}"); if (playerBust || dealerBlackjack) { // In case player busts or dealer hit's a blackjack, dealer // always wins. console.WriteDealerInfo( $"your hand {i + 1}/{betPair.Value.Count} lost total {bet.Amount}e"); } else { var winAmount = 0u; if (BlackjackRules.IsBlackjack(bet.Hand)) { // Player hit a blackjack. winAmount = bet.Amount * 2u; console.WritePlayerInfo( player.Name, $"hand {i + 1}/{betPair.Value.Count} blackjack wins {winAmount}e"); } else if (!playerBust && dealerBust) { // Dealer bust. winAmount = bet.Amount * 2u; console.WritePlayerInfo( player.Name, $"hand {i + 1}/{betPair.Value.Count} wins {winAmount}e, dealer bust"); } else { if ((playerValue > dealerValue && !BlackjackRules.IsBusted(playerValue)) || (playerSoft > dealerSoft && !BlackjackRules.IsBusted(playerSoft))) { // Player hand value greater than dealers. winAmount = bet.Amount * 2u; console.WritePlayerInfo( player.Name, $"hand {i + 1}/{betPair.Value.Count} wins {winAmount}e"); } else { // Dealers hand value greater than players. console.WritePlayerInfo( player.Name, $"your hand {i + 1}/{betPair.Value.Count} lost total {bet.Amount}e"); } } player.Wallet.Put(winAmount); } clock.Delay(DelayTime); } console.WritePlayerInfo(player.Name, $"my balance now is {player.Wallet.Balance}e"); } // Clear player and game states. foreach (var player in playingPlayers) { bets[player].Clear(); player.Clear(); if (player.Wallet.Empty) { console.WriteDealerInfo($"{player.Name} you are out! " + "come back when you have some money to play!"); activePlayers.Remove(player); absentPlayers.Add(player); } } playingPlayers.Clear(); // Clear dealer state. dealer.Hand.Clear(); }
private void PlayerPlayHand(Player player, Hand hand, int handIndex, int handsCount) { // Flag to keep track of blackjacks, busts and stays. var playing = true; while (playing) { var opts = PlayerOptions.DetermineOps(player, hand); var option = string.Empty; while (!console.TryAskLine($"playing your hand {handIndex + 1}/{handsCount}, what will you do? " + $"({string.Join(", ", opts)})", out option, s => opts.Contains(s))) { console.WriteWarning("invalid operation!"); } switch (option) { case PlayerOptions.OptStay: // Nothing else to do, end turn. console.WriteDealerInfo("staying, your turn is over"); playing = false; break; case PlayerOptions.OptHit: // Deal one card and reveal the hand to player. console.WriteDealerInfo("dealing one card..."); dealer.Deal(hand); RevealPlayerCards(player, hand); if (BlackjackRules.IsBlackjack(hand)) { // Handle blackjack. console.WriteDealerInfo("blackjack! your turn is over"); playing = false; } else if (BlackjackRules.IsBusted(hand)) { // Handle bust. console.WriteDealerInfo("busted! sorry, your hand is out"); playing = false; } break; case PlayerOptions.OptDouble: var bet = bets[player].First(); // Do not allow doubling if the player has not enough balance. if (player.Wallet.Balance < bet.Amount) { console.WriteDealerInfo("sorry, you do not have enough balance to double"); } else { // Do the actual doubling. console.WriteDealerInfo("doubling your hand..."); // Deal single card as of doubling. dealer.Deal(hand); // Take double amount from wallet. player.Wallet.Take(bet.Amount); // Create new bet in place of the current one, we can // always assume there is only one bet in play when // the player is doubling because casino does not allow // doubling split hands. var newBet = new PlayerBet(hand, bet.Amount * 2u); bets[player] = new List <PlayerBet> { newBet }; console.WriteDealerInfo($"your total bet is now {newBet.Amount}e " + $"and your total balance is now {player.Wallet.Balance}e"); RevealPlayerCards(player, hand); playing = false; } break; default: console.WriteWarning("sorry i could not understand you so i assume you are staying..."); playing = false; break; } clock.Delay(DelayTime); } }