生命游戏计划C夏普

问题描述 投票:-3回答:2

所以我正在尝试编写康威的生命游戏,但我不知道如何处理它周围的部分?有任何想法吗?我在这里标注了“SIMULATION CODE”

我不需要你为我做这件事,我只需要提示如何处理这部分过程,因为我不知道如何检查阵列周围的区域,看看有多少活点存在等等。

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;
                }
            }  
        } 
    }
}
} 
c# conways-game-of-life
2个回答
1
投票

首先用简单的英语写下规则:

  • 任何活着的邻居少于两个的活细胞就会死亡,就像人口不足一样。
  • 任何有两三个活邻居的活细胞都会留在下一代。
  • 任何有三个以上活着的邻居的活细胞都会死亡,好像是人口过剩。
  • 具有正好三个活邻居的任何死细胞变成活细胞,就好像通过繁殖一样。

您现在的工作是将这些简单的英语语句转换为代码。要使代码生效,您必须知道它需要什么输入。因此,我们需要查看语句以查看它的计算结果:

  • 任何活着的邻居少于两个的活细胞就会死亡,就像人口不足一样。
  • 任何有两三个活邻居的活细胞都会留在下一代。
  • 任何有三个以上活着的邻居的活细胞都会死亡,好像是人口过剩。
  • 具有正好三个活邻居的任何死细胞变成活细胞,就好像通过繁殖一样。

基于此分析,我们现在知道我们的代码需要两个输入:

  1. 它需要知道当前的细胞是否存活
  2. 它需要知道有多少相邻的细胞存活。

为了计算细胞是否存活,我假设你看看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);
}

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;
            }
        }
    }
}

更新alive数组

// 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;
}
© www.soinside.com 2019 - 2024. All rights reserved.