簡體   English   中英

我的程序在運行一段時間后遇到了 Stack-overflow 問題。 它在一段時間內工作正常,直到它不工作

[英]My program runs into a Stack-overflow issue after some time of running. It works fine for sometime until it doesn't

我做了一個小控制台 BlackJack 游戲。 這是我使用 C# 時的第一款游戲。游戲在幾輪內運行良好,直到我收到“Stackoverflow”的錯誤消息。 當我查看錯誤時,我發現在 DealerCardGenerator 和 PlayerCardGenerator 中,變量“con”的值是 null。 我不明白這是為什么。 任何幫助將非常感激。

我不知道如何解決這個問題。

這是代碼:

using System;
using System.Collections.Generic;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {

            bool displayMenu = true;
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("How much money: ");
            int moneyRes = Convert.ToInt32(Console.ReadLine());

            playerMoney.TotalMoney += moneyRes;


            while (displayMenu)
            {
                displayMenu = MainMenu();
            }



            Console.ReadLine();

        }



        private static bool MainMenu()
        {
            if (playerMoney.TotalMoney == 0)
            {
                Console.WriteLine("You have no money left. Do you want to insert more?");
                Console.WriteLine("1) Yes");
                Console.WriteLine("Enter any key to quit");

                string addMoney = Console.ReadLine();

                switch (addMoney)
                {
                    case "1":
                        {
                            Console.Write("How much money: ");
                            int moneyRes = Convert.ToInt32(Console.ReadLine());
                            playerMoney.TotalMoney += moneyRes;
                            break;
                        }

                    default:
                        Environment.Exit(0);
                        break;
                }
            }
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("1) Continue Playing");
            Console.WriteLine("Enter any character to quit");
            Console.ForegroundColor = ConsoleColor.White;
            string continuePlaying = Console.ReadLine();

            if (continuePlaying == "1")
            {
                playerMoney.Bet = 0;
                totalDealerValue = 0;
                totalPlayerValue = 0;
                dealerNumber = 0;
                playerNumber = 0;
                playerCards.Clear();
                dealerCards.Clear();
                dealerShownCards.Clear();
                playerShownCards.Clear();



                for (int i = 0; i < 2; i++)
                {
                    PlayerCardGenerator(playerCards);
                    DealerCardGenerator(dealerCards);
                }

                for (int i = 0; i < 2; i++)
                {
                    string conPlayer = String.Concat(playerCards[i].CardType + " ", playerCards[i].CardValue);
                    playerShownCards.Add(conPlayer);
                    playerNumber++;

                }


                Console.Write("How much are you betting? Bet with whole numbers: ");
                int betRes = Convert.ToInt32(Console.ReadLine());

                playerMoney.Bet += betRes;

                playerMoney.TotalMoney -= playerMoney.Bet;


                string conDealer = String.Concat(dealerCards[dealerNumber].CardType + " ", dealerCards[dealerNumber].CardValue);
                dealerShownCards.Add(conDealer);
                dealerNumber++;


                totalDealerValue += dealerCards[0].CardValue;
                totalPlayerValue += playerCards[0].CardValue;
                totalPlayerValue += playerCards[1].CardValue;


                bool BJTrue = true;
                while (BJTrue)
                {
                    BJTrue = BlackJack();
                }

            }
            return false;
        }


        public static Money playerMoney = new Money();

        private static bool BlackJack()
        {

            DisplayCards();
            Console.WriteLine("Your Cash: {0:C}", playerMoney.TotalMoney);

            Console.WriteLine("Choose an option: ");
            Console.WriteLine("1) HIT");
            Console.WriteLine("2) STAND");

            string result = Console.ReadLine();

            if (totalPlayerValue == 21)
            {
                Console.WriteLine("You win!");
                playerMoney.TotalMoney += playerMoney.Bet * 2;
                Console.WriteLine("Your Cash: {0:C}", playerMoney.TotalMoney);
                return false;
            }

            if (result == "1")
            {
                Hit();
                if (totalPlayerValue == 21)
                {

                    Console.WriteLine("You win!");
                    playerMoney.TotalMoney += playerMoney.Bet * 2;
                    Console.WriteLine("Your Cash: {0:C}", playerMoney.TotalMoney);
                    return false;
                }

                else if (totalPlayerValue > 21)
                {
                    Console.WriteLine("Busted!");

                    Console.WriteLine("Your Cash: {0:C}", playerMoney.TotalMoney);
                    return false;
                }
                return true;
            }

            else if (result == "2")
            {
                bool res = true;

                while (res)
                {
                    Stand();
                    if (totalDealerValue == 21)
                    {
                        Console.WriteLine("You lose!");

                        Console.WriteLine("Your Cash: {0:C}", playerMoney.TotalMoney);
                        return false;




                    }

                    else if (totalDealerValue > 21)
                    {

                        Console.WriteLine("You win!");
                        playerMoney.TotalMoney += playerMoney.Bet * 2;
                        Console.WriteLine("Your Cash: {0:C}", playerMoney.TotalMoney);
                        return false;



                    }

                    else if (totalDealerValue > totalPlayerValue)
                    {
                        Console.WriteLine("You Lose!");

                        Console.WriteLine("Your Cash: {0:C}", playerMoney.TotalMoney);
                        return false;




                    }
                }


            }
            return true;
        }


        private static void Stand()
        {
            DealerCardGenerator(dealerCards);
            string conDealer = String.Concat(dealerCards[dealerNumber].CardType + " ", dealerCards[dealerNumber].CardValue);
            dealerShownCards.Add(conDealer);
            totalDealerValue += dealerCards[dealerNumber].CardValue;
            dealerNumber++;
            DisplayCards();


        }


        public static List<string> allCards = new List<string>();

        public static List<string> playerShownCards = new List<string>();
        public static List<string> dealerShownCards = new List<string>();

        public static List<PlayerCards> playerCards = new List<PlayerCards>();
        public static List<DealerCards> dealerCards = new List<DealerCards>();

        public static int playerNumber = 0;
        public static int dealerNumber = 0;

        public static int totalPlayerValue = 0;
        public static int totalDealerValue = 0;
        static string CardTypeGenerator()
        {
            Random random = new Random();
            List<string> cardTypes = new List<string>
    {
        "CLUBS", "SPADES", "DIAMONDS", "HEARTS"
    };

            int randomCardType = random.Next(0, 4);
            return cardTypes[randomCardType];

        }

        static int CardValueGenerator()
        {
            Random random = new Random();
            return random.Next(2, 12);

        }

        static void DisplayCards()
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Dealers Cards:");
            foreach (string item in dealerShownCards)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(item);
            }

            Console.WriteLine("Value of dealers cards: {0}", totalDealerValue);

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("\nPlayers Cards: ");
            foreach (string item in playerShownCards)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(item);
            }

            Console.WriteLine("Value of player cards: {0}", totalPlayerValue);

            Console.ForegroundColor = ConsoleColor.White;

        }


        static void PlayerCardGenerator(List<PlayerCards> playerCards)
        {

            int value = CardValueGenerator();
            string type = CardTypeGenerator();
            string con = String.Concat(value, type);
            if (!allCards.Contains(con))
            {
                allCards.Add(con);
                playerCards.Add(new PlayerCards { CardType = type, CardValue = value });

            }
            else PlayerCardGenerator(playerCards);
        }


        static void DealerCardGenerator(List<DealerCards> dealerCards)
        {

            int value = CardValueGenerator();
            string type = CardTypeGenerator();
            string con = String.Concat(value, type);
            if (!allCards.Contains(con))
            {
                allCards.Add(con);
                dealerCards.Add(new DealerCards { CardType = type, CardValue = value });

            }
            else DealerCardGenerator(dealerCards);
        }


        static void Hit()
        {
            PlayerCardGenerator(playerCards);
            string conPlayer = String.Concat(playerCards[playerNumber].CardType + " ", playerCards[playerNumber].CardValue);
            playerShownCards.Add(conPlayer);
            totalPlayerValue += playerCards[playerNumber].CardValue;
            playerNumber++;
            DisplayCards();

        }
    }

    class PlayerCards
    {
        public string CardType { get; set; }
        public int CardValue { get; set; }

    }

    class DealerCards
    {
        public string CardType { get; set; }
        public int CardValue { get; set; }

    }

    class Money
    {
        public int Bet { get; set; }
        public int TotalMoney { get; set; }

    }
}

您嘗試使用allCards列表防止重復卡片的方式存在缺陷。 一旦一副牌用完,您的PlayerCardGeneratorDealerCardGenerator方法就會無限遞歸。 一般來說,這不是管理牌組的好方法(你最好將索引實際存儲到牌中並洗牌數組以模仿洗牌),但無論哪種情況,你都需要決定何時/如何處理甲板清空。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM