I am a novice coder learning C# for my class and am having trouble with a practice exercise where we have to make a deck of cards and deal them out.
We need to make a class that creates cards by receiving parameters from another class through a constructor. Then use those values in a Tostring method to set the value and suit of those cards with switch statements and return a card. In the second class we populate an array with cards and call the Tostring method in a Dealing method to randomly generate a user specified amount of cards by pulling them out of the array.
The problem is my array isn't being populated I have tried to Console.WriteLine parts of the array directly after they would be assigned and they are empty. You don't have to give me the full answer put just put me on the right track.
Here is the code:
This is the card creation class
` class Card
{
static int value;
static int suit;
static string cdvalue;
static string cdsuit;
string card;
public Card(int ranvalue, int ransuit) //constructor that sets value and suit
{
value = ranvalue;
suit = ransuit;
}
public override string ToString()
{
switch (value) //switch statement for card value
{
case 1: cdvalue = "ace";
break;
case 2: cdvalue = "two";
break;
case 3: cdvalue = "three";
break;
case 4: cdvalue = "four";
break;
case 5: cdvalue = "five";
break;
case 6: cdvalue = "six";
break;
case 7: cdvalue = "seven";
break;
case 8: cdvalue = "eight";
break;
case 9: cdvalue = "nine";
break;
case 10: cdvalue = "ten";
break;
case 11: cdvalue = "jack";
break;
case 12: cdvalue = "queen";
break;
case 13: cdvalue = "king";
break;
}
switch (suit) // switch for card suit
{
case 1: cdsuit = "Hearts ";
break;
case 2: cdsuit = "Spades ";
break;
case 3: cdsuit = "Diamonds ";
break;
case 4: cdsuit = "Clubs ";
break;
}
card = cdvalue + " of " + cdsuit;
return card;// returns a string in the form of "value of suit"`
This class creates the deck
class Deck
{
Random rng = new Random(); //Random object
private static Card[] deck;
static string[] cards;
public Deck()
{
deck = new Card[52];//creates array of 52 elements
int l = 0;
for (int i = 1; i <= 13; i++)//loops to create cards
{
for (int j = 1; j <= 4; j++)
{
deck[l++] = new Card(i,j); // populates the array with all 52 cards
}
}
}
static string dealt;
static int Rndm;
public string deal(int number)//parameter received from user
{
cards = new string[number];//creates an array to contain dealt card objects
int m = 0;
for (int num=0;num<number;num++) // determines the amount of cards to be dealt
{
Rndm = rng.Next(0,53);
cards[m++] = deck[Rndm].ToString();//fills the card array with randomly dealt cards from the deck
dealt = string.Join(" ", cards); // turns the card array into a single string of the cards dealt
}
return dealt;//returns the cards dealt
This is the test class
static void Main(string[] args)
{
// get the number of cards from the user - must be between 1 and 52
int cardsDealt = -1;
do
{
Console.Write("Enter number of cards to get (1-52): ");
String dealStr = Console.ReadLine();
Boolean parsed = int.TryParse(dealStr, out cardsDealt);
} while (cardsDealt < 1 || cardsDealt > 52);
// create a Deck object
Deck cardDeck = new Deck();
// Call the deal method
String cards = cardDeck.deal(cardsDealt);
// List the result
Console.WriteLine("\nCards dealt:\n" + cards);