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?

imperium2335
  • 23,402
  • 38
  • 111
  • 190
  • 1
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – James Thorpe Nov 20 '15 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. – James Thorpe Nov 20 '15 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# – Eric Hotinger Nov 20 '15 at 20:02
  • I'm coming from a PHP/Laravel/jquery/javascript background, that's why the arrays are strange to me. – imperium2335 Nov 20 '15 at 20:23

2 Answers2

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] = ...
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
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();
        }
    }
}
Julius Depulla
  • 1,493
  • 1
  • 12
  • 27
  • is there a way in c# to check if card already exists anywhere in hand[currentPlayer]? – imperium2335 Nov 20 '15 at 20:18
  • I have modified the code to check if it does not exist , then add it. Look for the if statement – Julius Depulla Nov 20 '15 at 20:29
  • 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? – imperium2335 Nov 20 '15 at 20:37
  • 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 – Julius Depulla Nov 20 '15 at 20:43
  • So string[,] hand = new string[5,5] is actually a string? – imperium2335 Nov 20 '15 at 20:44
  • No, string[,] hand = new string[5,5] ; is two dimensional array. It has 5 rows and 5 columns. You can change the size to suit your needs – Julius Depulla Nov 20 '15 at 20:47
  • So it is completely different to how arrays work in PHP? – imperium2335 Nov 20 '15 at 20:50
  • @imperium2335: There are no multidimensional arrays in PHP, there you have to use jagged arrays for that. – Guffa Nov 20 '15 at 20:53