AddCard() public method

public AddCard ( Card c ) : void
c Card
return void
Exemplo n.º 1
0
        /// <summary>
        /// 追加牌
        /// </summary>
        public void GiveAdditionalCard(Hand hand)
        {
            if (this.cards.Count < 1)
            {
                throw new InvalidOperationException();
            }

            hand.AddCard(this.cards.First());
            this.cards.RemoveAt(0);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 初始发2张牌
        /// </summary>
        public void Deal(Hand hand)
        {
            if (this.cards.Count < 2)
            {
                throw new InvalidOperationException();
            }

            var card = this.cards.First();
            hand.AddCard(card);
            this.cards.Remove(card);

            card = this.cards.First();

            if (hand.IsDealer)
            {
                card.Flip();
            }

            hand.AddCard(card);
            this.cards.Remove(card);
        }
Exemplo n.º 3
0
        public void Deal(Hand hand)
        {
            if (this.cards.Count < 2)
            {
                // TODO: Add a descriptive error message
                throw new InvalidOperationException();
            }

            var card = this.cards.First();
            hand.AddCard(card);
            this.cards.Remove(card);

            card = this.cards.First();

            if (hand.IsDealer)
            {
                card.Flip();
            }

            hand.AddCard(card);
            this.cards.Remove(card);
        }
Exemplo n.º 4
0
 public void ReceiveCard(Card card, Hand hand)
 {
     hand.AddCard(card);
 }
Exemplo n.º 5
0
        public void PlayRound()
        {
            GetPlayerBets();

            DealHands();

            // Test for insurance
            var dealerIsShowingAce = _dealerHand.Cards[0].IsAce;

            if (dealerIsShowingAce && Prompt.ForYesNo("Any insurance?"))
            {
                Console.WriteLine("Insurance bets");
                var insuranceBets = new int[_numberOfPlayers];
                foreach (var player in _players)
                {
                    insuranceBets[player.Index] = Prompt.ForInteger($"# {player.Index + 1} ?", 0, player.RoundBet / 2);
                }

                var insuranceEffectMultiplier = _dealerHand.IsBlackjack ? 2 : -1;
                foreach (var player in _players)
                {
                    player.RoundWinnings += insuranceBets[player.Index] * insuranceEffectMultiplier;
                }
            }

            // Test for dealer blackjack
            var concealedCard = _dealerHand.Cards[0];

            if (_dealerHand.IsBlackjack)
            {
                Console.WriteLine();
                Console.WriteLine("Dealer has {0} {1} in the hole for blackjack.", concealedCard.IndefiniteArticle, concealedCard.Name);
                return;
            }
            else if (dealerIsShowingAce)
            {
                Console.WriteLine();
                Console.WriteLine("No dealer blackjack.");
            }

            foreach (var player in _players)
            {
                PlayHand(player);
            }

            // Dealer hand
            var allPlayersBusted = _players.All(p => p.Hand.IsBusted && (!p.SecondHand.Exists || p.SecondHand.IsBusted));

            if (allPlayersBusted)
            {
                Console.WriteLine("Dealer had {0} {1} concealed.", concealedCard.IndefiniteArticle, concealedCard.Name);
            }
            else
            {
                Console.WriteLine("Dealer has {0} {1} concealed for a total of {2}", concealedCard.IndefiniteArticle, concealedCard.Name, _dealerHand.Total);
                if (_dealerHand.Total < 17)
                {
                    Console.Write("Draws");
                    while (_dealerHand.Total < 17)
                    {
                        var card = _dealerHand.AddCard(_deck.DrawCard());
                        Console.Write("  {0}", card.Name);
                    }
                    if (_dealerHand.IsBusted)
                    {
                        Console.WriteLine("  ...Busted");
                    }
                    else
                    {
                        Console.WriteLine("  ---Total is {0}", _dealerHand.Total);
                    }
                }
            }
        }
        static void endScene(ref Deck baseDeck, ref Hand playerHand, ref Hand dealerHand, ref bool playerCondition, ref bool firstTurn, string[] emptyArray)
        {
            bool madeChoice = false;

            //Reset the hands and deck then shuffle.
            baseDeck = new Deck();
            baseDeck.Shuffle();
            playerHand.resetHand();
            dealerHand.resetHand();
            Console.Write("\n");

            while (!madeChoice)
            {
                Console.Write("Type 1 to ");
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("play again");
                Console.ResetColor();
                Console.Write("Type 2 to ");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("show statistics");
                Console.ResetColor();
                Console.Write("Type 3 to ");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("head back to the menu");
                Console.ResetColor();

                ReadChoice("Choice: ", emptyArray, out int selection);
                //Play Again
                if (selection == 1)
                {
                    for (int ndx = 0; ndx < 2; ndx++)
                    {
                        playerHand.AddCard(baseDeck.Draw());
                        dealerHand.AddCard(baseDeck.Draw());
                    }
                    firstTurn  = true;
                    madeChoice = true;
                }
                //Show Statistics
                else if (selection == 2)
                {
                    displayStats();
                }
                //Quit
                else if (selection == 3)
                {
                    playerCondition = false;
                    firstTurn       = true;
                    madeChoice      = true;
                    Console.WriteLine("\nWould you like to reset the statistics?\n\n1. Reset\n2. Continue");
                    bool validChoice = false;
                    while (!validChoice)
                    {
                        ReadChoice("Choice: ", emptyArray, out int resetOption);
                        if (resetOption == 1)
                        {
                            winCount       = 0;
                            stalemateCount = 0;
                            lossCount      = 0;
                            gamesPlayed    = 0;
                            validChoice    = true;
                        }
                        else if (resetOption == 2)
                        {
                            validChoice = true;
                        }
                        else
                        {
                            Console.WriteLine("Please input 1 or 2");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("You input an invalid selection.");
                }
            }
        }
        static void Main(string[] args)
        {
            //Allows for printing of UTF8 encodec.
            Console.OutputEncoding = Encoding.UTF8;
            string[] menuOptions = { "1. Play Blackjack", "2. Shuffle and Show Deck", "3. Exit" };
            int      userChoice  = 0;

            bool willExit = false;

            while (!willExit)
            {
                Deck baseDeck   = new Deck();
                Hand playerHand = new Hand();
                Hand dealerHand = new Hand();

                //Shuffle the deck before it plays.
                baseDeck.Shuffle();
                //Welcome Message
                Console.Write("Welcome to the ");
                displayTitle();
                ReadChoice("Choice: ", menuOptions, out userChoice);

                //Play game
                if (userChoice == 1)
                {
                    bool playerCondition = true;
                    bool firstTurn       = true;
                    bool dealerInitilize = true;
                    bool dealerPlay      = false;

                    Console.Clear();
                    //Start blackjack by dealing two cards to each the dealer and player.
                    for (int ndx = 0; ndx < 2; ndx++)
                    {
                        playerHand.AddCard(baseDeck.Draw());
                        dealerHand.AddCard(baseDeck.Draw());
                    }

                    //DrawGame(playerHand, dealerHand, baseDeck, dealerInitilize);
                    string[] emptyArray = new string[] { string.Empty };
                    while (playerCondition)
                    {
                        Console.Clear();
                        DrawGame(playerHand, dealerHand, dealerInitilize, dealerPlay);
                        if ((dealerHand.Score == 21 || playerHand.Score == 21) && firstTurn)
                        {
                            dealerPlay      = true;
                            dealerInitilize = false;
                            Console.Clear();
                            DrawGame(playerHand, dealerHand, dealerInitilize, dealerPlay);
                            if (playerHand.Score == 21 && dealerHand.Score == 21)
                            {
                                stalemate("The dealer and player have 21.");
                            }
                            else if (dealerHand.Score == 21)
                            {
                                playerLost("Dealer has 21");
                            }
                            else if (playerHand.Score == 21)
                            {
                                playerWon("Player has 21");
                            }
                            dealerInitilize = true;
                            endScene(ref baseDeck, ref playerHand, ref dealerHand, ref playerCondition, ref firstTurn, emptyArray);
                        }
                        else if (playerHand.Score < 21)
                        {
                            ReadChoice("Choice: ", emptyArray, out int gameplayChoice);
                            //Hitting
                            if (gameplayChoice == 1)
                            {
                                playerHand.AddCard(baseDeck.Draw());
                                firstTurn = false;
                            }
                            //Standing
                            else if (gameplayChoice == 2)
                            {
                                dealerPlay = true;

                                dealerPlayTurn(playerHand, dealerHand, baseDeck);
                                firstTurn = false;
                                endScene(ref baseDeck, ref playerHand, ref dealerHand, ref playerCondition, ref firstTurn, emptyArray);
                            }
                            else
                            {
                                Console.WriteLine("Input 1 or 2.");
                            }
                        }
                        else if (playerHand.Score == 21 && !firstTurn)
                        {
                            Console.WriteLine("21 Attained, standing.");
                            dealerPlay = true;

                            dealerPlayTurn(playerHand, dealerHand, baseDeck);
                            endScene(ref baseDeck, ref playerHand, ref dealerHand, ref playerCondition, ref firstTurn, emptyArray);
                        }
                        else
                        {
                            playerLost("Player has busted!");
                            endScene(ref baseDeck, ref playerHand, ref dealerHand, ref playerCondition, ref firstTurn, emptyArray);
                        }
                    }
                }
                //Shuffle
                else if (userChoice == 2)
                {
                    Console.WriteLine("Shuffled, showing deck.");
                    baseDeck.Shuffle();

                    baseDeck.Display();

                    Console.ReadKey();
                }
                //Exit
                else if (userChoice == 3)
                {
                    willExit = true;
                }
                else
                {
                    Console.Write("You input an invalid solution, try again.");
                }
                Console.Clear();
            }
        }
Exemplo n.º 8
0
        public void GiveAdditionalCard(Hand hand)
        {
            if (this.cards.Count < 1)
            {
                // TODO: Add a descriptive error message
                throw new InvalidOperationException();
            }

            hand.AddCard(this.cards.First());
            this.cards.RemoveAt(0);
        }