using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GameOfLife.CellClasses
{
class CellGrid
{
public readonly int Height;
public readonly int Width;
Cell[,] cells;
public CellGrid(int height, int width)
{
this.Height = height;
this.Width = width;
cells = new Cell[Width, Height];
}
public Cell this[int x, int y]
{
get { return cells[x, y]; }
set { cells[x, y] = value; }
}
public void PassGeneration(RuleSet Rule)
{
RefreshFriends();
switch (Rule)
{
case RuleSet.Conway:
{
Conway();
break;
}
}
}
public void RefreshFriends()
{
for (int x = 0; x < this.Width; x++)
for (int y = 0; y < this.Width; y++)
{
cells[x, y].Neighbors = 0;
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
if (!(i == 0 && j == 0) && IsValidCoordinates(x + i, y + j) && cells[x + i, y + j].Alive)
cells[x, y].Neighbors++;
}
}
public bool IsValidCoordinates(int x, int y)
{
return (x >= 0 &&
x < Width &&
y >= 0 &&
y < Height);
}
#region Generation Functions
private void Conway()
{
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
if (cells[x, y].Alive)
{
cells[x, y].Alive = (cells[x, y].Neighbors > 1 && cells[x, y].Neighbors < 4);
continue;
}
cells[x, y].Alive = (cells[x, y].Neighbors == 3);
}
}
}
#endregion
public void GenerateNegetive()
{
for (int x = 0; x < this.Width; x++)
for (int y = 0; y < this.Height; y++)
cells[x, y].Alive = !cells[x, y].Alive;
}
}
}