Reputation: 3424
I have the following code in C, Its basically a maze where
S=starting point G=Goal .=open path and #=dead end
char maze[6][6] = {
"S...##",
"#.#...",
"#.##.#",
"..#.##",
"#...#G",
"#.#..."
};
I am trying to convert into c#, here is my attempt
char[,] maze = new char[6,6];
I don't know how to add the 2dimensional array into this object. basically I want the maze layout in C#.
I also want to be ably to access a point in my maze, for example maze[x][y]=="S" for comparison.
Upvotes: 1
Views: 300
Reputation: 10074
The char[,]
solution is probably the one you want to use but just for kicks if you just need to access elements as `maze[y][x]' you can use your old code with a slight twist:
string[] maze = new []{
"S...##",
"#.#...",
"#.##.#",
"..#.##",
"#...#G",
"#.#..."
};
You'll have to remember that this is an array of string
not char
but strings model a sequence of chars. This won't work if you intend on modifying individual elements (like say maze[3][2] = '.'
as strings are immutable.
Upvotes: 2
Reputation: 18540
char[,] myArray = new char[6, 6]
{
{ 'S','.','.','.','#','#' },
{ '#','.','#','.','.','.' },
{ '#','.','#','#','.','#' },
{ '.','.','#','.','#','#' },
{ '#','.','.','.','#','G' },
{ '#','.','#','.','.','.' },
};
Upvotes: 0
Reputation: 4530
char[,] maze = new char[6,6]
{
{'a','b','c','d','e','f'},
{'g','h','i','j','k','l'},
{'a','b','c','d','e','f'},
{'a','b','c','d','e','f'},
{'a','b','c','d','e','f'},
{'a','b','c','d','e','f'}
};
Edit: http://msdn.microsoft.com/en-us/library/2yd9wwz4%28v=vs.71%29.aspx
Upvotes: 3