I have made a little console BlackJack game. It is my first game when working with C#. The game works fine for a few rounds until I get an error message of "Stackoverflow". When I look at the error I see that when in the DealerCardGenerator and the PlayerCardGenerator that the variable "con" has a null value. I do not understand why this is. Any help would be much appreciated.
I don't know how to resolve the issue.
Here is the code:
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; }
}
}
CodePudding user response:
There's a flaw in the way you're trying to prevent duplicated cards using the allCards
list. Once the deck is exhausted, your PlayerCardGenerator
and DealerCardGenerator
methods infinitely recurse. In general this isn't a great way to manage a deck (you'd be better off actually storing indices to cards and shuffling an array to mimic shuffling a deck), but in either case you'll need to decide when/how to handle the deck emptying.