192 lines
5.0 KiB
C#
192 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GameOfLife
|
|
{
|
|
public class GameMap
|
|
{
|
|
private int _SizeX, _SizeY;
|
|
|
|
CellState[,] Cells = new CellState[1,1];
|
|
|
|
public int SizeX { get => _SizeX; set => _SizeX = value; }
|
|
public int SizeY { get => _SizeY; set => _SizeY = value; }
|
|
|
|
|
|
public CellState[,] GetAllCells() => Cells;
|
|
public GameMap(int xSize, int ySize)
|
|
{
|
|
_SizeX = xSize;
|
|
_SizeY = ySize;
|
|
InitializeCells();
|
|
}
|
|
|
|
public ref CellState GetCell(int x, int y)
|
|
{
|
|
|
|
|
|
//if (x > SizeX) { x = SizeX; }
|
|
//if (x < 0) { x = 0; }
|
|
//if (y > SizeY) { y = SizeY; }
|
|
//if (y < 0) { y = 0; }
|
|
|
|
//Trying a wrap around
|
|
if(x >= SizeX) { x = 0; }
|
|
if(y >= SizeY) { y = 0; }
|
|
if(x < 0) { x = SizeX-1; }
|
|
if(y < 0) { y = SizeY-1; }
|
|
|
|
|
|
return ref Cells[x, y];
|
|
}
|
|
|
|
private void InitializeCells()
|
|
{
|
|
Cells = new CellState[SizeX,SizeY];
|
|
for (int i = 0; i < Cells.GetLength(0) -1; i++)
|
|
{
|
|
for (int j = 0; j < Cells.GetLength(1) -1; j++)
|
|
{
|
|
Cells[i, j].isAlive = false;
|
|
Cells[i, j].XCoord = i;
|
|
Cells[i, j].YCoord = j;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void CloneFrom(GameMap source)
|
|
{
|
|
for(int i = 0;i < Cells.GetLength(0) - 1;i++)
|
|
{
|
|
for(int j = 0; j < Cells.GetLength(1) -1; j++)
|
|
{
|
|
this.Cells[i,j] = source.Cells[i,j];
|
|
}
|
|
}
|
|
}
|
|
|
|
public int GetAliveCells()
|
|
{
|
|
int alive = 0;
|
|
foreach (var cell in Cells)
|
|
{ if (cell.isAlive) alive++; }
|
|
return alive;
|
|
}
|
|
|
|
internal void Clear()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
public struct CellState
|
|
{
|
|
public bool isAlive;
|
|
public int XCoord, YCoord;
|
|
|
|
|
|
|
|
//Debug Function
|
|
public int GetNeighborVal(ref GameMap map)
|
|
{
|
|
return GetAliveNeighbors(ref map);
|
|
}
|
|
|
|
|
|
public void Update(ref GameMap CurrentMap, ref GameMap SwapMap)
|
|
{
|
|
int AliveNeighborCounter = GetAliveNeighbors(ref CurrentMap);
|
|
|
|
//Generate new Cell
|
|
if (AliveNeighborCounter == 3)
|
|
{
|
|
SwapMap.GetCell(XCoord, YCoord).isAlive = true;
|
|
return;
|
|
}
|
|
//State unchanged
|
|
if (AliveNeighborCounter == 2)
|
|
{
|
|
SwapMap.GetCell(XCoord, YCoord).isAlive = this.isAlive;
|
|
return;
|
|
}
|
|
//Cell Dies
|
|
else
|
|
{
|
|
SwapMap.GetCell(XCoord, YCoord).isAlive = false;
|
|
}
|
|
|
|
}
|
|
|
|
private int GetAliveNeighbors(ref GameMap CurrentMap)
|
|
{
|
|
int AliveNeighborCounter = 0;
|
|
|
|
// 0 0 0
|
|
// 0 C x
|
|
// 0 0 0
|
|
if (CurrentMap.GetCell(this.XCoord + 1, this.YCoord).isAlive)
|
|
{ AliveNeighborCounter++; }
|
|
// 0 0 0
|
|
// 0 C 0
|
|
// 0 0 x
|
|
if (CurrentMap.GetCell(this.XCoord + 1, this.YCoord - 1).isAlive)
|
|
{ AliveNeighborCounter++; }
|
|
// 0 0 0
|
|
// 0 C 0
|
|
// 0 x 0
|
|
if (CurrentMap.GetCell(this.XCoord, this.YCoord - 1).isAlive)
|
|
{ AliveNeighborCounter++; }
|
|
// 0 0 0
|
|
// 0 C 0
|
|
// x 0 0
|
|
if (CurrentMap.GetCell(this.XCoord - 1, this.YCoord - 1).isAlive)
|
|
{ AliveNeighborCounter++; }
|
|
|
|
if (AliveNeighborCounter > 3) return AliveNeighborCounter;
|
|
// 0 0 0
|
|
// x C 0
|
|
// 0 0 0
|
|
if (CurrentMap.GetCell(this.XCoord - 1, this.YCoord).isAlive)
|
|
{ AliveNeighborCounter++; }
|
|
|
|
if (AliveNeighborCounter > 3) return AliveNeighborCounter;
|
|
// x 0 0
|
|
// 0 C 0
|
|
// 0 0 0
|
|
if (CurrentMap.GetCell(this.XCoord - 1, this.YCoord + 1).isAlive)
|
|
{ AliveNeighborCounter++; }
|
|
|
|
if (AliveNeighborCounter > 3) return AliveNeighborCounter;
|
|
// 0 x 0
|
|
// 0 C 0
|
|
// 0 0 0
|
|
if (CurrentMap.GetCell(this.XCoord, this.YCoord + 1).isAlive)
|
|
{ AliveNeighborCounter++; }
|
|
|
|
if (AliveNeighborCounter > 3) return AliveNeighborCounter;
|
|
// 0 0 x
|
|
// 0 C 0
|
|
// 0 0 0
|
|
if (CurrentMap.GetCell(this.XCoord + 1, this.YCoord + 1).isAlive)
|
|
{ AliveNeighborCounter++; }
|
|
|
|
return AliveNeighborCounter;
|
|
}
|
|
|
|
public string ToString()
|
|
{
|
|
return isAlive ? "1" : "0";
|
|
}
|
|
|
|
|
|
|
|
internal void SetAlive()
|
|
{
|
|
this.isAlive = true;
|
|
}
|
|
}
|
|
}
|