예제 #1
0
        /**
         * <summary>Method DealerPlaysWithBotDealer uses the decision maker feature in BotPlayer to
         * control how the play goes. At the end, it compares the scores and declares a winner</summary>
         */
        private void DealerPlaysWithBotDealer(Dealer dealer, BotPlayer bot, Player player)
        {
            var table       = dealer.GetTable(); //Play at the same table as the player
            var playerScore = ValueCalculator.HandWorth(bot.GetHand());

            bot.ReceiveScore(playerScore);
            Table.AnnounceScore(bot, false);

            while (bot.DecisionMaker()) //While the bot is still under 17
            {
                dealer.DealCardToPlayer(bot, 1);
                playerScore = ValueCalculator.HandWorth(bot.GetHand());
                bot.ReceiveScore(playerScore);
                table.AnnounceDrawnCard(bot, false);
            }

            if (bot.PlayerScoreIs21())
            {
                table.DealerWins();
            }
            else if (!bot.PlayerScoreUnder21())
            {
                table.DealerBusted();
                table.Congratulations();
            }
            else
            {
                CompareScores(bot, player);
            }
        }
예제 #2
0
        /**
         * <summary>This method takes in a players hand, calculate the worth of the hand and returns it.</summary>
         * <param name="hand">List of cards representing the players hand</param>
         * <returns>An integer value representing the worth</returns>
         */
        public static int HandWorth(List <Card> hand)
        {
            var handWorthWithoutAceChange = 0;
            var aceCount = 0;

            //Work out the total worth in hand without changing ace values (ace default value is 1)
            try
            {
                foreach (var card in hand)
                {
                    var value = ValueCalculator.CardWorth(card);
                    if (value == 1)
                    {
                        aceCount++;
                        handWorthWithoutAceChange += value;
                    }
                    else
                    {
                        handWorthWithoutAceChange += value;
                    }
                }
            } catch (NullReferenceException) {}


            //If there is at least one ace, see if the worth of the ace(s) should change
            if (aceCount > 0)
            {
                handWorthWithoutAceChange = AceChange(aceCount, handWorthWithoutAceChange);
            }

            return(handWorthWithoutAceChange);
        }
예제 #3
0
        /**
         * <summary>GamePlay is the main flow of the game. It instructs what the dealer should do and
         * calls utility classes InputValidator and ValueCalculator to check at points in the game</summary>
         */
        private void GamePlay(Dealer dealer, Player player, BotPlayer bot)
        {
            var table = dealer.GetTable();

            //While player has not won or busted
            while (player.PlayerScoreUnder21())
            {
                var playerScore = ValueCalculator.HandWorth(player.GetHand()); //Get the player's current score
                player.ReceiveScore(playerScore);
                Table.AnnounceScore(player, true);                             //Print to console

                if (player.PlayerScoreIs21())
                {
                    break;
                }

                if (player.PlayerScoreUnder21())
                {
                    var hitOrStay = dealer.AskHitOrStay();
                    if (hitOrStay == 1) // Hit = 1
                    {
                        dealer.DealCardToPlayer(player, 1);
                        table.AnnounceDrawnCard(player, true);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (player.PlayerScoreIs21())
            {
                table.CongratulationsBlackJack();
            }
            else if (!player.PlayerScoreUnder21())
            {
                table.Busted();
            }
            else
            {
                //If player chose to stay, then bot player must face the player
                DealerPlaysWithBotDealer(dealer, bot, player);
            }
            //Ask to reset the game at the end
            Program.ResetGame();
        }