mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Eliminated StackOverflow bug.
This commit is contained in:
@@ -47,10 +47,6 @@ void PrintHeader()
|
|||||||
.First();
|
.First();
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
PrintHeader();
|
PrintHeader();
|
||||||
|
|
||||||
@@ -106,25 +102,13 @@ Simulation InitializeSimulation(string[] inputPattern, MatrixSpace matrixToIniti
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// PrintMatrix(matrixSpace.Matrix);
|
|
||||||
void PrintMatrix(int[,] matrix)
|
|
||||||
{
|
|
||||||
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()
|
void ProcessGeneration()
|
||||||
{
|
{
|
||||||
var matrix = matrixSpace.Matrix; // TODO refactor
|
var matrix = matrixSpace.Matrix; // TODO refactor
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
|
||||||
// generation++;
|
// generation++;
|
||||||
|
|
||||||
void PrintPopulation(int generation, int population)
|
void PrintPopulation(int generation, int population)
|
||||||
@@ -133,6 +117,7 @@ void PrintMatrix(int[,] matrix)
|
|||||||
if (isInvalid)
|
if (isInvalid)
|
||||||
Console.WriteLine("INVALID!");
|
Console.WriteLine("INVALID!");
|
||||||
}
|
}
|
||||||
|
|
||||||
PrintPopulation(simulation.Generation, simulation.Population);
|
PrintPopulation(simulation.Generation, simulation.Population);
|
||||||
|
|
||||||
simulation.StartNewGeneration();
|
simulation.StartNewGeneration();
|
||||||
@@ -167,6 +152,7 @@ void PrintMatrix(int[,] matrix)
|
|||||||
matrix[x, y] = 0;
|
matrix[x, y] = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (matrix[x, y] == 3)
|
if (matrix[x, y] == 3)
|
||||||
{
|
{
|
||||||
matrix[x, y] = 1;
|
matrix[x, y] = 1;
|
||||||
@@ -183,6 +169,7 @@ void PrintMatrix(int[,] matrix)
|
|||||||
nextMinY = Math.Min(y, nextMinY);
|
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));
|
Console.WriteLine(string.Join(separator: null, values: printedLine));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,6 +178,7 @@ void PrintMatrix(int[,] matrix)
|
|||||||
{
|
{
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
|
|
||||||
minX = nextMinX;
|
minX = nextMinX;
|
||||||
@@ -204,16 +192,19 @@ void PrintMatrix(int[,] matrix)
|
|||||||
minX = 3;
|
minX = 3;
|
||||||
isInvalid = true;
|
isInvalid = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (maxX > 22)
|
if (maxX > 22)
|
||||||
{
|
{
|
||||||
maxX = 22;
|
maxX = 22;
|
||||||
isInvalid = true;
|
isInvalid = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (minY < 3)
|
if (minY < 3)
|
||||||
{
|
{
|
||||||
minY = 3;
|
minY = 3;
|
||||||
isInvalid = true;
|
isInvalid = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (maxY > 68)
|
if (maxY > 68)
|
||||||
{
|
{
|
||||||
maxY = 68;
|
maxY = 68;
|
||||||
@@ -239,6 +230,7 @@ void PrintMatrix(int[,] matrix)
|
|||||||
neighbors++;
|
neighbors++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrintMatrix(matrix);
|
// PrintMatrix(matrix);
|
||||||
if (matrix[x, y] == 0)
|
if (matrix[x, y] == 0)
|
||||||
{
|
{
|
||||||
@@ -268,20 +260,14 @@ void PrintMatrix(int[,] matrix)
|
|||||||
maxY++;
|
maxY++;
|
||||||
}
|
}
|
||||||
// PrintMatrix(matrix);
|
// PrintMatrix(matrix);
|
||||||
ProcessGeneration();
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
ProcessGeneration();
|
ProcessGeneration();
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Console.WriteLine(e);
|
|
||||||
}
|
|
||||||
public class Simulation
|
public class Simulation
|
||||||
{
|
{
|
||||||
public int Generation { get; private set; }
|
public int Generation { get; private set; }
|
||||||
|
|||||||
Reference in New Issue
Block a user