Пример #1
0
        /// <summary>
        /// Used to fill the deck with the cards corresponding to the respective suit
        /// </summary>
        /// <param name="suit"></param>
        /// <param name="deck"></param>
        public void Fill(string suit, Deck deck)
        {
            char suitAbv = DefineSuit(suit);

            for (int i = 2; i < 11; i++)
            {
                deck.AddCard(new Card(suit, suitAbv, i.ToString()));
            }

            deck.AddCard(new Card(suit, suitAbv, "J"));
            deck.AddCard(new Card(suit, suitAbv, "Q"));
            deck.AddCard(new Card(suit, suitAbv, "K"));
            deck.AddCard(new Card(suit, suitAbv, "A"));
        }
Пример #2
0
        private void StartGame()
        {
            scoreP1 = scoreP2 = 0;
            m_rootZ = 0;

            firstHand      = true;
            showP2Score    = false;
            isAskingToDraw = true;
            p1HasDrawn     = false;
            cpuHasStopped  = false;
            winner         = "";

            p1Hand = new CardHand();
            p2Hand = new CardHand();

            m_dataList = new BlackJackDataList();
            bjData     = new BlackJackData();

            // prepare the deck
            m_deck.RemoveAllCards();

            m_allCards = m_allCards_cpy.ToList();
            m_random   = new Random();

            // Source deck
            for (int i = 0; i < m_allCards.Count; i++)
            {
                m_deck.AddCard(GetRandomCard());
                i--;
            }

            // the card under the pile
            m_deck.GetCard(0).setTurned(false);

            m_dataList.AddDeckData(m_deck);

            Card drawn;

            // draw the first 2 cards
            for (int i = 0; i < 2; i++)
            {
                drawn = m_deck.RemoveLast();
                drawn.setTurned(true);
                p1Hand.AddCard(drawn);
                //System.Diagnostics.Debug.WriteLine("Giocatore 1 ha pescato : " + drawn.CardId() + " " + drawn.Suit());

                drawn = m_deck.RemoveLast();
                drawn.setTurned((i % 2 == 0) ? true : false);
                p2Hand.AddCard(drawn);
                //System.Diagnostics.Debug.WriteLine("Avversario ha pescato : " + drawn.CardId() + " " + drawn.Suit());
            }
        }
Пример #3
0
 /// <summary>
 /// Adds a card obtained from the deck to the players hand.
 /// </summary>
 /// <param name="deck"> The deck from which the card will be dealt. </param>
 public void DealCard(Deck deck)
 {
     this.cardsInHand.Add(deck.AddCard());
 }
Пример #4
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();
        }