首页 > 解决方案 > 为什么使用带有二维数组的元胞自动机会产生不好的结果?

问题描述

我正在尝试使用元胞自动机制作程序生成的 map/2d_array。

我找到了一个很好的视频来解释元胞自动机并跟随它。视频链接:https ://www.youtube.com/watch?v=slTEz6555Ts

但由于某种原因,我只能得到一个具有相同值的完全填充的二维数组,而不是像二维数组那样的程序生成的地图。我试图调试它,看看有什么问题,由于某种原因,这个 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);
            }
        }
    }
}

结果:

-------+--
++---+-+--
+--++--+-+
--+--+----
-+--+-----
----+-+---
---+--+++-
-+---++--+
+--+-----+
++---+--+-
-------------------------------------------------------------------
-++-+-+-+-
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
-++++++++-

标签: c#procedural-generationcellular-automata

解决方案


首先,J & K 的 For 循环应该是 <= 而不是 <。第二个问题是您必须创建地图的深层副本作为参考。您不能为此使用相同的映射来写入和读取,因为一次迭代中的先前步骤可能会改变迭代中另一个单元格的结果。逐个调试步骤即可看到这一点。基本上,有一个数组来读取单元状态,然后另一个来写入新的单元状态。然后在迭代完成后,可以将读取单元设置为与写入单元相同


推荐阅读