Advertisement
Placido_GDD

GameOfLife(gridInstantiater)

Jan 5th, 2022
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.18 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Game : MonoBehaviour
  6. {
  7.     public int aliveChance;
  8.     public float speed;
  9.     public bool simEnabled = true;
  10.     public int screenWidth;
  11.     public int screenHeight;
  12.     public Color CellAliveColorVal;
  13.     public Color CellDeadColorVal;
  14.     /// <summary>
  15.     /// private variables
  16.     /// </summary>
  17.     private float refreshRate;
  18.     public static int Screen_Width = 64; //1024 pixels
  19.     public static int Screen_Height = 48; //768 pixels
  20.     public Cell[,] grid = new Cell[Screen_Width, Screen_Height];
  21.     // Start is called before the first frame update
  22.     void Start()
  23.     {
  24.         screenHeight = Screen_Height;
  25.         screenWidth = Screen_Width;
  26.  
  27.     refreshRate = speed * 0.01f;
  28.      PlaceCells();
  29.      InvokeRepeating("GameIteration",0,refreshRate);
  30.     }
  31.  
  32.     // Update is called once per frame
  33.     void Update()
  34.     {
  35.        
  36.     }
  37.  
  38.     void GameIteration()
  39.     {
  40.        if (simEnabled)
  41.         {
  42.             CountNeighbors();
  43.             PopControl();
  44.         }
  45.    
  46.     }
  47.  
  48.     void PlaceCells()
  49.     {
  50.         for (int y = 0; y < Screen_Height; y++)
  51.         {
  52.             for (int x = 0; x < Screen_Width; x++)
  53.             {
  54.                 Cell cell = Instantiate(Resources.Load("Prefabs/Cell", typeof(Cell)), new Vector2(x, y), Quaternion.identity) as Cell; //instantiate the cell
  55.                 grid[x, y] = cell;
  56.                 grid[x, y].SetAlive(RandomAliveCell());
  57.                 grid[x, y].CellAliveColor = CellAliveColorVal;
  58.                 grid[x, y].CellDeadColor = CellDeadColorVal;
  59.             }
  60.         }
  61.        
  62.     }
  63.     bool RandomAliveCell() //determines if a cell is alive or not randomly
  64.     {
  65.         int rand = UnityEngine.Random.Range(0, 100);
  66.         if (rand > aliveChance)
  67.         {
  68.             return true;
  69.         }
  70.         return false;
  71.     }
  72.  
  73.     void PopControl()
  74.     {
  75.         for (int y = 0; y < Screen_Height; ++y)
  76.         {
  77.             for (int x = 0; x < Screen_Width; ++x)
  78.             {
  79.                 //-Rules
  80.                 //any live cell with 2 or 3 alive neighbors is active
  81.                 //any dead cell with 2 or 3 alive neighbors becomes active
  82.                 //all other live cells become inactive in the next generation
  83.                 //all other inactive cells stay dead
  84.                 if (grid[x, y].isAlive)
  85.                 {
  86.                     //Cell is active
  87.                     if (grid[x, y].numNeighbors != 2 && grid[x, y].numNeighbors != 3)
  88.                     {
  89.                         grid[x,y].SetAlive(false);
  90.                     }
  91.                 }
  92.                 else
  93.                 {
  94.                     //Cell is inactive
  95.                     if (grid[x, y].numNeighbors == 3)
  96.                     {
  97.                         grid[x, y].SetAlive(true);
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.     }
  103.     void CountNeighbors()
  104.     {
  105.         for(int y = 0; y < Screen_Height; ++y)
  106.         {
  107.             for (int x = 0; x < Screen_Width; x++)
  108.             {
  109.                 int numNeighbors = 0;
  110.  
  111.                 //North
  112.                 if(y+1 < Screen_Height)
  113.                 {
  114.                     if (grid[x, y + 1].isAlive)
  115.                     {
  116.                         numNeighbors++;
  117.                     }
  118.                 }
  119.                 //East
  120.                 if (x + 1 < Screen_Width)
  121.                 {
  122.                     if (grid[x + 1, y].isAlive)
  123.                     {
  124.                         numNeighbors++;
  125.                     }
  126.  
  127.                 }
  128.                 //South
  129.                 if (y - 1 >= 0)
  130.                 {
  131.                     if (grid[x, y - 1].isAlive)
  132.                     {
  133.                         numNeighbors++;
  134.                     }
  135.                 }
  136.                 //West
  137.                 if (x - 1 >= 0)
  138.                 {
  139.                     if (grid[x - 1, y].isAlive)
  140.                     {
  141.                         numNeighbors++;
  142.                     }
  143.                 }
  144.                 //North East
  145.                 if (x + 1 < Screen_Width && y + 1 < Screen_Height)
  146.                 {
  147.                     if (grid[x + 1, y + 1].isAlive)
  148.                     {
  149.                         numNeighbors++;
  150.                     }
  151.                 }
  152.                 //North West
  153.                 if(x-1 >= 0 && y+1 < Screen_Height)
  154.                 {
  155.                     if(grid[x-1,y+1].isAlive)
  156.                     {
  157.                         numNeighbors++;
  158.                     }    
  159.                 }
  160.                 //SouthEast
  161.                 if (x + 1 < Screen_Width && y - 1 >= 0)
  162.                 {
  163.                     if (grid[x + 1, y - 1].isAlive)
  164.                     {
  165.                         numNeighbors++;
  166.                     }
  167.                 }
  168.                 //SouthWest
  169.                 if (x - 1 >= 0 && y - 1 >= 0)
  170.                 {
  171.                     if (grid[x - 1, y - 1].isAlive)
  172.                     {
  173.                         numNeighbors++;
  174.                     }
  175.                 }
  176.                 grid[x, y].numNeighbors = numNeighbors;
  177.  
  178.             }
  179.         }
  180.     }
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement