Eliminated StackOverflow bug.

This commit is contained in:
Dyego Maas
2022-01-11 20:43:14 -03:00
parent be5e35f7e0
commit 44b1ada7d4

View File

@@ -47,10 +47,6 @@ void PrintHeader()
.First();
}
try
{
PrintHeader();
@@ -106,25 +102,13 @@ Simulation InitializeSimulation(string[] inputPattern, MatrixSpace matrixToIniti
}
// PrintMatrix(matrixSpace.Matrix);
void PrintMatrix(int[,] matrix)
void ProcessGeneration()
{
Console.WriteLine("Matrix:");
for (int x = 0; x < matrix.GetLength(0); x++)
{
for (int y = 0; y < matrix.GetLength(1); y++)
{
var character = matrix[x, y] == 0 ? ' ' : '*';
Console.Write(character);
}
Console.WriteLine();
}
}
void ProcessGeneration()
{
var matrix = matrixSpace.Matrix; // TODO refactor
while (true)
{
// generation++;
void PrintPopulation(int generation, int population)
@@ -133,6 +117,7 @@ void PrintMatrix(int[,] matrix)
if (isInvalid)
Console.WriteLine("INVALID!");
}
PrintPopulation(simulation.Generation, simulation.Population);
simulation.StartNewGeneration();
@@ -167,6 +152,7 @@ void PrintMatrix(int[,] matrix)
matrix[x, y] = 0;
continue;
}
if (matrix[x, y] == 3)
{
matrix[x, y] = 1;
@@ -179,10 +165,11 @@ void PrintMatrix(int[,] matrix)
printedLine[y] = '*';
nextMinX = Math.Min(x, nextMinX);
nextMaxX = Math.Max(x+1, nextMaxX);
nextMaxX = Math.Max(x + 1, nextMaxX);
nextMinY = Math.Min(y, nextMinY);
nextMaxY = Math.Max(y+1, nextMaxY);
nextMaxY = Math.Max(y + 1, nextMaxY);
}
Console.WriteLine(string.Join(separator: null, values: printedLine));
}
@@ -191,6 +178,7 @@ void PrintMatrix(int[,] matrix)
{
Console.WriteLine();
}
Console.WriteLine();
minX = nextMinX;
@@ -204,16 +192,19 @@ void PrintMatrix(int[,] matrix)
minX = 3;
isInvalid = true;
}
if (maxX > 22)
{
maxX = 22;
isInvalid = true;
}
if (minY < 3)
{
minY = 3;
isInvalid = true;
}
if (maxY > 68)
{
maxY = 68;
@@ -239,6 +230,7 @@ void PrintMatrix(int[,] matrix)
neighbors++;
}
}
// PrintMatrix(matrix);
if (matrix[x, y] == 0)
{
@@ -268,20 +260,14 @@ void PrintMatrix(int[,] matrix)
maxY++;
}
// PrintMatrix(matrix);
ProcessGeneration();
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
ProcessGeneration();
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
ProcessGeneration();
public class Simulation
{
public int Generation { get; private set; }