0

I have:

namespace CardGame
{
    class Dealer
    {
        static void Main(string[] args)
        {
            string[] suits = { "Clubs", "Spades", "Hearts", "Diamonds" };
            string[] specials = { "Jack", "Queen", "King", "Ace" };
            string[][] hand = new string[5][];

            Console.WriteLine("Please enter the number of players:");

            int playerCount = Int32.Parse(Console.ReadLine());

            for (int currentPlayer = 0; currentPlayer < playerCount; currentPlayer++)
            {
                Random rand = new Random();
                for (int cardNumber = 0; cardNumber < 5; cardNumber++)
                {
                    string card;

                    int mode = rand.Next(0, 2);
                    if (mode == 1) // Numeric card...
                    {
                        card = rand.Next(2, 10).ToString();
                    }
                    else // Face card or ace...
                    {
                        card = specials[rand.Next(0, 4)];
                    }
                    hand[currentPlayer][cardNumber] = card += " of " + suits[rand.Next(0, 3)];
                    Console.WriteLine(card += " of " + suits[rand.Next(0, 3)]);
                }
            }
            Console.ReadLine();
        }
    }
}

The line: hand[currentPlayer][cardNumber] = card += " of " + suits[rand.Next(0, 3)];

Is throwing the error in the title. I have no clue how to fix this as I am very new to C#.

What do I need to do?

4
  • 1
    Possible duplicate of What is a NullReferenceException and how do I fix it? Commented Nov 20, 2015 at 19:59
  • This exception is something that everyone will hit at some point. The answer in the linked question goes through how to trace down the source of them in detail - the best thing you can do is learn the ins and outs of the VS debugger. Commented Nov 20, 2015 at 20:00
  • The real problem you have here is misunderstanding what a jagged array is in C# vs a two dimensional array. In this case, you want the latter and it is declared with like: hand = new string[5,5] in C# Commented Nov 20, 2015 at 20:02
  • I'm coming from a PHP/Laravel/jquery/javascript background, that's why the arrays are strange to me. Commented Nov 20, 2015 at 20:23

2 Answers 2

1

You have created a jagged array, but it's not a complete array of arrays, it's just an array of null references. You have to create an inner array for each item in the outer array.

You have created an array of five items, but it should be an array that has the length of the number of players. So, you have to create the array after you know how many players there are:

...
string[][] hand;

Console.WriteLine("Please enter the number of players:");
int playerCount = Int32.Parse(Console.ReadLine());

hand = new string[playerCount][];
...

Now, inside the loop you should create an inner array for each item, that's the array that should have the length five. The Random instance should be created outside the outer loop, if you create new instances too close in time they will generate the same number sequences.

...
Random rand = new Random();
for (int currentPlayer = 0; currentPlayer < playerCount; currentPlayer++)
{
  hand[currentPlayer] = new string[5];
  ...

As all your inner arrays have the same length, you could use a two dimensional array instead of a jagged array. It's declared in a similar way:

string[,] hand;

and it is created in a similar way:

hand = new string[playerCount, 5];

As it is a single array and not an array of arrays, you don't need to create any inner arrays in the loop.

The assignment of items is also slightly different:

hand[currentPlayer, cardNumber] = ...
Sign up to request clarification or add additional context in comments.

Comments

0

I have made changes to your code and it runs. I believe that is what you would like to achieve

using System.Linq;
namespace CardGame
{
    class Dealer
    {
        static void Main(string[] args)
        {
            string[] suits = { "Clubs", "Spades", "Hearts", "Diamonds" };
            string[] specials = { "Jack", "Queen", "King", "Ace" };

            // Two dimensional arrays - I believe this is what you want to achieve, run the application 
            string[,] hand = new string[5,5];

            Console.WriteLine("Please enter the number of players:");

            int playerCount = Int32.Parse(Console.ReadLine());

            for (int currentPlayer = 0; currentPlayer < playerCount; currentPlayer++)
            {
                Random rand = new Random();
                for (int cardNumber = 0; cardNumber < 5; cardNumber++)
                {
                    string card;

                    int mode = rand.Next(0, 2);
                    if (mode == 1) // Numeric card...
                    {
                        card = rand.Next(2, 10).ToString();
                    }
                    else // Face card or ace...
                    {
                        card = specials[rand.Next(0, 4)];
                    }

                    var temp = " of " + suits[rand.Next(0, 3)];

                    if (card != null && !card.Contains(temp))
                    {
                        hand[currentPlayer, cardNumber] = card += " of " + suits[rand.Next(0, 3)];


                       Console.WriteLine(card += " of " + suits[rand.Next(0, 3)]);

                        //Result
                        Console.WriteLine("Result: {0}", hand[currentPlayer, cardNumber]);
                    }




                }
            }
            Console.ReadLine();
        }
    }
}

8 Comments

is there a way in c# to check if card already exists anywhere in hand[currentPlayer]?
I have modified the code to check if it does not exist , then add it. Look for the if statement
It has to check if the card is in the array, e.g. if 2 of spades already exists then don't add it again. Won't your code only allow one kind of suit?
No, you have defined string card variable, and you are appending all cards to this string variable. Thus, it contains all your cards, so it checks there is no duplication before adding again. If the card already exist , it will not add it. You can simply test it by using duplicate hard coded numbers instead of random numbers. You will not see the affect with the pseudo random numbers, because you are not likely to get duplicates any sooner with the pseudo random numbers
So string[,] hand = new string[5,5] is actually a string?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.