r/programminghelp • u/RedDeadJunkie • Dec 11 '21
C# How to make sure my program does not crash if User does not fill his/her deck first
So for my assignment I have to make a deck of cards that I can fill up into the console. And then I can Shuffle, Cut Deck Randomly, Player selected cut deck, draw and display card, display all cards, and to quit it. I have EVERYTHING working except that I cannot figure out how to stop it from crashing if I do not FILL the Deck first. So my question is, what do I have to do to stop that crash? Below I will put my main program code and after that the Deck Class Code
Main Program Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CardGame;
namespace DES1_Week2_Assignment
{
class Program
{
static void Main(string[] args)
{
Deck MyDeck = new Deck();
bool Running = true;
// TODO: Create a deck variable and assign a new object of type deck to it
// TODO: Create a menu that will let players choose from the following options
// - Fill Deck
// - Shuffle Deck
// - Cut Deck (Random number of cards)
// - Cut Deck (Player selects how many cards to cut off the top)
// - Draw a card from the deck and display the card
// - Display every card in the deck
// - Quit the program
while (Running)
{
Console.WriteLine("Program User Menu: ");
Console.WriteLine("1. Fill Deck\n" +
"2. Shuffle Deck\n" +
"3. Cut Deck(Random Location)\n" +
"4. Cut Deck(Player Selected)\n" +
"5. Draw and Display Card\n" +
"6. Display All Cards\n" +
"7. Quit Program");
Console.Write("What would you like to do?");
string UserChoice = Console.ReadLine();
switch (UserChoice.ToLower())
{
case "1":
{
Deck.FillDeckWithPlayingCards(MyDeck);
Console.Clear();
Console.WriteLine("Deck has been filled!\n");
}
break;
case "2":
{
Console.Clear();
MyDeck.Shuffle();
PressAKey("Deck has now been shuffled!");
Console.Clear();
}
break;
case "3":
{
Console.Clear();
MyDeck.CutDeck();
PressAKey("Deck cut randomly is a success. Press any key to continue");
Console.Clear();
}
break;
case "4":
{
Console.Clear();
Console.WriteLine("By what number would you like to cut the deck by?");
string UserResponseInput = Console.ReadLine();
int UserResponse;
bool NeedValidUserResponse = true;
bool WasANumber = int.TryParse(UserResponseInput, out UserResponse);
while (NeedValidUserResponse)
{
if (UserResponse <= 0 || UserResponse >= 52)
{
Console.Clear();
PressAKey("That is an invalid input, please input # from 1-51 only please! Press any key and select Option 4 to try again!");
Console.Clear();
}
else if (UserResponse > 0 && UserResponse < 52)
{
Console.Clear();
MyDeck.CutDeck(UserResponse);
PressAKey("Deck was cut by your input. Press any key to continue");
Console.Clear();
}
else if (WasANumber)
{
NeedValidUserResponse = false;
}
else
{
Console.Clear();
PressAKey("That is an invalid input, please input # from 1-51 only please! Press any key and select Option 4 to try again!");
Console.Clear();
}
break;
}
}
break;
case "5":
{
Console.Clear();
PlayingCard CardThatWasDrawn = MyDeck.DrawCard();
CardThatWasDrawn.DrawCardAtLocation(1,1);
PressAKey("Card is Drawn and here it is for you!");
Console.Clear();
}
break;
case "6":
{
Console.Clear();
MyDeck.DisplayDeck(13);
PressAKey("Press any key to continue");
Console.Clear();
}
break;
case "7":
{
Running = false;
continue;
}
case "quit":
{
Running = false;
continue;
}
case "exit":
{
Running = false;
continue;
}
default:
{
Console.Clear();
Console.WriteLine("That was not a valid option, please input 1-7 please.\n");
}
break;
}
}
// TODO: Add a loop so the program keeps running until the player chooses to quit
// TODO: Implement each of the menu options so they call the required functions on your deck variable
// - (Will often require additional code for full functionality)
PressAKey("Press any key to continue");
}
static void PressAKey(string Message)
{
// Save console cursor position
int OldX = Console.CursorLeft;
int OldY = Console.CursorTop;
// Write message at the bottom of the console window
Console.SetCursorPosition(0, Console.WindowHeight - 1);
Console.Write(Message);
Console.ReadKey();
// Return the cursor to its previous location
Console.SetCursorPosition(OldX, OldY);
}
}
}
DECK CLASS CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CardGame;
namespace DES1_Week2_Assignment
{
class Deck
{
// Fields ////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: Declare required fields here like the array of PlayingCards to hold all of the cards in the deck.
private PlayingCard[] CardArray;
private int TopCardIndex;
private bool HasChanged;
private bool IsFilled;
// Constructors //////////////////////////////////////////////////////////////////////////////////////////
// TODO: Implement a default constructor
public Deck()
{
}
// TODO: Implement a constructor that takes an parameter for the size of the deck
public Deck(int NumCardsInDeck = 52)
{
CardArray = new PlayingCard[NumCardsInDeck];
TopCardIndex = 0;
HasChanged = true;
IsFilled = false;
}
// Public Methods ////////////////////////////////////////////////////////////////////////////////////////
// TODO: Implement AddCard method
public void AddCard()
{
PlayingCard AddCardToDeck = null;
if (TopCardIndex >= 52)
{
CardArray[TopCardIndex] = AddCardToDeck;
TopCardIndex++;
HasChanged = true;
}
}
// TODO: Implement Shuffle method
public void Shuffle()
{
Random rand = new Random();
for (int IndexCurrent = 0; IndexCurrent < CardArray.Length; IndexCurrent++)
{
int IndexNew = rand.Next(0, CardArray.Length);
Swap(IndexCurrent, IndexNew);
}
}
// TODO: Implement DrawCard method
public PlayingCard DrawCard()
{
PlayingCard CardToDraw = null;
if (TopCardIndex < 52)
{
CardToDraw = CardArray[TopCardIndex + 1];
TopCardIndex++;
HasChanged = true;
}
return CardToDraw;
}
// TODO: Implement CutDeck method
public void CutDeck(int CardToSwap = -1)
{
Random rng = new Random();
if (CardToSwap < 0)
{
CardToSwap = rng.Next(1, CardArray.Length + 1);
}
PlayingCard[] tempArray = new PlayingCard[CardToSwap];
for (int i = 0; i < CardToSwap; i++)
{
tempArray[i] = CardArray[i];
}
int CardsLeft = CardArray.Length - CardToSwap;
for (int i = 0; i < CardsLeft; i++)
{
CardArray[i] = CardArray[CardToSwap + i];
}
Console.WriteLine();
for (int i = 0; i < CardToSwap; i++)
{
CardArray[CardsLeft + i] = tempArray[i];
}
}
// TODO: Implement DisplayDeck method
public void DisplayDeck(int CardsInRow, int PositionX = 0, int PositionY = 0)
{
int CurrentX = PositionX;
int CurrentY = PositionY;
ConsoleColor OldBGColor = Console.BackgroundColor;
ConsoleColor OldFGColor = Console.ForegroundColor;
Console.BackgroundColor = ConsoleColor.White;
for (int i = TopCardIndex; i <= 51; i++)
{
if (i % CardsInRow == 0 && i != 0)
{
CurrentX = 0;
CurrentY += 4;
}
CardArray[i].DrawCardAtLocation(CurrentX, CurrentY);
CurrentX += 6;
}
Console.BackgroundColor = OldBGColor;
}
// Private Methods ///////////////////////////////////////////////////////////////////////////////////////
// TODO: Implement any private methods that you need here
private void Swap(int IndexOne, int IndexTwo)
{
PlayingCard Temp = CardArray[IndexOne];
CardArray[IndexOne] = CardArray[IndexTwo];
CardArray[IndexTwo] = Temp;
}
// Static Methods ////////////////////////////////////////////////////////////////////////////////////////
// TODO: Implement FillDeckWIthPlayingCards method
public static void FillDeckWithPlayingCards(Deck deck)
{
//deck.CardArray = deck.IsFilled;
if (deck.CardArray == null || deck.CardArray.Length != 53)
{
deck.CardArray = new PlayingCard[52];
}
PlayingCard.Ranks CurrentRank = PlayingCard.Ranks.Ace;
PlayingCard.Suits CurrentSuit = PlayingCard.Suits.Hearts;
{
}
for (int i = 0; i < deck.CardArray.Length; i++)
{
deck.CardArray[i] = new PlayingCard(CurrentSuit, CurrentRank);
if (CurrentRank == PlayingCard.Ranks.King)
{
CurrentSuit++;
CurrentRank = PlayingCard.Ranks.Ace;
}
else
{
CurrentRank++;
}
}
}
}
}
3
u/BrookieYT Dec 11 '21
Before you can run any of the functions (Cut Deck Randomly, Draw & Display), make sure that the deck is filled, you can do this by wrapping your functions in a while loop or an if loop. And then have something a boolean function for Deck, which checks if deck is filled or not, and returns 1 and 0 accordingly.