Example #1
0
        static void DealHand()
        {
            if (deck.GetAmountOfRemainingCrads() < 20)
            {
                deck.Initialize();
                deck.Shuffle();
            }

            Console.WriteLine("Remaining Cards: {0}", deck.GetAmountOfRemainingCrads());
            Console.WriteLine("Current Chips: {0}", chips);
            Console.WriteLine("How much would you like to bet? (1 - {0})", chips);
            string input = Console.ReadLine().Trim().Replace(" ", "");
            int    betAmount;

            while (!Int32.TryParse(input, out betAmount) || betAmount < 1 || betAmount > chips)
            {
                Console.WriteLine("amount is illegal. How much would you like to bet? (1 - {0})", chips);
                input = Console.ReadLine().Trim().Replace(" ", "");
            }
            Console.WriteLine();

            userHand = new List <Card>();
            userHand.Add(deck.DrawACard());
            userHand.Add(deck.DrawACard());

            foreach (Card card in userHand)
            {
                if (card.Face == Face.Ace)
                {
                    card.Value += 10;
                    break;
                }
            }

            Console.WriteLine("[Player]");
            Console.WriteLine("Card 1: {0} of {1}", userHand[0].Face, userHand[0].Suit);
            Console.WriteLine("Card 2: {0} of {1}", userHand[1].Face, userHand[1].Suit);
            Console.WriteLine("Total: {0}\n", userHand[0].Value + userHand[1].Value);

            dealerHand = new List <Card>();
            dealerHand.Add(deck.DrawACard());
            dealerHand.Add(deck.DrawACard());

            foreach (Card card in dealerHand)
            {
                if (card.Face == Face.Ace)
                {
                    card.Value += 10;
                    break;
                }
            }

            Console.WriteLine("[Delaer]");
            Console.WriteLine("Card 1: {0} of {1}", dealerHand[0].Face, dealerHand[1].Suit);
            Console.WriteLine("Card 2: [Hole Card]");
            Console.WriteLine("Total: {0}\n", dealerHand[0].Value);

            bool insurance = false;;

            if (dealerHand[0].Face == Face.Ace)
            {
                Console.WriteLine("Insurance? (y / n)");
                string userInput = Console.ReadLine();

                while (userInput != "y" && userInput != "n")
                {
                    Console.WriteLine("Could not understand. Insurance? (y / n)");
                    userInput = Console.ReadLine();
                }

                if (userInput == "y")
                {
                    insurance = true;
                    //chips -= betAmount / 2;
                    Console.WriteLine("\n[Insurance Accepted!]\n");
                }
                else
                {
                    insurance = false;
                    Console.WriteLine("\n[Insurance Rejected]\n");
                }
            }

            if (dealerHand[0].Face == Face.Ace || dealerHand[0].Value == 10)
            {
                Console.WriteLine("Delaer checks if he has blackjack...\n");
                Thread.Sleep(2000);
                if (dealerHand[0].Value + dealerHand[1].Value == 21)
                {
                    Console.WriteLine("[Delaer]");
                    Console.WriteLine("Card 1: {0} of {1}", dealerHand[0].Face, dealerHand[1].Suit);
                    Console.WriteLine("Card 2: {0} of {1}", dealerHand[1].Face, dealerHand[1].Suit);
                    Console.WriteLine("Total: {0}\n", dealerHand[0].Value + dealerHand[1].Value);

                    Thread.Sleep(2000);

                    int amountLost = 0;

                    if (userHand[0].Value + userHand[1].Value == 21 && insurance)
                    {
                        amountLost = betAmount / 2;
                        chips     -= betAmount / 2;
                    }
                    else if (userHand[0].Value + userHand[1].Value != 21 && !insurance)
                    {
                        amountLost = betAmount + betAmount / 2;
                        chips     -= betAmount + betAmount / 2;
                    }

                    Console.WriteLine("You lost {0} chips", amountLost);

                    Thread.Sleep(1000);

                    return;
                }
                else
                {
                    Console.WriteLine("Dealer does not have a blackjack, moving on...\n");
                }
            }

            if (userHand[0].Value + userHand[1].Value == 21)
            {
                Console.WriteLine("Blackjack, You Won! ({0} chips)\n", betAmount + betAmount / 2);
                chips += betAmount + betAmount / 2;
                return;
            }

            do
            {
                Console.WriteLine("Please choose a valid option: [(S)tand (H)it]");
                ConsoleKeyInfo userOption = Console.ReadKey(true);
                while (userOption.Key != ConsoleKey.H && userOption.Key != ConsoleKey.S)
                {
                    Console.WriteLine("illegal key. Please choose a valid option: [(S)tand (H)it]");
                    userOption = Console.ReadKey(true);
                }
                Console.WriteLine();

                switch (userOption.Key)
                {
                case ConsoleKey.H:
                    userHand.Add(deck.DrawACard());
                    Console.WriteLine("Hitted {0} of {1}", userHand[userHand.Count - 1].Face, userHand[userHand.Count - 1].Suit);
                    int totalCardsValue = 0;
                    foreach (Card card in userHand)
                    {
                        totalCardsValue += card.Value;
                    }
                    Console.WriteLine("Total cards value now: {0}\n", totalCardsValue);
                    if (totalCardsValue > 21)
                    {
                        Console.Write("Busted!\n");
                        chips -= betAmount;
                        Thread.Sleep(2000);
                        return;
                    }
                    else if (totalCardsValue == 21)
                    {
                        Console.WriteLine("Good job! I assume you want to stand from now on...\n");
                        Thread.Sleep(2000);
                        continue;
                    }
                    else
                    {
                        continue;
                    }
                    break;

                case ConsoleKey.S:

                    Console.WriteLine("[Delaer]");
                    Console.WriteLine("Card 1: {0} of {1}", dealerHand[0].Face, dealerHand[1].Suit);
                    Console.WriteLine("Card 2: {0} of {1}", dealerHand[1].Face, dealerHand[1].Suit);

                    int dealerCardsValue = 0;
                    foreach (Card card in dealerHand)
                    {
                        dealerCardsValue += card.Value;
                    }

                    while (dealerCardsValue < 17)
                    {
                        Thread.Sleep(2000);
                        dealerHand.Add(deck.DrawACard());
                        dealerCardsValue = 0;
                        foreach (Card card in dealerHand)
                        {
                            dealerCardsValue += card.Value;
                        }
                        Console.WriteLine("Card {0}: {1} of {2}", dealerHand.Count, dealerHand[dealerHand.Count - 1].Face, dealerHand[dealerHand.Count - 1].Suit);
                    }
                    dealerCardsValue = 0;
                    foreach (Card card in dealerHand)
                    {
                        dealerCardsValue += card.Value;
                    }
                    Console.WriteLine("Total: {0}\n", dealerCardsValue);

                    if (dealerCardsValue > 21)
                    {
                        Console.WriteLine("Dealer bust! You win! ({0} chips)", betAmount);
                        chips += betAmount;
                        return;
                    }
                    else
                    {
                        int playerCardValue = 0;
                        foreach (Card card in userHand)
                        {
                            playerCardValue += card.Value;
                        }

                        if (dealerCardsValue > playerCardValue)
                        {
                            Console.WriteLine("Dealer has {0} and player has {1}, dealer wins!", dealerCardsValue, playerCardValue);
                            chips -= betAmount;
                            return;
                        }
                        else
                        {
                            Console.WriteLine("Player has {0} and dealer has {1}, player wins!", playerCardValue, dealerCardsValue);
                            chips += betAmount;
                            return;
                        }
                    }
                    break;

                default:
                    break;
                }

                Console.ReadLine();
            }while (true);
        }
        public static void Hand()
        {
            if (deck.GetAmountOfRemeiningCard() < 20)
            {
                deck.Initialize();
            }

            Console.WriteLine("Cards: {0}", deck.GetAmountOfRemeiningCard());


            Console.WriteLine("Your balance {0}, is:  {1}$", user.PlayerName, user.PlayerBalance);
            Console.WriteLine("Your can bet (1 - {0})", user.PlayerBalance);
            String input = Console.ReadLine().Trim().Replace(" ", "");


            while (!Int32.TryParse(input, out betAmount) || betAmount < 1 || betAmount > user.PlayerBalance)
            {
                Console.WriteLine("Input error.\nTry again:");
                input = Console.ReadLine().Trim().Replace(" ", "");
            }

            userHand = new List <AllCards>();
            userHand.Add(deck.DrowACard());
            userHand.Add(deck.DrowACard());

            //  Int32 totalUserCardsValue = 0;
            Boolean acePlayer = true;

            foreach (AllCards card in userHand)//исправить чтоб при получении туза повторно он был равен 1 +
            {
                if (card.Face == Face.Ace && acePlayer)
                {
                    card.Value += 10;
                    acePlayer   = false;
                    break;
                }

                // totalUserCardsValue += card.Value;
            }

            Console.WriteLine("[{0}]", user.PlayerName);
            Console.WriteLine("1 card: {0} {1}", userHand[0].Face, userHand[0].Suit);
            Console.WriteLine("2 card: {0} {1}", userHand[1].Face, userHand[1].Suit);
            Console.WriteLine("Your score: {0}\n", userHand[0].Value + userHand[1].Value);

            dillerHand = new List <AllCards>();
            dillerHand.Add(deck.DrowACard());
            dillerHand.Add(deck.DrowACard());

            // Int32 totalDillerCardValue = 0;
            Boolean aceDiller = true;

            foreach (AllCards card in dillerHand)//исправить чтоб при получении туза повторно он был равен 1 +
            {
                if (card.Face == Face.Ace && aceDiller)
                {
                    card.Value += 10;
                    aceDiller   = false;
                    break;
                }

                // totalDillerCardValue += card.Value;
            }

            Console.WriteLine("[Diller]");
            Console.WriteLine("1 card: {0} {1}", dillerHand[0].Face, dillerHand[0].Suit);
            Console.WriteLine("2 card: {0} {1}", dillerHand[1].Face, dillerHand[1].Suit);
            Console.WriteLine("Diller score: {0}\n", dillerHand[0].Value + dillerHand[1].Value);
            Int32 totalDillerCardValue = dillerHand[0].Value + dillerHand[1].Value;
            Int32 totalUserCardsValue  = userHand[0].Value + userHand[1].Value;

            if (totalDillerCardValue == 21 && totalUserCardsValue == 21)
            {
                Console.WriteLine("The draw");

                user.PlayerBalance += betAmount;
            }
            else if (totalDillerCardValue == 21 && totalUserCardsValue != 21)
            {
                Console.WriteLine("Your lose");

                user.PlayerBalance -= betAmount;
                return;//1
            }
            else if (totalUserCardsValue == 21 && totalDillerCardValue != 21)
            {
                Console.WriteLine("Your win.");

                user.PlayerBalance += betAmount;
                return;//2
            }


            do
            {
                Console.WriteLine("Would you like to take another card ? If yes, press Y, if not press N.");
                ConsoleKeyInfo userChose = Console.ReadKey(true);
                while (userChose.Key != ConsoleKey.Y && userChose.Key != ConsoleKey.N)
                {
                    Console.WriteLine("No command, press again");
                    userChose = Console.ReadKey(true);
                }
                switch (userChose.Key)
                {
                case ConsoleKey.Y:
                    userHand.Add(deck.DrowACard());
                    Console.WriteLine("Your take a card {0} {1}", userHand[userHand.Count - 1].Face, userHand[userHand.Count - 1].Suit);
                    totalUserCardsValue += userHand[userHand.Count - 1].Value;
                    Console.WriteLine("Your score is: {0}\n", totalUserCardsValue);
                    if (totalUserCardsValue > 21)
                    {
                        Console.WriteLine("Your lose\n");
                        user.PlayerBalance -= betAmount;
                        return;
                    }
                    else if (totalUserCardsValue == 21)
                    {
                        Console.WriteLine("Your have Black Jack!");
                        continue;
                    }
                    else
                    {
                        continue;
                    }
                    break;

                case ConsoleKey.N:
                    Console.WriteLine("[Diller]");
                    Console.WriteLine("1 card: {0} {1}", dillerHand[0].Face, dillerHand[0].Suit);
                    Console.WriteLine("2 card: {0} {1}", dillerHand[1].Face, dillerHand[1].Suit);
                    Console.WriteLine("Diller score: {0}\n", totalDillerCardValue);

                    while (totalDillerCardValue < 17)
                    {
                        dillerHand.Add(deck.DrowACard());
                        Console.WriteLine("Diller take a card {0}, {1} {2}", dillerHand.Count, dillerHand[dillerHand.Count - 1].Face, dillerHand[dillerHand.Count - 1].Suit);
                        totalDillerCardValue += dillerHand[dillerHand.Count - 1].Value;
                        Console.WriteLine("Diller score {0}", totalDillerCardValue);

                        if (totalDillerCardValue > 21)
                        {
                            Console.WriteLine("Your win!");
                            user.PlayerBalance += betAmount;
                        }
                        else
                        {
                            if (totalDillerCardValue > totalUserCardsValue)
                            {
                                Console.WriteLine("Diller has {0}, {1} has {2}. Diller win!", totalDillerCardValue, user.PlayerName, totalUserCardsValue);

                                user.PlayerBalance -= betAmount;
                                return;
                            }
                            else if (totalDillerCardValue == totalUserCardsValue)
                            {
                                Console.WriteLine("Diller has {0}, {1} has {2}. Standoff!", totalDillerCardValue, user.PlayerName, totalUserCardsValue);

                                user.PlayerBalance += betAmount;
                                return;
                            }
                            else
                            {
                                Console.WriteLine("Diller has {0}, {1} has {2}. {1} win!", totalDillerCardValue, user.PlayerName, totalUserCardsValue);

                                user.PlayerBalance += betAmount;
                                return;
                            }
                        }
                    }
                    break;

                default:
                    break;
                }
                Console.ReadLine();
            }while(true);
        }