Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 康威';s的生命游戏逻辑错误_C#_Conways Game Of Life - Fatal编程技术网

C# 康威';s的生命游戏逻辑错误

C# 康威';s的生命游戏逻辑错误,c#,conways-game-of-life,C#,Conways Game Of Life,我正在学习一门使用C#的课程,我们的第一个任务是实现康威的《生命游戏》。我们必须通过读取如下格式的文本文件来完成此操作: * * *** *** 然后我们必须在屏幕上显示下10代。 我将文件读入一个字符串数组,然后将其复制到另一个数组。然后我逐个字符地检查它,并更改复制的数组以匹配下一代应该是什么。我的问题是,我必须计算住的邻居的代码不起作用,我不知道为什么。我在屏幕上显示了每个单元格的活动邻居数量,其中大约一半是错误的。我知道错误发生在“电路板”边缘的单元上,但我

我正在学习一门使用C#的课程,我们的第一个任务是实现康威的《生命游戏》。我们必须通过读取如下格式的文本文件来完成此操作:

 *
  *
***

          ***
然后我们必须在屏幕上显示下10代。 我将文件读入一个字符串数组,然后将其复制到另一个数组。然后我逐个字符地检查它,并更改复制的数组以匹配下一代应该是什么。我的问题是,我必须计算住的邻居的代码不起作用,我不知道为什么。我在屏幕上显示了每个单元格的活动邻居数量,其中大约一半是错误的。我知道错误发生在“电路板”边缘的单元上,但我不知道如何修复它

现在,我不想把整件事都写下来,那会有点毫无意义。我就是想不出我的逻辑在哪里。任何帮助都将不胜感激。另外,我知道我的代码总体上相当糟糕。这是我唯一能弄明白的办法。对不起

 class Program
{
    static void Main(string[] args)
    {
        //gets file name from command arguments
        //checks to make sure file exists, exits if file does not exist
        if (!File.Exists(Environment.GetCommandLineArgs()[1])) { System.Environment.Exit(1); }

        //gets file name from command arguments then reads file into array of strings
        string[] gen0 = File.ReadAllLines(Environment.GetCommandLineArgs()[1]);

        string[] gen1 = gen0;
        char alive = '*';
        char dead = ' ';

        //displays first generation
        foreach (string s in gen0)
        {
            Console.WriteLine(s);
        }
        Console.WriteLine("=====================================");
        //counts live neighbors of a cell
        int count = 0;
        for (int i = 0; i < gen0.Length; i++)
        {
            count = 0;
            for (int j = 0; j < gen0[i].Length; j++)
            {
                //check top left neighbor
                if (i > 0 && j > 0 && j < gen0[i-1].Length )
                {
                    if (gen0[i - 1][j - 1] == alive) { count++; }
                }
                //check above neighbor
                if (i > 0 && j < gen0[i-1].Length)
                {
                    if (gen0[i - 1][j] == alive) { count++; }
                }
                //check top right neighbor
                if (i > 0 && j + 1 < gen0[i - 1].Length)
                {
                    if (gen0[i - 1][j + 1] == alive) { count++; }
                }
                //check left neighbor
                if (j > 0)
                {
                    if (gen0[i][j - 1] == alive) { count++; }
                }
                //check right neighbor
                if (j + 1 < gen0[i].Length)
                {
                    if (gen0[i][j + 1] == alive) { count++; }
                }
                //check bottom left neighbor
                if (i + 1 < gen0.Length && j > 0 && j < gen0[i+1].Length)
                {
                    if (gen0[i + 1][j - 1] == alive) { count++; }
                }
                //check below neighbor
                if (i + 1 < gen0.Length && j < gen0[i+1].Length)
                {
                    if (gen0[i + 1][j] == alive) { count++; }
                }
                //check bottom right neighbor
                if (i + 1 < gen0.Length && j + 1 < gen0[i].Length && j + 1 < gen0[i+1].Length)
                {
                    if (gen0[i + 1][j + 1] == alive) { count++; }
                }

                //Console.WriteLine(count); 
                //kills cells
                if (count < 2 || count > 3) 
                {
                    gen1[i] = gen1[i].Remove(j, 1);
                    gen1[i] = gen1[i].Insert(j, dead.ToString()); 
                }
                //births cells
                if (count == 3)
                {
                    gen1[i] = gen1[i].Remove(j, 1);
                    gen1[i] = gen1[i].Insert(j, alive.ToString());
                }
            }
        }
        foreach (string s in gen1)
        {
            Console.WriteLine(s);
        }
    } 
}
类程序
{
静态void Main(字符串[]参数)
{
//从命令参数获取文件名
//检查以确保文件存在,如果文件不存在则退出
如果(!File.Exists(Environment.GetCommandLineArgs()[1]){System.Environment.Exit(1);}
//从命令参数获取文件名,然后将文件读入字符串数组
string[]gen0=File.ReadAllLines(Environment.GetCommandLineArgs()[1]);
字符串[]gen1=gen0;
char alive='*';
char dead='';
//显示第一代
foreach(gen0中的字符串s)
{
控制台。写入线(s);
}
Console.WriteLine(“====================================================”);
//计算细胞的活邻居数
整数计数=0;
for(int i=0;i0&&j>0&&j0&&j0&&j+10)
{
如果(gen0[i][j-1]==alive){count++;}
}
//检查右邻居
if(j+10&&j3)
{
gen1[i]=gen1[i]。删除(j,1);
gen1[i]=gen1[i].插入(j,dead.ToString());
}
//出生细胞
如果(计数=3)
{
gen1[i]=gen1[i]。删除(j,1);
gen1[i]=gen1[i].插入(j,alive.ToString());
}
}
}
foreach(gen1中的字符串s)
{
控制台。写入线(s);
}
} 
}

您的问题非常简单-您在错误的位置重置了
计数。把它放在循环中,它(可能)会工作

关于代码的其余部分,如果你想让它更容易理解,只需给你的游戏区域一个元素边界

您需要填充读取的文件(上面和下面的空行,左边和右边的空白字符),并将循环更改为:

  for (int i = 1; i < gen0.Length - 1; i++)

这将使代码更清晰,并确保您可能犯的任何其他错误都更容易发现。

因此,我看到的第一个错误是,您实际上没有为下一次迭代复制电路板

gen1 = gen0;
上面的代码仅将gen1引用分配给与gen0相同的对象。所以当你修改gen1时,实际上你也修改了gen0。。。在后面的迭代中导致不一致。试试这个:

gen1 = (string[])gen0.Clone();
第二个错误是
int count=0
应该在第二个循环中,而不是第一个循环中:

for (int i = 0; i< gen0.Length; i++)
{
    // not here
    // int count = 0
    for (int j = 0; j < gen0[i].Length; j++)
    {
        // here
        int count = 0
        ...
    }
...
}
for(int i=0;i

通过这种方式,您可以重置每个单元格的计数,而不是每一行。

是的,如果字符串数组被二维整数数组替换,那么逻辑将变得更加简单:
count=board[i-1,j-1]+board[i-1,j]+…
,其中
1
表示活动和
0
dead@OlivierJacot-尽管评论并不意味着“谢谢”或“+1”,但描述却很重要,你说得对-在主循环开始之前,只需对文本文件进行一点处理,这将是一种很好的方法。在复制之前,有没有办法使gen1中的每一行都成为gen0中最长行的长度?@user2773218当然,gen1 = (string[])gen0.Clone();
for (int i = 0; i< gen0.Length; i++)
{
    // not here
    // int count = 0
    for (int j = 0; j < gen0[i].Length; j++)
    {
        // here
        int count = 0
        ...
    }
...
}