因此,我正在尝试编写Conway的《生活游戏》,但我不知道该如何处理围绕它的部分?有任何想法吗?我已将区域标记为“模拟代码”
我不需要您为我做这件事,我只需要一个关于如何执行此过程部分的提示,因为我不知道如何检查阵列周围的区域,以查看有多少个活点存在,等等。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Gameoflife
{
class Program
{
static int[,] currentgeneration = new int[Console.LargestWindowHeight + 1, Console.LargestWindowWidth + 1];
static int[,] nextgeneration = new int[Console.LargestWindowHeight + 1, Console.LargestWindowWidth + 1];
static void Main(string[] args)
{
ConsoleColor cellcolour = ConsoleColor.Green;
ConsoleColor backgroundcolour = ConsoleColor.Black;
Introduction();
Console.Title = "GAME OF LIFE SIMULATION";
int worldwidth = Console.LargestWindowWidth;
int worldheight = Console.LargestWindowHeight;
Console.SetWindowSize(worldwidth, worldheight);
Console.SetWindowPosition(0, 0);
setupworld(worldwidth, worldheight, cellcolour, backgroundcolour);
int generation = 0;
DrawWorld(worldwidth, worldheight, cellcolour, backgroundcolour, generation);
// SIMULATION CODE HERE!
generation++;
for (int row = 1; row < worldheight; row++)
{
for (int col = 1; col < worldwidth; col++)
{
currentgeneration[row, col] = nextgeneration[row, col];
}
}
DrawWorld(worldwidth, worldheight, cellcolour, backgroundcolour, generation);
}
//---------------------------------------------------------------------------------------------------
static void Introduction()
{
Console.WriteLine("CONWAY'S GAME OF LIFE");
Console.WriteLine();
Console.WriteLine("To set up your starting world use the following keys..");
Console.WriteLine("Arrow keys - move around the screen");
Console.WriteLine("Space Bar - places a cell in that location");
Console.WriteLine("Escape key - To end setup");
Console.WriteLine();
Console.WriteLine("Hit any key to continue");
Console.ReadKey();
Console.Clear();
}
//-------------------------------------------------------------------------------------------------
static void setupworld(int worldwidth, int worldheight, ConsoleColor cellcolour, ConsoleColor backgroundcolour)
{
Boolean setupcomplete = false;
int cursorx = 1;
int cursory = 1;
Console.SetCursorPosition(cursorx, cursory);
while (!setupcomplete)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo cki = Console.ReadKey();
switch (cki.Key)
{
case ConsoleKey.UpArrow:
if (cursory > 1)
{
cursory = cursory - 1;
}
break;
case ConsoleKey.DownArrow:
if (cursory < Console.LargestWindowHeight - 1)
{
cursory = cursory + 1;
}
break;
case ConsoleKey.LeftArrow:
if (cursorx > 1)
{
cursorx = cursorx - 1;
}
break;
case ConsoleKey.RightArrow:
if (cursorx < Console.LargestWindowWidth - 1)
{
cursorx = cursorx + 1;
}
break;
case ConsoleKey.Spacebar:
currentgeneration[cursory, cursorx] = 1;
break;
case ConsoleKey.Escape:
setupcomplete = true;
break;
}
DrawWorld(worldwidth, worldheight, cellcolour, backgroundcolour, 0);
Console.SetCursorPosition(cursorx, cursory);
}
}
Console.SetCursorPosition(15, 0);
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write("Press Any key to now start the simulation");
Console.ReadKey();
}
//-----------------------------------------------------------------------------------------------------
static void DrawWorld(int worldwidth, int worldheight, ConsoleColor cellcolour, ConsoleColor backgroundcolour, int generation)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.Clear();
Console.Write("Generation: {0}", generation);
for (int r = 0; r < worldheight; r++)
{
for (int c = 0; c < worldwidth; c++)
{
if (currentgeneration[r, c] == 1)
{
Console.SetCursorPosition(c, r);
Console.BackgroundColor = cellcolour;
Console.Write(" ");
Console.BackgroundColor = backgroundcolour;
}
}
}
}
}
}
答案 0 :(得分:1)
首先以简单的英语写下规则:
您现在的工作是将这些简单的英语语句转换为代码。为了使代码正常工作,您必须知道它需要什么输入。因此,我们需要遍历以下语句以查看其计算结果:
基于此分析,我们现在知道我们的代码将需要两个输入:
要计算该单元格是否还存在,我假设您要查看currentgeneration[r,c]
。
要计算有多少个相邻小区还活着,请查看以下所有内容:
currentgeneration[r-1,c-1]
currentgeneration[r-1,c ]
currentgeneration[r-1,c+1]
currentgeneration[r ,c-1]
currentgeneration[r ,c ]
currentgeneration[r ,c+1]
currentgeneration[r+1,c-1]
currentgeneration[r+1,c ]
currentgeneration[r+1,c+1]
因此,您的代码将需要检查所有这些元素并计算活动元素。
然后您可以编写一个接受两个输入并确定某个单元是否存在的函数:
bool IsAlive(bool wasAlive, int count)
{
if (wasAlive && count<2) return false; // Any live cell with fewer than two live neighbors dies, as if by underpopulation.
if (wasAlive && (count==2 || count == 3)) return true; // Any live cell with two or three live neighbors lives on to the next generation.
if (wasAlive && count>=3) return false; // Any live cell with more than three live neighbors dies, as if by overpopulation.
if (!wasAlive && count == 3) return true; // Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
}
现在我们可以编写一个方法来检索要传递给函数的数据:
bool IsAlive(int r, int c)
{
var count = currentgeneration[r-1,c-1]
+ currentgeneration[r-1,c ]
+ currentgeneration[r-1,c+1]
+ currentgeneration[r ,c-1]
+ currentgeneration[r ,c ]
+ currentgeneration[r ,c+1]
+ currentgeneration[r+1,c-1]
+ currentgeneration[r+1,c ]
+ currentgeneration[r+1,c+1];
var isAlive = ( currentgeneration[r,c] == 1);
return IsAlive(isAlive, count);
}
答案 1 :(得分:0)
我将再添加2个数组,一个数组跟踪哪些单元格处于活动状态(bool
),另一个数组跟踪一个单元格具有多少邻居(int
)。
在每次更新时,在活动的单元格之间循环,并在其周围的位置增加相邻数组的增量。完成后,只需根据该位置的邻居数量杀死,产生或留下细胞即可。
以下是该程序在JavaScript中的示例:
添加邻居
// Iterate through the alive array
// If the cell at that position is alive...
if (alive[x][y] == true)
{
for (X = -1; X <= 1; X++)
{
for (Y = -1; Y <= 1; Y++)
{
// Get the coordinates of the positions around it
dx = x + X;
dy = y + Y;
// If the coordinates are valid
if (dx >= 0 && dy >= 0 && dx < ROW && dy < COLUMN)
{
// Increment the neighbour array
neighbours[dx][dy] += 1;
}
}
}
}
更新活动阵列
// If the cell is alive...
if (alive[x][y] == true)
{
// Remove the bias (the UpdateNeighbour function adds neighbour in a 3x3 area, not only the neighbours.)
numNeighbours -= 1;
// If it has less than 2 neighbours...
if (numNeighbours < 2)
{
// It dies
alive[i][y] = false;
}
// If it has 2 or 3 neighbours...
else if(numNeighbours < 4)
{
// It survives
alive[i][y] = true;
}
// If it has more than 4 neighbours...
else if (numNeighbours >= 4)
{
// It dies
alive[i][y] = false;
}
}
// If the cell isn't alive but has exactly 3 neighbours...
else if (alive[x][y] == false && numNeighbours == 3)
{
// Spawn new cell
alive[x][y] = true;
}