Exemplo n.º 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);
                    }
                }
            }
        }
Exemplo n.º 2
0
        static bool PlayBlackjack()
        {
            Deck deck = new Deck();

            deck.Shuffle();

            //userHand and dealerHand
            BlackjackHand userHand   = new BlackjackHand();
            BlackjackHand dealerHand = new BlackjackHand();

            userHand.AddCard(deck.DealCard());
            userHand.AddCard(deck.DealCard());
            dealerHand.AddCard(deck.DealCard());
            dealerHand.AddCard(deck.DealCard());

            if (dealerHand.GetBlackjackValue() == 21)
            {
                Console.WriteLine("Dealer's cards are: ");
                dealerHand.Display();
                Console.WriteLine("Your cards are: ");
                userHand.Display();
                Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You lose.");
                return(false);
            }
            if (userHand.GetBlackjackValue() == 21)
            {
                Console.WriteLine("Dealer's cards are: ");
                dealerHand.Display();
                Console.WriteLine(" \nYour cards are: ");
                userHand.Display();
                Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You win.");
                return(true);
            }

            string hitOrStand;

            while (true)
            {
                Console.WriteLine(" \nYour cards are: ");
                userHand.Display();
                Console.WriteLine($"Your total is {userHand.GetBlackjackValue()}");
                Console.WriteLine("\nDealer is showing: ");
                dealerHand.Display(dealerHand.GetCard(0));
                Console.WriteLine();

                hitOrStand = GetHitOrStand();
                if (hitOrStand == "S" || hitOrStand == "s")
                {
                    Console.WriteLine("\nUser stands.");
                    break;
                }
                else
                {
                    userHand.AddCard(deck.DealCard());
                    if (userHand.GetBlackjackValue() > 21)
                    {
                        Console.WriteLine("User lose! Your cards are: ");
                        userHand.Display();
                        Console.WriteLine("Dealer's cards are: ");
                        dealerHand.Display();
                        Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You lose.");

                        return(false);
                    }
                }
            }
            Console.WriteLine("Dealer's cards are: ");
            dealerHand.Display();
            Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()}.");

            while (dealerHand.GetBlackjackValue() <= 16)
            {
                Console.WriteLine("Dealer hits and gets a card: ");
                dealerHand.AddCard(deck.DealCard());
                dealerHand.Display(dealerHand.GetCard(dealerHand.GetCardCount() - 1));
                if (dealerHand.GetBlackjackValue() > 21)
                {
                    Console.WriteLine("Dealer busted by going over 21. You win.");
                    return(true);
                }
            }
            if (dealerHand.GetBlackjackValue() >= userHand.GetBlackjackValue())
            {
                Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You lose.");
                return(false);
            }
            else
            {
                Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You win.");
                return(true);
            }
        }