示例#1
0
        static void Main()
        {
            Console.WriteLine("==-== BlackJack ==-==");
            Console.WriteLine();

            uint playerCount       = GetPlayerCount();
            bool humanParticipates = ConsoleInteractions.Confirm("Will you particiate in game? [y/n]: ");

            var players = new List <AbstractPlayer>();

            uint nonHumanplayers;

            if (humanParticipates)
            {
                players.Add(new HumanPlayer(InitialMoney));
                nonHumanplayers = playerCount - 1;
            }

            else
            {
                nonHumanplayers = playerCount;
            }

            var random = new Random();

            for (int i = 0; i < nonHumanplayers; i++)
            {
                // Can possibly support more bots
                switch (random.Next(1, 3))
                {
                case 1:
                    int number = players.OfType <SameAsDealerBotPlayer>().Count();
                    players.Add(new SameAsDealerBotPlayer(InitialMoney, $"Same-as-dealer bot{(number == 0 ? "" : $" #{number + 1}")}"));
                    break;
示例#2
0
        static void PlayGame()
        {
            Console.WriteLine("==-== BlackJack ==-==");
            Console.WriteLine();

            uint playerCount       = GetPlayerCount();
            bool humanParticipates = ConsoleInteractions.Confirm("Will you particiate in game? [y/n]: ");

            var players = new List <AbstractPlayer>();

            uint nonHumanplayers;

            if (humanParticipates)
            {
                var humanPlayer = Container.Resolve <AbstractPlayer>("HumanPlayer");
                players.Add(humanPlayer);
                nonHumanplayers = playerCount - 1;
            }

            else
            {
                nonHumanplayers = playerCount;
            }

            var random = new Random();

            for (int i = 0; i < nonHumanplayers; i++)
            {
                int            count;
                string         name;
                AbstractPlayer player;
                // Can possibly support more bots
                switch (random.Next(1, 3))
                {
                case 1:
                    count = players.OfType <SameAsDealerBotPlayer>().Count();
                    name  = $"Same-as-dealer bot{(count == 0 ? "" : $" #{count + 1}")}";
                    Container.RegisterInstance("BotName", name);
                    player = Container.Resolve <AbstractPlayer>("SameAsDealerBotPlayer");
                    players.Add(player);
                    break;
示例#3
0
        internal static void Round(IList <AbstractPlayer> players, IList <Card> deck, int roundNumber,
                                   IList <AbstractPlayer> lost)
        {
            int i = 0;

            while (i < players.Count)
            {
                if (players[i].Money == 0)
                {
                    lost.Add(players[i]);
                    players.RemoveAt(i);
                }
                else
                {
                    i++;
                }
            }

            if (!players.Any())
            {
                return;
            }

            // Following formula ensures that players
            // can pick as many cards from the deck as they want
            if (deck.Count < (players.Count + 1) * 11)
            {
                deck = ShuffleMachine.GetShuffledDecks(8);
            }

            Console.WriteLine($"==-== Round {roundNumber} ==-==");
            if (lost.Any())
            {
                ConsoleInteractions.WriteList(lost, "(");
                Console.WriteLine(" have lost all their money)");
            }

            Console.WriteLine();

            var bets = GetInitialBets(players);

            Console.WriteLine();

            var hands  = GetHands(players, bets, deck);
            var dealer = new Dealer(deck);

            Console.WriteLine($"Dealer's card is {dealer.FirstCard} (score: {dealer.FirstCard.Score()})");
            Console.WriteLine();

            i = 0;
            while (i < hands.Count)
            {
                Console.WriteLine(hands[i].Owner.Name == "You" ? "Your turn!" : $"Turn of {hands[i].Owner.Name}.");
                if (!PerformActions(hands, deck, i, dealer))
                {
                    hands.RemoveAt(i);
                }
                else
                {
                    i++;
                }

                ConsoleInteractions.PressAnyKey();
            }

            Console.WriteLine();
            Console.WriteLine("Dealer collects cards:");

            dealer.TakeEnoughCards(deck);

            dealer.WriteCards();
            Console.WriteLine($" ({dealer.Score()})");
            Console.WriteLine();

            if (dealer.Score() > 21)
            {
                Console.WriteLine("Dealer lost!");
                foreach (var hand in hands)
                {
                    if (CardUtils.GetScore(hand.Cards) == 21 && hand.Cards.Count == 2)
                    {
                        Console.WriteLine(
                            $"{hand.Owner.Name} {(hand.Owner.Name == "You" ? "have" : "has")} blackjack!");
                        Console.WriteLine(
                            $"{hand.Owner.Name} {(hand.Owner.Name == "You" ? "get" : "gets")} repaid 3:2!");
                        hand.Owner.GiveMoney((int)hand.InitialBet * 5 / 2);
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine(
                            $"{hand.Owner.Name} {(hand.Owner.Name == "You" ? "get" : "gets")} repaid 1:1!");
                        hand.Owner.GiveMoney((int)hand.InitialBet * 2);
                        Console.WriteLine();
                    }
                }
            }
            else
            {
                foreach (var hand in hands)
                {
                    uint score = CardUtils.GetScore(hand.Cards);

                    if (score == dealer.Score())
                    {
                        Console.WriteLine("{0} {1} equal score with dealer. {2}$ bet is returned.", hand.Owner.Name,
                                          hand.Owner.Name == "You" ? "have" : "has", hand.InitialBet);
                        hand.Owner.GiveMoney((int)hand.InitialBet);
                        Console.WriteLine();
                    }
                    else if (score > dealer.Score())
                    {
                        Console.WriteLine(
                            $"{hand.Owner.Name} {(hand.Owner.Name == "You" ? "beat" : "beats")} the dealer!");
                        Console.WriteLine(
                            $"{hand.Owner.Name} {(hand.Owner.Name == "You" ? "get" : "gets")} repaid 1:1!");
                        hand.Owner.GiveMoney((int)hand.InitialBet * 2);
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine($"{hand.Owner.Name} lost.");
                        Console.WriteLine();
                    }
                }
            }

            ConsoleInteractions.PressAnyKey();
            Round(players, deck, roundNumber + 1, lost);
        }