c# - 为什么使用带有二维数组的元胞自动机会产生不好的结果?
问题描述
我正在尝试使用元胞自动机制作程序生成的 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);
}
}
}
}
结果:
-------+--
++---+-+--
+--++--+-+
--+--+----
-+--+-----
----+-+---
---+--+++-
-+---++--+
+--+-----+
++---+--+-
-------------------------------------------------------------------
-++-+-+-+-
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
-++++++++-
解决方案
首先,J & K 的 For 循环应该是 <= 而不是 <。第二个问题是您必须创建地图的深层副本作为参考。您不能为此使用相同的映射来写入和读取,因为一次迭代中的先前步骤可能会改变迭代中另一个单元格的结果。逐个调试步骤即可看到这一点。基本上,有一个数组来读取单元状态,然后另一个来写入新的单元状态。然后在迭代完成后,可以将读取单元设置为与写入单元相同
推荐阅读
- gekko - Gekko 长期性能
- javascript - 如何将一组日期范围分组为具有最小时间间隔的较小组?
- c# - 将命令行参数传递给 C# .NET 应用程序而不使用 UWP 应用程序内的清单文件
- matlab - MATLAB - 从结构中获取单个字段名称
- ios - iOS中的Firebase注册错误通过本机反应
- google-sheets-api - 如何获得有效期超过 24 小时的令牌
- powershell - 在 powershell 中复制脚本时遇到问题
- azure-devops - 将图形文件上传到 devops 以在 markdown 中使用
- c# - 使用身份服务器 4 使用有效的访问令牌获取 401 Unauthorized
- r - 在 R 中的 log10 scale_y_continuous 中设置 ylim