c 到 c# 中的字符数组

发布于 12-03 05:45 字数 374 浏览 0 评论 0原文

我有以下 C 代码, 它基本上是一个迷宫,其中

S=起点 G=目标 .=开放路径 和 #=死胡同

char maze[6][6] = {
    "S...##",
    "#.#...",
    "#.##.#",
    "..#.##",
    "#...#G",
    "#.#..."
};

我试图将其转换为 c#,这是我的尝试,

char[,] maze = new char[6,6];

我不知道如何将二维数组添加到该对象中。基本上我想要 C# 中的迷宫布局。

我还希望能够访问迷宫中的一个点,例如 maze[x][y]=="S" 进行比较。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿挽旧人2024-12-10 05:45:49
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'}
};

编辑:
http://msdn.microsoft.com/en -us/library/2yd9wwz4%28v=vs.71%29.aspx

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

日裸衫吸2024-12-10 05:45:49

char[,] 解决方案可能是您想要使用的解决方案,但只是为了好玩,如果您只需要以“maze[y][x]”访问元素,您可以将旧代码与略有不同:

string[] maze = new []{
    "S...##",
    "#.#...",
    "#.##.#",
    "..#.##",
    "#...#G",
    "#.#..."
};

您必须记住,这是一个 string 数组,而不是 char ,而是字符串模型的字符序列。如果您打算修改单个元素(例如 maze[3][2] = '.' 因为字符串是不可变的),那么这将不起作用。

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.

水水月牙2024-12-10 05:45:49
char[,] myArray = new char[6, 6] 
{
    { 'S','.','.','.','#','#' }, 
    { '#','.','#','.','.','.' }, 
    { '#','.','#','#','.','#' }, 
    { '.','.','#','.','#','#' }, 
    { '#','.','.','.','#','G' }, 
    { '#','.','#','.','.','.' }, 
};
char[,] myArray = new char[6, 6] 
{
    { 'S','.','.','.','#','#' }, 
    { '#','.','#','.','.','.' }, 
    { '#','.','#','#','.','#' }, 
    { '.','.','#','.','#','#' }, 
    { '#','.','.','.','#','G' }, 
    { '#','.','#','.','.','.' }, 
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文