C#如何在下一代中正确调用此方法



提前感谢您的帮助我整个星期都在挣扎,我的老师不会回应。

下面你会发现我目前的代码用于我的康威人生游戏,我无法调用实际运行模拟的方法;你能把我的代码拆开,帮我找到答案吗。

你不必给我实际的代码,只需告诉我如何获得它。我真的很想学习这一点,并在未来自己解决这个问题,但今晚到期了,我已经被困了一周

我需要在NextGeneration()中调用CountNeighborsToroidal(),它需要运行游戏,谢谢。

namespace The_Game_of_life
{
public partial class Form1 : Form
{

bool[,] universe = new bool[30, 30];
Color gridColor = Color.Black;
Color cellColor = Color.Gray;
Timer timer = new Timer();
int generations = 0;
public Form1()
{
InitializeComponent();
timer.Interval = 100; 
timer.Tick += Timer_Tick;
timer.Enabled = false; 
}
// Calculate the next generation of cells
private void NextGeneration()
{
CountNeighborsToroidal(cellRect.X, cellRect.Y);
// Increment generation count
generations++;
// Update status strip generations
toolStripStatusLabelGenerations.Text = "Generations = " + generations.ToString();
}
private void Timer_Tick(object sender, EventArgs e)
{
NextGeneration();
}
private void graphicsPanel1_Paint(object sender, PaintEventArgs e)
{

int cellWidth = graphicsPanel1.ClientSize.Width / universe.GetLength(0);

int cellHeight = graphicsPanel1.ClientSize.Height / universe.GetLength(1);

Pen gridPen = new Pen(gridColor, 1);
Brush cellBrush = new SolidBrush(cellColor);

for (int y = 0; y < universe.GetLength(1); y++)
{

for (int x = 0; x < universe.GetLength(0); x++)
{

Rectangle cellRect = Rectangle.Empty;
cellRect.X = x * cellWidth;
cellRect.Y = y * cellHeight;
cellRect.Width = cellWidth;
cellRect.Height = cellHeight;

if (universe[x, y] == true)
{
e.Graphics.FillRectangle(cellBrush, cellRect);
}

e.Graphics.DrawRectangle(gridPen, cellRect.X, cellRect.Y, cellRect.Width, cellRect.Height);
}
}

gridPen.Dispose();
cellBrush.Dispose();
}
private void graphicsPanel1_MouseClick(object sender, MouseEventArgs e)
{

if (e.Button == MouseButtons.Left)
{

int cellWidth = graphicsPanel1.ClientSize.Width / universe.GetLength(0);
int cellHeight = graphicsPanel1.ClientSize.Height / universe.GetLength(1);
int x = e.X / cellWidth;

int y = e.Y / cellHeight;
universe[x, y] = !universe[x, y];

graphicsPanel1.Invalidate();
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void openToolStripButton_Click(object sender, EventArgs e)
{

if (timer.Enabled)
{
timer.Enabled = false;
}
else
{
timer.Enabled = true;
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int y = 0; y < universe.GetLength(1); y++)
{
for (int x = 0; x < universe.GetLength(0); x++)
{
universe[x, y] = false;
}
}
graphicsPanel1.Invalidate();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
NextGeneration();
}
private int CountNeighborsToroidal(int x, int y)
{
int count = 0;
int xLen = universe.GetLength(0);
int yLen = universe.GetLength(1);
for (int yOffset = -1; yOffset <= 1; yOffset++)
{
for (int xOffset = -1; xOffset <= 1; xOffset++)
{
int xCheck = x + xOffset;
int yCheck = y + yOffset;
if ((xCheck - 1 >= 0 && yCheck - 1 > 0) && universe[xCheck - 1, yCheck - 1] == true)
{
count++;
}
if ((xCheck - 1 >= 0) && universe[xCheck - 1, yCheck] == true)
{
count++;
}
if ((xCheck - 1 >= 0 && yCheck + 1 < yLen) && universe[xCheck - 1, yCheck + 1] == true)
{
count++;
}
if ((xCheck - 1 >= 0) && universe[xCheck, yCheck - 1] == true)
{
count++;
}
if ((xCheck + 1 < yLen) && universe[xCheck, yCheck + 1] == true)
{
count++;
}
if ((xCheck + 1 < xLen && yCheck - 1 >= 0) && universe[xCheck + 1, yCheck - 1] == true)
{
count++;
}
if ((xCheck + 1 < xLen) && universe[xCheck + 1, yCheck] == true)
{
count++;
}
if ((xCheck + 1 < xLen && yCheck + 1 < yLen) && universe[xCheck + 1, yCheck + 1] == true)
{
count++;
}
if (universe[xCheck, yCheck] == true)
{
count++;
}
}
}
return count;
}
}
}

我认为从NextGeneration函数调用CountNeighborsTorooid函数没有任何问题。我在这里看到的唯一问题是,参数cellRect.x和cellRect.y没有在这段代码中定义,所以你总是会得到一个错误。。。。尝试更改CountNeighborsToroid(cellRect.X,cellRect.Y(;使用一些随机int值,看看它是否有效:

private void NextGeneration()
{
CountNeighborsToroidal(22, 12);
// Increment generation count
generations++;
// Update status strip generations
toolStripStatusLabelGenerations.Text = "Generations = " + generations.ToString();
}

最新更新