Advertisement
Guest User

Untitled

a guest
Nov 21st, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Threading;
  4.  
  5. namespace GameOfLife
  6. {
  7.     static class Program
  8.     {
  9.         const int Rows = 30;
  10.         const int Columns = 30;
  11.  
  12.         static bool runSimulation = true;
  13.  
  14.         public static void Main()
  15.         {
  16.             var grid = new Status[Rows, Columns];
  17.  
  18.             // randomly initialize our grid
  19.             for (var row = 0; row < Rows; row++)
  20.             {
  21.                 for (var column = 0; column < Columns; column++)
  22.                 {
  23.                     var random = new Random();
  24.                     //grid[row, column] = (Status) RandomNumberGenerator.GetInt32(0, 2);
  25.                     grid[row, column] = (Status)random.Next(0, 2);
  26.                 }
  27.             }
  28.  
  29.             Console.CancelKeyPress += (sender, args) =>
  30.             {
  31.                 runSimulation = false;
  32.                 Console.WriteLine("\nEnd of the simulation");
  33.             };
  34.  
  35.             // let's give our console
  36.             // a good scrubbing
  37.             Console.Clear();
  38.  
  39.             // Displaying the grid
  40.             while (runSimulation)
  41.             {
  42.                 Print(grid);
  43.                 grid = NextGeneration(grid);
  44.             }
  45.         }
  46.  
  47.         private static Status[,] NextGeneration(Status[,] currentGrid)
  48.         {
  49.             var nextGeneration = new Status[Rows, Columns];
  50.  
  51.             // Loop through every cell
  52.             for (var row = 1; row < Rows - 1; row++)
  53.                 for (var column = 1; column < Columns - 1; column++)
  54.                 {
  55.                     // find your alive neighbors
  56.                     var aliveNeighbors = 0;
  57.                     for (var i = -1; i <= 1; i++)
  58.                     {
  59.                         for (var j = -1; j <= 1; j++)
  60.                         {
  61.                             aliveNeighbors += currentGrid[row + i, column + j] == Status.Alive ? 1 : 0;
  62.                         }
  63.                     }
  64.  
  65.                     var currentCell = currentGrid[row, column];
  66.  
  67.                     // The cell needs to be subtracted
  68.                     // from its neighbors as it was  
  69.                     // counted before
  70.                     aliveNeighbors -= currentCell == Status.Alive ? 1 : 0;
  71.  
  72.                     // Implementing the Rules of Life
  73.  
  74.                     // Cell is lonely and dies
  75.                     if (currentCell == Status.Alive && aliveNeighbors < 2)
  76.                     {
  77.                         nextGeneration[row, column] = Status.Dead;
  78.                     }
  79.  
  80.                     // Cell dies due to over population
  81.                     else if (currentCell == Status.Alive && aliveNeighbors > 3)
  82.                     {
  83.                         nextGeneration[row, column] = Status.Dead;
  84.                     }
  85.  
  86.                     // A new cell is born
  87.                     else if (currentCell == Status.Dead && aliveNeighbors == 3)
  88.                     {
  89.                         nextGeneration[row, column] = Status.Alive;
  90.                     }
  91.                     // stays the same
  92.                     else
  93.                     {
  94.                         nextGeneration[row, column] = currentCell;
  95.                     }
  96.                 }
  97.             return nextGeneration;
  98.         }
  99.  
  100.         private static void Print(Status[,] future, int timeout = 250) //4 iterations per second
  101.         {
  102.             var stringBuilder = new StringBuilder();
  103.             for (var row = 0; row < Rows; row++)
  104.             {
  105.                 for (var column = 0; column < Columns; column++)
  106.                 {
  107.                     var cell = future[row, column];
  108.                     stringBuilder.Append(cell == Status.Alive ? "O" : " ");
  109.                 }
  110.                 stringBuilder.Append("\n");
  111.             }
  112.  
  113.             Console.CursorVisible = false;
  114.             Console.SetCursorPosition(0, 0);
  115.             Console.Write(stringBuilder.ToString());
  116.             Thread.Sleep(timeout);
  117.         }
  118.     }
  119.  
  120.     public enum Status
  121.     {
  122.         Dead,
  123.         Alive,
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement