【发布时间】:2014-10-25 08:34:33
【问题描述】:
我花了很多时间才弄清楚这一点。
我有 2 个多维数组:
int[,] OldGeneration = new int[WidthX + 1, HeightY + 1];
int[,] NextGeneration = new int[WidthX + 1, HeightY + 1];
稍后在我的代码中,在“NextGeneration”中设置一些值后,我使数组包含相同的值。
OldGeneration = NextGeneration;
当我在运行程序时检查值时,它的工作原理。
然后我更改“NextGeneration”的一个值,当我这样做时,“OldGeneration”中的相同值也将被更改。
你能告诉我为什么吗?
using System;
namespace CGoL
{
class CGoL_Base
{
static void Main(string[] args)
{
//Declare and define (initialize) Variables used by the Program
int WidthX = 5, HeightY = 5, Iterations = 5, Speed = 1000, Random = 0, CellsAlive = 0;
//Declare Multidimensional Arrays for actual generation and next generation
int[,] OldGeneration = new int[WidthX + 1, HeightY + 1];
int[,] NextGeneration = new int[WidthX + 1, HeightY + 1];
//########### Initialize "Game Board" ##########################
//Set Game Board to contain only dead cells
for (int y = 1; y <= HeightY; y++)
{
for (int x = 1; x <= WidthX; x++)
{
NextGeneration[x, y] = 0;
}
}
//Set pattern for oscillating (moving) structure with 3 living cells (Can be changed at will)
NextGeneration[3, 2] = 1;
NextGeneration[3, 3] = 1;
NextGeneration[3, 4] = 1;
//Set OldGeneration equal with NextGeneration so that the calculation can work
OldGeneration = NextGeneration;
//##################################################################
//Start the iterationcounter
for (int Iteration = 1; Iteration <= Iterations; Iteration++)
{
//########### Calculate actual generation ######################
//Calculate how the Game Board will change with the usual CGoL rules
if (Iteration >= 2) //Without this, the initialization above will not work
{
for (int y = 1; y <= HeightY; y++)
{
for (int x = 1; x <= WidthX; x++)
{
//########### Check surrounding Cells ##########################
//Check how much cells, surrounding the actual cell, are still alive
//to calculate later how the Game Board will change
CellsAlive = 0;
for (int i = -1; i <= 1; i++)
{
for (int n = -1; n <= 1; n++)
{
if (i == 0 && n == 0)
{
continue;
}
//Check if some Array Index will be out of Array Range (for example when index is smaller than 0)
if (x + n == 0 || x + n > WidthX || y + i == 0 || y + i > HeightY)
{
continue;
}
if (OldGeneration[(x + n), (y + i)] == 1)
{
CellsAlive++;
}
}
}
//##################################################################
//If a dead cell got 3 living neighbours, the cell will become alive in the next generation
if (OldGeneration[x, y] == 0 && CellsAlive == 3)
{
NextGeneration[x, y] = 1;
}
//If a living cell got less than 2 living neighbours, the cell will die in the next generation
else if (OldGeneration[x, y] == 1 && CellsAlive < 2)
{
NextGeneration[x, y] = 0; //OldGeneration[x, y] will be changed to 0 even if it is not written here????? why?
}
//If a living cell got 2 or 3 living neighbours, the cell will stay alive in the next generation
else if (OldGeneration[x, y] == 1 && (CellsAlive == 2 || CellsAlive == 3))
{
NextGeneration[x, y] = 1;
}
//If a living cell got more than 3 living neighbours, the cell will die in the next generation
else if (OldGeneration[x, y] == 1 && CellsAlive > 3)
{
NextGeneration[x, y] = 0;
}
}
}
Console.ReadKey();
}
//##################################################################
//########### Draw the "Game Board" ############################
//Makes the console window Empty :)
Console.Clear();
//"for" is making new rows with Console.Write("\n");
for (int y = 1; y <= HeightY; y++)
{
//"for" is writing "O"'s in one row
for (int x = 1; x <= WidthX; x++)
{
if (NextGeneration[x, y] == 1)
{
Console.Write("O ");
}
else
{
Console.Write(" ");
}
}
Console.Write("\n");
}
Console.WriteLine("Iteration: {0}", Iteration);
System.Threading.Thread.Sleep(Speed);
//At the end, make the actual generation same like the old generation to be calculated next
OldGeneration = NextGeneration;
//##################################################################
}
Console.ReadKey();
}
}
}
【问题讨论】:
标签: c# arrays multidimensional-array conways-game-of-life