I am converting a piece of C code into C# so I can use it in a project. It has the following function that expects a char* as it's last parameter:
static int get_to(int x, int y, char* store)
{
int l;
if (reachable[y][x] != 0)
l = get_to(x+movesx[reachable[y][x]],y+movesy[reachable[y][x]],store);
else
return 0;
store[l] = "ldru"[reachable[y][x]-2];
return l+1;
}
And this function is used in the code like this:
char seq[4*LVLSIZE*LVLSIZE][256];
//...
//...
len[last] = get_to(x+movesx[i],y+movesy[i],seq[last]);
Since seq is a 2D array I assume that last
is accessing the array like index = x + (y * width)
. Here is the C# code that I have so far:
public static int getTo(int x, int y, ref char[] store)
{
int l;
if(Global.reachable[y, x] != 0)
{
l = getTo(x + Global.movesX[Global.reachable[y, x]], y + Global.movesY[Global.reachable[y, x]], ref store);
}
else
{
return 0;
}
store[l] = "ldru"[Global.reachable[y, x] - 2];
return l + 1;
}
And here is how I use the function:
static char[,] seq = new char[4 * Global.LVLSIZE * Global.LVLSIZE, 256];
//...
//...
len[last] = getTo(x + Global.movesX[i], y + Global.movesY[i], seq[last]);
But C# doesn't allow a 2D array to be accessed with only 1 index, so I tried changing it to seq[last % seq.GetLength(0), last / seq.GetLength(0)]
but since the function is expecting a char array this does not work either.
So how should this C code be replicated in C#?