Main Site Documentation

Problem with jagged Arrays


#1

So i recently programmed a Tic-Tac-Toe game in C# as a Console App and i wanted to port it to my cerberus and N18 Display. The problem was i needed to change my 2d array to an jagged 2d array , because NetMF doesn’t support multi dimensional arrays and I’m getting a weird exception, because it is exactly the same code, but i only changed the array and the console to my display.

Console Code:


static void Spielfeld()
        {
            Console.Clear();
            
            for (int i = 0; i < 2; i++)
            {
                Console.Clear();
                Console.WriteLine("-------------");
                
                for (int Row = 0; Row < 3; Row++)
                {
                    for (int Column = 0; Column < 3; Column++)
                    {
                        if (Board[Row, Column] != "X" && Board[Row, Column] != "O")
                        {
                            Board[Row, Column] = " ";
                        }
                    }
                    
                    Console.WriteLine("| " + Board[0, Row] + " | " + Board[1, Row] + " | " + Board[2, Row] + " |");
                    Console.WriteLine("-------------");
                }
            }
        }

Now the NetMF Code:


void Spielfeld()
        {
            uint Position = 5;
            display_N18.Clear();
            for (int i = 0; i < 2; i++)
            {
                display_N18.Clear();
                lcdwritetext("-------------", 0,0);

                for (int Row = 0; Row < 3; Row++)
                {
                    for (int Column = 0; Column < 3; Column++)
                    {
                        if (Board[Row][Column] != "X" && Board[Row][Column] != "O") /*<-- Here im getting the Exception!*/
                        {
                            Board[Row] [Column] = " ";
                        }
                    }

                    lcdwritetext("| " + Board[0][Row] + " | " + Board[1][Row] + " | " + Board[2][Row] + " |", 0,Position);
                    lcdwritetext("-------------", 0,10);
                    Position = Position + 5;
                }
            }
        }

Thank you for your help.


#2

What kind of exception are you getting?

Edit: Show please how you instantiate your Board array.


#3

System.NullReferenceException


#4

Please show how you instantiate the Board array.


#5

Oh forgot that sorry :wink:

String[][] Board = new String[3][];

#6

Do it like this:

string[][] Board = new string[][]
        {
            new string[]{string.Empty,string.Empty,string.Empty},
            new string[]{string.Empty,string.Empty,string.Empty},
            new string[]{string.Empty,string.Empty,string.Empty}
        };


#7

Thank you, its working.


#8

You are welcome!