Игра "Жизнь", программа C Sharp - PullRequest
0 голосов
/ 26 февраля 2019

Итак, я пытаюсь запрограммировать игру жизни Конвея, но я не знаю, как приблизиться к той части, где она проверяется вокруг них?Есть идеи?Я обозначил область «КОД МОДЕЛИРОВАНИЯ ЗДЕСЬ»

Мне не нужно, чтобы вы делали это для меня, мне просто нужен совет о том, как подойти к этой части процесса, так как я понятия не имею, какпроверить область вокруг массива, узнать, сколько существует живых точек и т. д.

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

Ответы [ 2 ]

0 голосов
/ 26 февраля 2019

Начните с записи правил на простом английском языке:

  • Умирает любая живая клетка, у которой меньше двух живых соседей, как если бы она была из-за недостаточного населения.
  • Любая живая клетка с двумя илитри живых соседа живут в следующем поколении.
  • любая живая клетка с более чем тремя живыми соседями умирает, как если бы она была перенаселена.
  • любая мертвая клетка с ровно тремя живыми соседями становится живой клеткойКак при воспроизведении.

Теперь ваша задача - преобразовать эти простые английские выражения в код.Чтобы код работал, вы должны знать, какие входные данные ему нужны.Таким образом, нам нужно просмотреть операторы, чтобы увидеть, что он вычисляет:

  • Любая живая ячейка с менее чем двумя живыми соседями умирает, как если бымалонаселенность.
  • Любая живая клетка с двумя или тремя живыми соседями живет для следующего поколения.
  • Любая жива ячейка с более чем тремя живыми соседями умирает, как будто из-за перенаселения.
  • любая мертвая клетка с ровно тремя живыми соседями становится живой клеткойкак при воспроизведении.

Основываясь на этом анализе, теперь мы знаем, что нашему коду потребуются два ввода:

  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 голосов
/ 26 февраля 2019

Я бы добавил еще 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;
}
...