Main Site Documentation

Multi-Dimensional Arrays


#1

Hi,
Just hit my first Catastrophic Failure.

Can anyone suggest another way of storing this maze information that is easily addressable?

I am storing Maze/Map data in a multi-dimensional char array:

private char[,] maze = new char[20,20];

Which basically stores the equivalent of this:

##########
 #0#   5=4#
 #1#6   # #
 #=###### #
 #2      3#
 ########=#
 #7#   ## #
 # # #  # #
 #   ##   #
 ##########

Thanks for any help.

Jas


#2

Use a jagged array

http://msdn.microsoft.com/en-us/library/2s05feca.aspx


#3

Thanks,

That seems to work.

My c# knowledge is starting to embarrass me :wink:

Jas


#4

You are welcome!

Nothing to be embarrassed about.


#5

For anyone else who ends up in the same boat. This is the code I ended up using:

    private char[][] maze = new char[20][];

// Stuff

        private void InitialiseMaze()
        {
            for (int lp = 0; lp <= 19; lp++)
            {
                maze[lp] = new char[20];
            }
        }
 
// Stuff

        private void LoadMaze()
        {
            // read in the text file and convert to a string.
            string path = _mazePath + @ "\store\" + _currentLevel + ".map";
            byte[] rawBytes = _storageDev.ReadFile(path);
            char[] rawCharacters = System.Text.UTF8Encoding.UTF8.GetChars(rawBytes);
            string tmpMap = new string(rawCharacters);
            // Now split the text file into lines using the \r character from the text file
            // note this will leave the \n character at the end of each line
            string[] lines = tmpMap.Split('\r');
            // Get the header, removing the \n
            string header = lines[0].Trim('\n');
            // check maze version and the dimensions of the maze
            string[] bits = header.Split('|');
            string ver = bits[0];
            int width = int.Parse(bits[1]);
            int height = int.Parse(bits[2]);
            if (ver != "1.0")
            {
                throw new Exception("Maze version is incorrect");
            }
            for (int y = 1; y <= height; y++)
            {
                char[] row = lines[y].Trim('\n').ToCharArray();
                for (int x = 0; x < width; x++)
                {
                    maze[x][y-1] = row[x];
                }
            }
        }

#6

FYI - this isn’t a C# limitation, it’s a NETMF limitation. Your original code would work just fine in the “big” C#. :wink:


#7

Ian,

I gathered that :wink:

In the way that the IDE let me do it and though it was ok, buy as soon as I tried to deploy, you hit the error.

The good thing is I’ve learnt something new which may help in the Big C# world too :slight_smile:

Jas