Game of life - populating the cells not working properly
am trying to create the conway's gameo of life simulation. i have created the grid, calculated the neighbors but when i try to call the population control method, the neighbor calculation method doesnt work properly. The simulation comes to zero gradually. i couldnt able to figure out where am missing the logic. need help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameOfLife_GameManager : MonoBehaviour
{
#region Integers
[Header("Integers")]
[SerializeField]
private int NoOfColumns = 6; //Screen Height
[SerializeField]
private int NoOfRows = 6; //Screen Width
[SerializeField]
private int RandomCeilingHeight = 40;
#endregion
#region Floats
private float rectWidth, rectHeight, xDistance = 0f, yDistance = 0f;
[SerializeField]
private float speed = 0f, timer = 0f;
#endregion
#region Gameobjects
[Header("Gameobjects")]
[SerializeField]
private GameObject BlackSprite_gobj;
[SerializeField]
private GameObject WhiteSprite_gobj;
[SerializeField]
private GameObject GridParent_gobj;
#endregion
//public CellScript[,] Grid;
public GameObject[,] Grid;
void Awake()
{
InitValues();
}
// Start is called before the first frame update
void Start()
{
GridCreation();
CalculateNeighbours();
}
// Update is called once per frame
void Update()
{
if (timer >= speed)
{
timer = 0;
CalculateNeighbours();
PopulationControl();
}
else
{
timer += Time.deltaTime;
}
}
private void InitValues()
{
//Grid = new CellScript[NoOfRows, NoOfColumns];
Grid = new GameObject[NoOfRows, NoOfColumns];
if (NoOfColumns > Camera.main.orthographicSize * 2)
{
float val = NoOfColumns - Camera.main.orthographicSize * 2;
Camera.main.orthographicSize += val / 2;
}
rectWidth = rectHeight = Camera.main.orthographicSize;
//Assigning the offset
xDistance = rectWidth / NoOfColumns;
yDistance = rectHeight / NoOfRows;
}
private void GridCreation()
{
for (int x = 0; x < NoOfRows; x++)
{
for (int y = 0; y < NoOfColumns; y++)
{
Vector3 position = new Vector3(y * WhiteSprite_gobj.transform.localScale.x - (rectWidth - 0.5f) ,
x * WhiteSprite_gobj.transform.localScale.y - (rectHeight - 0.5f),
WhiteSprite_gobj.transform.position.z);
GameObject go = Instantiate(WhiteSprite_gobj, position, Quaternion.identity);
go.GetComponent<CellScript>().SetAliveOrNot(SetCellAliveOrDead());
Grid[x, y] = go;
go.transform.parent = GridParent_gobj.transform;
}
}
}
private void CalculateNeighbours()
{
for(int x = 0; x < NoOfRows; x++)
{
for(int y = 0; y < NoOfColumns; y++)
{
int NoOfNeighbours = 0;
//North
//if(x+1 < NoOfRows)
if (y+1 < NoOfColumns)
{
if(Grid[x, y+1].GetComponent<CellScript>().IsAlive)
{
NoOfNeighbours++;
//Debug.LogError( x.ToString() + ": " + y.ToString() + " -- " + x.ToString() + "," + (y + 1));
}
}
//South
//if(x -1 >= 0)
if(y -1 >= 0)
{
if(Grid[x,y-1].GetComponent<CellScript>().IsAlive)
{
NoOfNeighbours++;
//Debug.LogError(x.ToString() + ": " + y.ToString() + " -- " + x.ToString() + "," + (y - 1));
}
}
//East
//if(y+1 < NoOfColumns)
if(x+1 < NoOfRows)
{
if(Grid[x+1, y].GetComponent<CellScript>().IsAlive)
{
NoOfNeighbours++;
//Debug.LogError(x.ToString() + ": " + y.ToString() + " -- " + (x+1).ToString() + "," + y);
}
}
//West
//if(y-1 >= 0)
if(x-1 >= 0)
{
if(Grid[x-1,y].GetComponent<CellScript>().IsAlive)
{
NoOfNeighbours++;
//Debug.LogError(x.ToString() + ": " + y.ToString() + " -- " + (x-1).ToString() + "," + y.ToString());
}
}
//North East
//if(x + 1 < NoOfRows && y + 1 < NoOfColumns)
if(y + 1 < NoOfColumns && x + 1 < NoOfRows)
{
if(Grid[x+1, y+1].GetComponent<CellScript>().IsAlive)
{
NoOfNeighbours++;
//Debug.LogError(x.ToString() + ": " + y.ToString() + " -- " + (x+1).ToString() + "," + (y+1).ToString());
}
}
//NorthWest
//if (x + 1 < NoOfRows && y - 1 >= 0)
if (y + 1 < NoOfColumns && x - 1 >= 0)
{
if (Grid[x-1, y + 1].GetComponent<CellScript>().IsAlive)
{
NoOfNeighbours++;
//Debug.LogError(x.ToString() + ": " + y.ToString() + " -- " + (x-1).ToString() + "," + (y+1).ToString());
}
}
//SouthEast
if (y - 1 >= 0 && x + 1 < NoOfRows)
{
if (Grid[x + 1, y - 1].GetComponent<CellScript>().IsAlive)
{
NoOfNeighbours++;
//Debug.LogError(x.ToString() + ": " + y.ToString() + " -- " + (x+1).ToString() + "," + (y-1).ToString());
}
}
//SouthWest
if(y - 1 >= 0 && x - 1 >= 0)
{
if (Grid[x - 1, y - 1].GetComponent<CellScript>().IsAlive)
{
NoOfNeighbours++;
//Debug.LogError(x.ToString() + ": " + y.ToString() + " -- " + (x-1).ToString() + "," + (y-1).ToString());
}
}
Grid[x, y].GetComponent<CellScript>().Neighbours = NoOfNeighbours;
}
}
}
/// <summary>
/// Rules
/// 1 - Any live cell with two or three live neighbours survives.
/// 2 - Any dead cell with three live neighbours becomes a live cell.
/// 3 - All other live cells die in the next generation.Similarly, all other dead cells stay dead.
/// </summary>
private void PopulationControl()
{
for (int x = 0; x < NoOfRows; x++)
{
for (int y = 0; y < NoOfColumns; y++)
{
if (Grid[x, y].GetComponent<CellScript>().IsAlive)
{
//Any live cell with two or three live neighbours survives
if (Grid[x, y].GetComponent<CellScript>().Neighbours != 2 || Grid[x, y].GetComponent<CellScript>().Neighbours != 3)
{
Grid[x, y].GetComponent<CellScript>().SetAliveOrNot(false);
}
}
else
{
//Any dead cell with three live neighbours becomes a live cell
if (Grid[x, y].GetComponent<CellScript>().Neighbours == 3)
{
Grid[x, y].GetComponent<CellScript>().SetAliveOrNot(true);
}
}
}
}
}
private bool SetCellAliveOrDead()
{
int rand = UnityEngine.Random.Range(0, 10);
if(rand > RandomCeilingHeight)
{
return true;
}
return false;
}
}
Comment
Your answer

Follow this Question
Related Questions
How to swap any two gameobjects in a field area? 0 Answers
Simple `Match 3+ in a row` pattern 0 Answers
switch case statment 0 Answers
Problem with the logic in my code 0 Answers
Script function being applied to everything it's attached to logic error 2 Answers