Zie Scherp - Oefeningen
Handboek Zie Scherp Scherper 2e editieThe Corona filesHandboek backupOefeningen backup
  • Welkom
  • Oefeningen H1 tot en met H5
    • Oefeningen H1
    • Oefeningen H2
    • Oefeningen H3
    • Oefeningen H4
    • Oefeningen H5
    • Gecombineerde oefeningen
      • Opgave 1
      • Opgave 2
      • Opgave 3
      • Opgave 4
      • Opgave 5
  • Oefeningen H6 tot en met H8
    • Oefeningen H6
      • Week 1
      • Week 2
    • Oefeningen H7
      • Week 1
      • Week 2
    • Oefeningen H8
      • Week 1
      • Week 2
    • Voorbeeld vaardigheidsproeven
      • Opgave 1 (2022)
      • Opgave 2 (2019)
      • Opgave 3 (2019)
      • Opgave 4 (2020)
      • Opgave 5 (2020)
      • Opgave 6 (2021)
      • Opgave 7 (2022)
      • Opgave 8 (2022)
      • Opgave 9 (2023)
      • Opgave 10 (2024)
      • Opgave 11 (2024)
      • Opgave 12 (2025)
    • Gecombineerde opgaven
      • Console Matrix
      • Unicode filmpjes maken met loops
      • Unicode filmpjes maken met methoden
      • Music Maestro
      • Fun with methods: een verhaalgenerator
      • Tekst-gebaseerd Maze game
      • Conway game of life
      • How to make your console app look cool
  • Oefeningen H9 tot en met H12
    • Oefeningen H9
    • Oefeningen H10
    • Oefeningen H11
    • Oefeningen H12
    • Gecombineerde oefeningen
      • Opgave 1
      • Opgave 2
      • Opgave 3
      • Opgave 4
      • Opgave 5
  • Oefeningen H13 tot en met H18
    • Oefeningen H13
    • Oefeningen H14
    • Oefeningen H15
    • Oefeningen H16
    • Oefeningen H17
    • Oefeningen H18
    • Voorbeeld vaardigheidsproeven
      • Opgave 1 (2019)
      • Opgave 2 (2019)
      • Opgave 3 (2020)
      • Opgave 4 (2021)
      • Opgave 5 (2021)
      • Opgave 6 (2022)
      • Opgave 7 (2022)
      • Opgave 8 (2023)
      • Opgave 9 (2024)
    • Gecombineerde oefeningen
      • OO Textbased Game
      • Map Maker
      • Mapmaker & WPF
      • Magic The Gathering API
      • C# Game remakes
Powered by GitBook
On this page
  • Conway game of life
  • Oplossing

Was this helpful?

  1. Oefeningen H6 tot en met H8
  2. Gecombineerde opgaven

Conway game of life

PreviousTekst-gebaseerd Maze gameNextHow to make your console app look cool

Last updated 7 months ago

Was this helpful?

Conway game of life

Life (of Conway Game of Life) is een leuke manier om 2D arrays te gebruiken om 'leven' te simuleren. Afhankelijk van enkele eenvoudige regels wordt beslist of een cel (1 element in de array) wel of niet overleeft naar de volgende generatie. De regels zijn gebaseerd op de 8 omliggende plekken rond de cel die ofwel leeg zijn ofwel een andere cel bevatten:

  • Any live cell with fewer than two live neighbors dies, as if by underpopulation.

  • Any live cell with two or three live neighbors lives on to the next generation.

  • Any live cell with more than three live neighbors dies, as if by overpopulation.

  • Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Meer uitleg:

Kan je zelf een simulator maken die telkens generatie per generatie op het scherm toont?

Oplossing

static void Main(string[] args)
{
  
    int[,] playfield = new int[50, 250];
    ResetField(playfield);

    while (true)
    {

        ShowField(playfield);
        Console.WriteLine("Enter for next generation");
        Console.ReadLine();
        playfield = GenerateNextLevel(playfield);
        Console.Clear();
    }

}

private static void ShowField(int[,] field)
{
    StringBuilder str = new StringBuilder();
    int xl = field.GetLength(0);
    int yl = field.GetLength(1);
    for (int i = 1; i < xl - 1; i++)
    {
        for (int j = 1; j < yl - 1; j++)
        {
            if (field[i, j] == 1)
            {
                str.Append("X");
            }
            else
            {
                str.Append(" ");
            }
        }
        str.Append(Environment.NewLine);
    }
    Console.WriteLine(str.ToString());
}

private static int[,] GenerateNextLevel(int[,] playfield)
{
    int[,] newfield = new int[playfield.GetLength(0), playfield.GetLength(1)];


    for (int i = 1; i < playfield.GetLength(0) - 1; i++)
    {
        for (int j = 1; j < playfield.GetLength(1) - 1; j++)
        {
            int neighbours = CountNeighbours(playfield, i, j);
            if (playfield[i, j] > 0)
            {
                if (neighbours < 2)
                    newfield[i, j] = 0;
                else if (neighbours == 2 || neighbours == 3)
                    newfield[i, j] = 1;
                else if (neighbours > 3)
                    newfield[i, j] = 0;
            }
            else if (neighbours == 3)
                newfield[i, j] = 1;
        }
    }
    return newfield;
}

private static int CountNeighbours(int[,] playfield, int orx, int ory)
{

    int sum = 0;
    for (int i = orx - 1; i < orx + 2; i++)
    {
        for (int j = ory - 1; j < ory + 2; j++)
        {
            if (!(i == orx && j == ory))
                sum += playfield[i, j];
        }
    }
    return sum;
}

static void ResetField(int[,] field, int randomcells = 100)
{
    for (int i = 0; i < field.GetLength(0); i++)
    {
        for (int j = 0; j < field.GetLength(1); j++)
        {
            field[i, j] = 0;
        }
    }

    Random r = new Random();
    for (int i = 0; i < randomcells; i++)
    {
        int x = r.Next(1, field.GetLength(0) - 2);
        int y = r.Next(1, field.GetLength(1) - 2);
        field[x, y] = 1;
    }



}
        ```
wikipedia