运行 一段时间后,我的程序遇到了堆栈溢出问题。它在一段时间内工作正常,直到它不工作
My program runs into a Stack-overflow issue after some time of running. It works fine for sometime until it doesn't
我做了一个小的控制台 BlackJack 游戏。这是我使用 C# 开发的第一款游戏。游戏可以正常运行几轮,直到我收到“Whosebug”的错误消息。当我查看错误时,我发现在 DealerCardGenerator 和 PlayerCardGenerator 中变量“con”具有空值。我不明白这是为什么。任何帮助将不胜感激。
我不知道如何解决这个问题。
代码如下:
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
列表防止重复卡片的方式存在缺陷。一旦牌组用完,您的 PlayerCardGenerator
和 DealerCardGenerator
方法将无限递归。一般来说,这不是管理牌组的好方法(你最好将索引存储到牌中并洗牌数组以模仿洗牌),但无论哪种情况,你都需要决定 when/how 处理甲板清空。
我做了一个小的控制台 BlackJack 游戏。这是我使用 C# 开发的第一款游戏。游戏可以正常运行几轮,直到我收到“Whosebug”的错误消息。当我查看错误时,我发现在 DealerCardGenerator 和 PlayerCardGenerator 中变量“con”具有空值。我不明白这是为什么。任何帮助将不胜感激。
我不知道如何解决这个问题。
代码如下:
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
列表防止重复卡片的方式存在缺陷。一旦牌组用完,您的 PlayerCardGenerator
和 DealerCardGenerator
方法将无限递归。一般来说,这不是管理牌组的好方法(你最好将索引存储到牌中并洗牌数组以模仿洗牌),但无论哪种情况,你都需要决定 when/how 处理甲板清空。