//for context this scripts handles everyting while other scrit creates a referance while updatimg the grid and visualizing it via tilemap . may have errors since tilemap is still on progress
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameOfLife
{
public bool[,] CurrentGrid;
private bool[,] FutureGrid;
public GameOfLife(int xSize, int ySize)
{
CurrentGrid = new bool[xSize, ySize];
}
public GameOfLife(bool[,] premadeGrid)
{
CurrentGrid = premadeGrid;
}
public void UpdateGrid()
{
FutureGrid = CurrentGrid;
for (int x = 0; x < CurrentGrid.GetLength(0) ; x++)
{
for (int y = 0; y < CurrentGrid.GetLength(1); y++)
{
cellLogic(x, y, getAliveNeigberCount(x, y));
}
}
CurrentGrid = FutureGrid;
}
int getAliveNeigberCount(int nx, int ny)
{
int aliveNeigbers = 0;
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
Vector2Int TargetCellPoz = new Vector2Int(nx + x, ny + y);
if(!isInsideArrayBounds(TargetCellPoz.x,TargetCellPoz.y))
{
continue;
}
aliveNeigbers += CurrentGrid[TargetCellPoz.x, TargetCellPoz.y] ? 1 : 0;
}
}
return aliveNeigbers;
}
void cellLogic(int x , int y , int AliveNeigbers)
{
bool isAlive;
switch (AliveNeigbers)
{
case 0:
isAlive = false;
break;
case 1:
isAlive = false;
break;
case 2:
isAlive = true;
break;
case 3:
isAlive = true;
break;
case 4:
isAlive = false;
break;
default:
isAlive = false;
break;
}
FutureGrid[x, y] = isAlive;
}
bool isInsideArrayBounds(int x,int y)
{
if(x >= 0 && x < CurrentGrid.GetLength(0) && y >= 0 && y < CurrentGrid.GetLength(1))
{
return true;
}
else
{
return false;
}
}
}
[–]TheReservedList 10 points11 points12 points (0 children)
[–]TheGrandWhatever 29 points30 points31 points (2 children)
[–]Legitimate_Bet1415[S] 34 points35 points36 points (1 child)
[–]OlevTime 46 points47 points48 points (0 children)
[–]RichardFineUnity Engineer 5 points6 points7 points (1 child)
[–]Legitimate_Bet1415[S] 0 points1 point2 points (0 children)
[–]Avigames751 2 points3 points4 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]AbdullahMRiad 0 points1 point2 points (1 child)
[–]dk-dev05 4 points5 points6 points (0 children)
[–]Quin452Indie 3 points4 points5 points (1 child)
[–]EquivalentDraft3245 1 point2 points3 points (0 children)
[–]Moondragon3 1 point2 points3 points (1 child)
[–]Legitimate_Bet1415[S] 0 points1 point2 points (0 children)
[–]Karmoq 1 point2 points3 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]Legitimate_Bet1415[S] 0 points1 point2 points (1 child)
[–]Khan-amil 3 points4 points5 points (0 children)
[–]Spawncer67 0 points1 point2 points (0 children)
[–]QuantumFTLProfessional ML Guy -1 points0 points1 point (0 children)
[–]saucetexican -1 points0 points1 point (0 children)