user478636
user478636

Reputation: 3424

character array in c to c#

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

Answers (3)

Ron Warholic
Ron Warholic

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

alf
alf

Reputation: 18540

char[,] myArray = new char[6, 6] 
{
    { 'S','.','.','.','#','#' }, 
    { '#','.','#','.','.','.' }, 
    { '#','.','#','#','.','#' }, 
    { '.','.','#','.','#','#' }, 
    { '#','.','.','.','#','G' }, 
    { '#','.','#','.','.','.' }, 
};

Upvotes: 0

Saad Imran.
Saad Imran.

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

Related Questions