Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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# 为什么将细胞自动机用于二维阵列会产生不好的结果?_C#_Procedural Generation_Cellular Automata - Fatal编程技术网

C# 为什么将细胞自动机用于二维阵列会产生不好的结果?

C# 为什么将细胞自动机用于二维阵列会产生不好的结果?,c#,procedural-generation,cellular-automata,C#,Procedural Generation,Cellular Automata,我正在尝试使用细胞自动机制作一个程序生成的map/2d_数组 我找到了一个很好的解释细胞自动机的视频,并跟踪了它。 视频链接: 但由于某些原因,我只得到一个完全填充的2d数组,具有相同的值,而不是像2d数组那样按程序生成的贴图。 我试着调试它,看看有什么问题,出于某种原因,这个if语句总是错误的: //check if neighbor is a - and if so add neighbor_wall_count if (temp_grid[k, j] == '-') { nei

我正在尝试使用细胞自动机制作一个程序生成的map/2d_数组

我找到了一个很好的解释细胞自动机的视频,并跟踪了它。 视频链接:

但由于某些原因,我只得到一个完全填充的2d数组,具有相同的值,而不是像2d数组那样按程序生成的贴图。 我试着调试它,看看有什么问题,出于某种原因,这个if语句总是错误的:

//check if neighbor is a - and if so add neighbor_wall_count
 if (temp_grid[k, j] == '-')
 {
    neighbor_wall_count++;
 }
我找不到原因。 也许你能帮忙

以下是所有代码:

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //this line string is used for printing
            string line = "";
            //2d array size specifications
            int width = 10;
            int height = 10;
            //density = how much procent will be - in the array when i we make random noise
            int density = 60;
            //create the 2d array
            char[,] grid = new char[width, height];

            //This creates random noise in array or its a function make_noise_grid(density) from video https://www.youtube.com/watch?v=slTEz6555Ts
            for (int y = 0; y < height; y++)
            {
                line = "";
                for(int x = 0; x < width; x++)
                {
                    int rand = new Random().Next(1, 100);
                    if(rand > density)
                    {
                        grid[x, y] = '+';
                    }
                    else
                    {
                        grid[x, y] = '-';
                    }
                    line += grid[x, y];
                }
                Console.WriteLine(line);
            }

            // This is just a divider to divide random noise map from map with applied with cellulat automata
            Console.WriteLine("-------------------------------------------------------------------");

            //This section is the function apply_cellular_automation(grid, count) from video https://www.youtube.com/watch?v=slTEz6555Ts
            //This first for will determine how much times Cellular Automata will be added
            for (int i = 0; i < 1; i++)
            {
                //Create a temporary map
                char[,] temp_grid = grid;
                //Go through array
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        //create variable to store neighbor minuses (-)
                        int neighbor_wall_count = 0;
                        //go through neigbors
                        for (int j = y-1; j <= y+1; j++)
                        {
                            for(int k = x-1; k <= x+1; k++)
                            {
                                //check if in bounds of array if not just assume its -
                                if (j >= 0 && j < grid.GetLength(1) && k >= 0 && k < grid.GetLength(0))
                                {
                                    //check if this is not the coordinate whose neighbors we are checking
                                    if (j != y && k != x)
                                    {
                                        //check if neighbor is a - and if so add neighbor_wall_count
                                        if (temp_grid[k, j] == '-')
                                        {
                                            neighbor_wall_count++;
                                        }
                                    }
                                }
                                else
                                {
                                    neighbor_wall_count++;
                                }
                            }
                        }
                        //if there are more than 4 neighbors that are - make the coordinate a - and if not make it +
                        if (neighbor_wall_count > 4)
                        {
                            grid[x, y] = '-';
                        }
                        else
                        {
                            grid[x, y] = '+';
                        }
                    }
                }
            }

            // this is to print the array when we apply Cellular automata.
            for (int y = 0; y < height; y++)
            {
                line = "";
                for (int x = 0; x < width; x++)
                {
                    line += grid[x, y];
                }
                Console.WriteLine(line);
            }
        }
    }
}


首先,J&K的For循环应该是两个内部循环,看起来像For(int J=y-1;J,但是想想看,它只包含值
J==y-1
J==y
,跳过
J==y+1
。实际上使用temp\u grid=temp不会复制网格。你要找的是一份深度副本。一开始我没有看到,但我认为如果你深度复制它应该可以工作这有一些很好的例子我是这样做的:char[,]temp_grid=grid.Clone()作为char[,],但还是一样
-------+--
++---+-+--
+--++--+-+
--+--+----
-+--+-----
----+-+---
---+--+++-
-+---++--+
+--+-----+
++---+--+-
-------------------------------------------------------------------
-++-+-+-+-
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
-++++++++-