c# - 生命游戏程序 C Sharp
问题描述
所以我正在尝试编写康威的生命游戏,但我不知道如何处理它检查它们的部分?有任何想法吗?我已将区域标记为“此处的模拟代码”
我不需要你为我做这件事,我只需要一个关于如何处理这部分过程的提示,因为我不知道如何检查阵列周围的区域,看看有多少活点存在,等等.
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;
}
}
}
}
}
}
解决方案
首先用简单的英语写下规则:
- 任何少于两个活邻居的活细胞都会死亡,就像人口不足一样。
- 任何有两三个活邻居的活细胞都可以活到下一代。
- 任何有超过三个活邻居的活细胞都会死亡,就像人口过剩一样。
- 任何只有三个活邻居的死细胞都会变成活细胞,就像通过繁殖一样。
您现在的工作是将这些简单的英语语句转换为代码。要使代码正常工作,您必须知道它需要哪些输入。所以我们需要检查这些语句以查看它的计算结果:
- 任何少于两个活邻居的活细胞都会死亡,就像人口不足一样。
- 任何有两三个活邻居的活细胞都可以活到下一代。
- 任何有超过三个活邻居的活细胞都会死亡,就像人口过剩一样。
- 任何只有三个活邻居的死细胞都会变成活细胞,就像通过繁殖一样。
基于此分析,我们现在知道我们的代码将需要两个输入:
- 它需要知道当前单元格是否还活着
- 它需要知道有多少相邻的细胞是活着的。
要计算细胞是否还活着,我假设你会查看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);
}
推荐阅读
- reactjs - CORS 策略已阻止从源“http://localhost:3000”访问“http://127.0.0.1:8000/blog/1”获取
- ruby-on-rails - Ruby 的 mysql2 gem 使用了不正确的数据库包(MariaDB 而不是 MySQL)
- c++ - 将 gtkmm4 FileChooserNative 的 signal_response 连接到函数时生成错误
- css - Bootstrap 4 卡在多行上响应相同的高度
- reactjs - 将 reactjs 应用程序部署到 github 页面后得到一个空白页面
- c++ - Destructor crashes when working with pointers in Visual Studio
- python - Sqlite3 关系数据库
- pandas - 如何在 str 列 pandas 数据框中放置一个掩码?
- javascript - Javascript仅检查数组的第一项
- goland - 升级到 Goland 2021.2 “未发现问题”大量 RED 文件