0

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#?

MordechayS
  • 1,552
  • 2
  • 19
  • 29
Conor Watson
  • 567
  • 1
  • 7
  • 28
  • a List of a List? or a List of string? or a List of Char array? – Danh Nov 01 '16 at 15:33
  • 1
    `Array[][]` != `Array[,]` read here: [What are the differences between a multidimensional array and an array of arrays in C#?](http://stackoverflow.com/questions/597720/what-are-the-differences-between-a-multidimensional-array-and-an-array-of-arrays) – Jeroen van Langen Nov 01 '16 at 15:35
  • Also a jagged array can be accessed only by one dimension. (which results an one dimension) – Jeroen van Langen Nov 01 '16 at 15:38
  • @JeroenvanLangen I can't just instantiate all of the arrays though with that right? Like I'd have to go through and do seq[0] = new char[256] etc – Conor Watson Nov 01 '16 at 15:45
  • 1
    You could do: `char[][] seq = Enumerable.Range(0, 4*LVLSIZE*LVLSIZE).Select(i => new char[256]).ToArray();` – Jeroen van Langen Nov 01 '16 at 15:50
  • Ahh perfect! Feel free to post that as an answer and I'll accept it. – Conor Watson Nov 01 '16 at 16:01

0 Answers0