Files
test/GameOfLife/Game.cs
2024-10-24 03:03:13 +02:00

138 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameOfLife
{
public class Game
{
public Game(int x, int y) {
ActiveMap = new(x, y);
SwapMap = new(x, y);
}
public int Iteration => _Iteration;
private GameMap ActiveMap, SwapMap;
private int _Iteration;
public void Update()
{
//SwapMap.Clear();
foreach ( var cell in ActiveMap.GetAllCells())
{
cell.Update(ref ActiveMap, ref SwapMap);
}
_Iteration++;
//Swap Maps
ActiveMap.CloneFrom(SwapMap);
}
public GameMap getActiveMap()
{
return ActiveMap;
}
public string ToString()
{
return $"""
Current Iteration: {Iteration}
""";
}
public string RenderToString()
{
int MapRowSize = ActiveMap.SizeY;
int row = 0;
int rowCounter = 0;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("\t0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5\n");
stringBuilder.AppendLine();
stringBuilder.Append("0\t");
foreach ( var cell in ActiveMap.GetAllCells())
{
stringBuilder.Append(cell.ToString() + " ");
rowCounter++;
if (rowCounter == MapRowSize)
{
rowCounter = 0;
row++;
stringBuilder.Append($"\n{row}\t");
}
}
return stringBuilder.ToString();
}
public string RenderToDebugString()
{
int row = 0;
int MapRowSize = (int)Math.Sqrt(ActiveMap.GetAllCells().Length);
int rowCounter = 0;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("\t0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5\n");
stringBuilder.AppendLine();
stringBuilder.Append("0\t");
foreach (var cell in ActiveMap.GetAllCells())
{
stringBuilder.Append(cell.GetNeighborVal(ref ActiveMap) + " ");
rowCounter++;
if (rowCounter == MapRowSize)
{
rowCounter = 0;
row++;
stringBuilder.Append($"\n{row}\t");
}
}
return stringBuilder.ToString();
}
public string RenderToSwapDebugString()
{
int MapRowSize = (int)Math.Sqrt(SwapMap.GetAllCells().Length);
int rowCounter = 0;
StringBuilder stringBuilder = new StringBuilder();
foreach (var cell in SwapMap.GetAllCells())
{
stringBuilder.Append(cell.GetNeighborVal(ref SwapMap));
rowCounter++;
if (rowCounter == MapRowSize)
{
rowCounter = 0;
stringBuilder.Append("\n");
}
}
return stringBuilder.ToString();
}
public void SetRandomStart(int XSize, int YSize, int Seed = 0,int mod = 2)
{
//Not using Size args right now
for (int i = 0; i < ActiveMap.SizeX - 1; i++)
{
for (int j = 0; j < ActiveMap.SizeY - 1; j++)
{
ActiveMap.GetCell(i,j).isAlive = (Random.Shared.Next()%mod == 0);
}
}
}
public void GeneraterSwitcher()
{
ActiveMap.GetCell(ActiveMap.SizeX/2, ActiveMap.SizeY/2).isAlive = true;
ActiveMap.GetCell(ActiveMap.SizeX / 2, (ActiveMap.SizeY / 2)+1).isAlive = true;
ActiveMap.GetCell(ActiveMap.SizeX / 2, (ActiveMap.SizeY / 2)-1).isAlive = true;
}
}
}