Exemplo n.º 1
0
        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()));
        }
Exemplo n.º 2
0
        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();
        }