Example #1
0
        static void Main(string[] args)
        {
            Deck        c           = new Deck();
            GameMethods gameMethods = new GameMethods();

            Deck[] cards = c.InitializationDeck();
            Player human = new Player(PlayersName.Player, 0, 0, true);
            Player ai    = new Player(PlayersName.Bot_Vanya, 0, 0, true);

            bool restart;

            do
            {
                c.DeckMixed(cards);
                Console.Clear();
                int playerCardValue = 0;
                int aiCardValue     = 0;
                human.Answer = true;
                ai.Answer    = true;

                Console.WriteLine("The deck is ready!\n");

                var firstPlayer = gameMethods.WhoGoesFirst() % 2 == 0;
                if (firstPlayer)
                {
                    Console.WriteLine("The first player to receive a card (YOU)"); //Первым получает карты Игрок(ТЫ)
                    Console.WriteLine("The second receives AI cards(COMPUTER)\n");
                }
                else
                {
                    Console.WriteLine("The first to receive AI cards (COMPUTER)"); //Первым получает карты ИИ(КОМПЬЮТЕР)
                    Console.WriteLine("The second player receives the card(YOU)\n");
                }

                // Раздаем по 2 карты в соответствии с очередью кто первый должен был их получить

                int lastIndexCard = 0;
                if (firstPlayer)
                {
                    human.PrintCardRecipient();
                    //human.NewValueCard = c.GiveTwoCard(cards, lastIndexCard, out lastIndexCard);
                    human.NewValueCard = c.GiveTwoCard(cards, lastIndexCard);
                    playerCardValue   += human.NewValueCard;
                    lastIndexCard     += 2;

                    ai.PrintCardRecipient();
                    ai.NewValueCard = c.GiveTwoCard(cards, lastIndexCard);
                    aiCardValue    += ai.NewValueCard;
                    lastIndexCard  += 2;
                }
                else
                {
                    ai.PrintCardRecipient();
                    ai.NewValueCard = c.GiveTwoCard(cards, lastIndexCard);
                    aiCardValue    += ai.NewValueCard;
                    lastIndexCard  += 2;

                    human.PrintCardRecipient();
                    human.NewValueCard = c.GiveTwoCard(cards, lastIndexCard);
                    playerCardValue   += human.NewValueCard;
                    lastIndexCard     += 2;
                }

                Console.WriteLine($"Summa {human.Name} : {playerCardValue}");
                Console.WriteLine($"Summa {ai.Name} : {aiCardValue}\n");

                if (playerCardValue == 22 || playerCardValue == 21)
                {
                    Console.WriteLine($"{human.Name} WIN , him score = {playerCardValue}\n");
                    Console.WriteLine($"END GAME\n");
                    human.UpdateNumOfVic();
                    restart = gameMethods.RestartGame();
                    continue;
                }
                else if (aiCardValue == 22 || aiCardValue == 21)
                {
                    Console.WriteLine($"{ai.Name} WIN , him score = {aiCardValue}\n");
                    Console.WriteLine($"END GAME\n");
                    ai.UpdateNumOfVic();
                    restart = gameMethods.RestartGame();
                    continue;
                }
                else
                {
                    Console.WriteLine($"Nobody scored 21, continue the game\n");
                }

                //Начинаем спрашивать не хотят ли игроки взять дополнительные карты

                do
                {
                    //Взависимости кто первый получал карты , тот и начинает первым брать дополнительные карты
                    if (firstPlayer)
                    {
                        if (human.Answer && gameMethods.Question())
                        {
                            human.PrintCardRecipient();
                            human.NewValueCard = c.AddCard(cards, lastIndexCard);
                            playerCardValue   += human.NewValueCard;
                            lastIndexCard++;
                        }
                        else
                        {
                            human.Answer = false;
                        }

                        if (ai.Answer && gameMethods.Question(aiCardValue))
                        {
                            ai.PrintCardRecipient();
                            ai.NewValueCard = c.AddCard(cards, lastIndexCard);
                            aiCardValue    += ai.NewValueCard;
                            lastIndexCard++;
                        }
                        else
                        {
                            ai.Answer = false;
                        }
                    }
                    else
                    {
                        if (ai.Answer && gameMethods.Question(aiCardValue))
                        {
                            ai.PrintCardRecipient();
                            ai.NewValueCard = c.AddCard(cards, lastIndexCard);
                            aiCardValue    += ai.NewValueCard;
                            lastIndexCard++;
                        }
                        else
                        {
                            ai.Answer = false;
                        }

                        if (human.Answer && gameMethods.Question())
                        {
                            human.PrintCardRecipient();
                            human.NewValueCard = c.AddCard(cards, lastIndexCard);
                            playerCardValue   += human.NewValueCard;
                            lastIndexCard++;
                        }
                        else
                        {
                            human.Answer = false;
                        }
                    }
                    Console.WriteLine($"Summa {human.Name} : {playerCardValue}");
                    Console.WriteLine($"Summa {ai.Name} : {aiCardValue}\n");
                }while ((human.Answer || ai.Answer) && (playerCardValue < 21 && aiCardValue < 21));

                Console.WriteLine($"Value calculation...\n");

                if (aiCardValue == playerCardValue)
                {
                    Console.WriteLine($"Draw!!!");
                }
                else if (aiCardValue > 21 && playerCardValue < 21)
                {
                    human.PrintNameWinner();
                    human.UpdateNumOfVic();
                }
                else if (playerCardValue > 21 && aiCardValue < 21)
                {
                    ai.PrintNameWinner();
                    ai.UpdateNumOfVic();
                }
                else if (playerCardValue > 21 && aiCardValue > 21)
                {
                    if (playerCardValue < aiCardValue)
                    {
                        human.PrintNameWinner();
                        human.UpdateNumOfVic();
                    }
                    else
                    {
                        ai.PrintNameWinner();
                        ai.UpdateNumOfVic();
                    }
                }
                else
                {
                    if (playerCardValue > aiCardValue)
                    {
                        human.PrintNameWinner();
                        human.UpdateNumOfVic();
                    }
                    else
                    {
                        ai.PrintNameWinner();
                        ai.UpdateNumOfVic();
                    }
                }
                Console.WriteLine($"END GAME\n");
                restart = gameMethods.RestartGame();
            }while (restart);
            Console.WriteLine($"{human.Name} wins : {human.PrintNumberOfVictories()}\n{ai.Name} wins : {ai.PrintNumberOfVictories()}");
            Console.ReadLine();
        }