C# で Game of Life プログラムを作成しています。構造体の 2D 配列を使用しています。ランダムメソッドを表示すると、隣人のアルゴリズムか何かが間違っているようです。何世代にもわたって、ランダムなセルは、想定されていないときに「生きている」ようになります。何か助けはありますか?
public struct cellDetail
{
public int curGenStatus;
public int nextGenStatus;
public int age;
}
public class Class1
{
static cellDetail[,] Generations(cellDetail[,] cells)
{
int neighbours = 0;
for (int i = 0; i < 40; i++)
for (int j = 0; j < 60; j++)
cells[i, j].nextGenStatus = 0;
for (int row = 0; row < 39; row++)
for (int col = 0; col < 59; col++)
{
neighbours = 0;
if (row > 0 && col > 0)
{
if (cells[row - 1, col - 1].curGenStatus > 0)
neighbours++;
if (cells[row - 1, col].curGenStatus > 0)
neighbours++;
if (cells[row - 1, col + 1].curGenStatus > 0)
neighbours++;
if (cells[row, col - 1].curGenStatus > 0)
neighbours++;
if (cells[row, col + 1].curGenStatus > 0)
neighbours++;
if (cells[row + 1, col - 1].curGenStatus > 0)
neighbours++;
}
if (cells[row + 1, col].curGenStatus > 0)
neighbours++;
if (cells[row + 1, col + 1].curGenStatus > 0)
neighbours++;
if (neighbours < 2)
cells[row, col].nextGenStatus = 0;
if (neighbours > 3)
cells[row, col].nextGenStatus = 0;
if ((neighbours == 2 || neighbours == 3) && cells[row, col].curGenStatus > 0)
cells[row, col].nextGenStatus = 1;
if (neighbours == 3 && cells[row, col].curGenStatus == 0)
cells[row, col].nextGenStatus = 1;
}
for (int i = 0; i < 40; i++)
for (int j = 0; j < 60; j++)
cells[i, j].curGenStatus = cells[i, j].nextGenStatus;
return cells;
}
static void PrintCells(cellDetail[,] cells)
{
for (int row = 0; row < 40; row++)
{
for (int col = 0; col < 60; col++)
{
if (cells[row, col].curGenStatus != 0)
{
cells[row, col].curGenStatus = (char)30;
Console.Write((char)cells[row, col].curGenStatus);
}
else
Console.Write(" ");
}
}
}
public static void Random(int numOfGenerations)
{
cellDetail[,] cells = new cellDetail[40,60];
Random rand = new Random();
for (int row = 0; row < 40; row++)
for (int col = 0; col < 60; col++)
cells[row, col].curGenStatus = rand.Next(0, 2);
for (int i = 0; i < numOfGenerations; i++)
{
Console.ForegroundColor = (ConsoleColor.Green);
Generations(cells);
Console.SetCursorPosition(0, 3);
PrintCells(cells);
}
}
}