Example #1
0
        static bool PlayBlackjack()
        {
            var deck = new Deck();

            deck.Shuffle();

            var dealerHand = new BlackjackHand();
            var userHand   = new BlackjackHand();

            dealerHand.AddCard(deck.DealCard());
            dealerHand.AddCard(deck.DealCard());
            Console.WriteLine("\nDealer's face card is " + dealerHand.GetCard(0));

            userHand.AddCard(deck.DealCard());
            userHand.AddCard(deck.DealCard());
            Console.Write("\nUser has" + " " + userHand.GetBlackjackValue() + "\n");
            userHand.DisplayCards();

            if (userHand.GetBlackjackValue() == 21)
            {
                Console.WriteLine("\nCongratulations you win!");
                userHand.Clear();
                return(true);
            }
            else if (dealerHand.GetBlackjackValue() == 21)
            {
                Console.WriteLine("\n You Lose");
                userHand.Clear();
                return(false);
            }

            while (true)
            {
                if (userHand.GetBlackjackValue() < 21)
                {
                    int choice = GetChoice();
                    switch (choice)
                    {
                    case 1:
                    {
                        userHand.AddCard(deck.DealCard());
                        userHand.DisplayCards();
                        break;
                    }

                    case 2:
                    {
                        Console.Write("\n" + userHand.GetBlackjackValue());
                        break;
                    }

                    default:
                    {
                        Console.WriteLine("Please enter a valid choice");
                        break;
                    }
                    }
                }
                else
                {
                    Console.WriteLine("You lose!");
                    Console.WriteLine(dealerHand.GetBlackjackValue());
                    return(false);
                }
                // Console.WriteLine("\nDealer has " + dealerHand.GetBlackjackValue() + "/21");
                while (dealerHand.GetBlackjackValue() <= 16)
                {
                    dealerHand.AddCard(deck.DealCard());
                    // Console.Write("\nDealer has" + " " + dealerHand.GetCardCount() + " ");

                    if (dealerHand.GetBlackjackValue() > 21)
                    {
                        Console.WriteLine("\nDealer busts... User wins!");
                        Console.WriteLine("\nDealer has " + dealerHand.GetBlackjackValue() + "/21");
                        return(true);
                    }
                    else if (dealerHand.GetBlackjackValue() >= userHand.GetBlackjackValue())
                    {
                        Console.WriteLine("\nDealer Wins!");
                        Console.WriteLine("\nDealer has " + dealerHand.GetBlackjackValue() + "/21");
                        return(false);
                    }
                    else if (userHand.GetBlackjackValue() >= dealerHand.GetBlackjackValue())
                    {
                        Console.WriteLine("\nUser wins!");
                        Console.WriteLine("\nDealer has " + dealerHand.GetBlackjackValue() + "/21");
                        return(true);
                    }
                }
            }
        }