mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Got it working.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
using System.Xml;
|
||||
using System.Text;
|
||||
|
||||
IEnumerable<string> ReadPattern(int limitHeight)
|
||||
{
|
||||
@@ -47,6 +47,10 @@ void PrintHeader()
|
||||
.First();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
|
||||
PrintHeader();
|
||||
|
||||
@@ -54,11 +58,9 @@ Console.WriteLine("ENTER YOUR PATTERN:");
|
||||
// var pattern = ReadPattern(limitHeight: MaxHeight).ToArray();
|
||||
var pattern = new[]
|
||||
{
|
||||
" ",
|
||||
" * ",
|
||||
" * ",
|
||||
" * ",
|
||||
" "
|
||||
"*",
|
||||
"*",
|
||||
"*"
|
||||
}; // FOR DEBUGGING PURPOSES
|
||||
var (index, value) = GetLongestInput(pattern);
|
||||
Console.WriteLine("" + index + ", " + value);
|
||||
@@ -69,26 +71,42 @@ Console.WriteLine("" + index + ", " + value);
|
||||
const int MaxWidth = 70; // Y2
|
||||
const int MaxHeight = 24; // X2
|
||||
|
||||
var matrix = new int[24, 70]; // TODO understand it
|
||||
int population = 0;
|
||||
// var matrix = new int[24, 70]; // TODO understand it
|
||||
var matrixSpace = new MatrixSpace(height: 24, width: 70);
|
||||
// int population = 0;
|
||||
var isInvalid = false; // TODO understand
|
||||
|
||||
|
||||
int x1 = (11 - index / 2) - 1; // middle x
|
||||
int y1 = (33 - value.Length / 2) - 1; // middle y
|
||||
for (var x = 0; x < pattern.Length; x++)
|
||||
{
|
||||
for (var y = 0; y < pattern[x].Length; y++)
|
||||
|
||||
int minX = (11 - index / 2) - 1; // middle x
|
||||
int minY = (33 - value.Length / 2) - 1; // middle y
|
||||
int maxX = MaxHeight;
|
||||
int maxY = MaxWidth;
|
||||
var simulation = InitializeSimulation(pattern, matrixSpace);
|
||||
|
||||
|
||||
|
||||
Simulation InitializeSimulation(string[] inputPattern, MatrixSpace matrixToInitialize) {
|
||||
var newSimulation = new Simulation();
|
||||
|
||||
for (var x = 0; x < inputPattern.Length; x++)
|
||||
{
|
||||
if (pattern[x][y] != ' ')
|
||||
for (var y = 0; y < inputPattern[x].Length; y++)
|
||||
{
|
||||
matrix[x1 + x, y1 + y] = 1; // copy the pattern to the middle of the simulation
|
||||
population++; // increments the population
|
||||
if (inputPattern[x][y] != ' ')
|
||||
{
|
||||
matrixToInitialize.Matrix[minX + x, minY + y] = 1; // copy the pattern to the middle of the simulation
|
||||
// population++; // increments the population
|
||||
newSimulation.IncreasePopulation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newSimulation;
|
||||
}
|
||||
|
||||
PrintMatrix(matrix);
|
||||
|
||||
// PrintMatrix(matrixSpace.Matrix);
|
||||
void PrintMatrix(int[,] matrix)
|
||||
{
|
||||
Console.WriteLine("Matrix:");
|
||||
@@ -96,149 +114,191 @@ void PrintMatrix(int[,] matrix)
|
||||
{
|
||||
for (int y = 0; y < matrix.GetLength(1); y++)
|
||||
{
|
||||
Console.Write(matrix[x, y].ToString());
|
||||
var character = matrix[x, y] == 0 ? ' ' : '*';
|
||||
Console.Write(character);
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int generation = 0;
|
||||
|
||||
void ProcessGeneration()
|
||||
{
|
||||
generation++;
|
||||
void PrintPopulation(int generation, int population)
|
||||
void ProcessGeneration()
|
||||
{
|
||||
Console.WriteLine($"GENERATION: {generation}\tPOPULATION: {population}");
|
||||
if (isInvalid)
|
||||
Console.WriteLine("INVALID!");
|
||||
}
|
||||
PrintPopulation(generation, population);
|
||||
|
||||
int nextMaxX = MaxHeight, nextMaxY = MaxWidth;
|
||||
int nextMinX = 1, nextMinY = 1;
|
||||
|
||||
for (int x = 0; x < x1; x++)
|
||||
{
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
for (var x = x1; x < MaxHeight; x++)
|
||||
{
|
||||
Console.WriteLine();
|
||||
for (var y = y1; y < MaxWidth; y++)
|
||||
var matrix = matrixSpace.Matrix; // TODO refactor
|
||||
|
||||
// generation++;
|
||||
|
||||
void PrintPopulation(int generation, int population)
|
||||
{
|
||||
if (matrix[x, y] == 2)
|
||||
{
|
||||
matrix[x, y] = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (matrix[x, y] == 3)
|
||||
{
|
||||
matrix[x, y] = 1; // birth?
|
||||
Console.WriteLine(new string(' ', y+1) + "*");
|
||||
continue;
|
||||
}
|
||||
|
||||
nextMinX = Math.Min(x, nextMinX);
|
||||
nextMaxX = Math.Max(x, nextMaxX);
|
||||
nextMinY = Math.Min(y, nextMinY);
|
||||
nextMaxY = Math.Max(y, nextMaxY);
|
||||
Console.WriteLine($"GENERATION: {generation}\tPOPULATION: {population}");
|
||||
if (isInvalid)
|
||||
Console.WriteLine("INVALID!");
|
||||
}
|
||||
}
|
||||
PrintPopulation(simulation.Generation, simulation.Population);
|
||||
|
||||
var x2 = MaxHeight;
|
||||
for (int x = x2 + 1; x < MaxHeight; x++) // TODO test +1
|
||||
{
|
||||
Console.WriteLine();
|
||||
}
|
||||
simulation.StartNewGeneration();
|
||||
|
||||
x1 = nextMaxX;
|
||||
x2 = nextMinX;
|
||||
y1 = nextMaxY;
|
||||
var y2 = nextMinY;
|
||||
|
||||
// TODO boundaries? review
|
||||
if (x1 < 3)
|
||||
{
|
||||
x1 = 3;
|
||||
isInvalid = true;
|
||||
}
|
||||
// LINE 215
|
||||
// x3 = 24 = MaxHeight
|
||||
// y3 = 70 = MaxWidth
|
||||
// x4 = 1
|
||||
// y4 = 1
|
||||
// g = g + 1
|
||||
|
||||
if (x2 > 22)
|
||||
{
|
||||
x2 = 22;
|
||||
isInvalid = true;
|
||||
}
|
||||
|
||||
if (y1 < 3)
|
||||
{
|
||||
y1 = 3;
|
||||
isInvalid = true;
|
||||
}
|
||||
|
||||
if (y2 > 68)
|
||||
{
|
||||
y2 = 68;
|
||||
isInvalid = true;
|
||||
}
|
||||
|
||||
ProcessPopulation();
|
||||
// TODO line 635
|
||||
|
||||
void ProcessPopulation()
|
||||
{
|
||||
var population = 0;
|
||||
for (int x = x1 - 1; x < x2 + 1; x++) // TODO review indices
|
||||
int nextMinX = MaxHeight - 1; // x4
|
||||
int nextMinY = MaxWidth - 1; // y4
|
||||
int nextMaxX = 0; // x3
|
||||
int nextMaxY = 0; // y3
|
||||
|
||||
|
||||
// prints lines before
|
||||
for (int x = 0; x < minX; x++)
|
||||
{
|
||||
for (int y = y1 - 1; y < y2 + 1; y++) // TODO review indices
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
// prints matrix and
|
||||
for (var x = minX; x < maxX; x++)
|
||||
{
|
||||
var printedLine = Enumerable.Repeat(' ', MaxWidth).ToList();
|
||||
for (var y = minY; y < maxY; y++)
|
||||
{
|
||||
var counter = 0;
|
||||
for (int i = x - 1; i < x + 1; i++)
|
||||
if (matrix[x, y] == 2)
|
||||
{
|
||||
for (int j = y - 1; j < y + 1; j++)
|
||||
{
|
||||
if (matrix[i, j] == 1 || matrix[i, j] == 2)
|
||||
counter++;
|
||||
}
|
||||
matrix[x, y] = 0;
|
||||
continue;
|
||||
}
|
||||
if (matrix[x, y] == 3)
|
||||
{
|
||||
matrix[x, y] = 1;
|
||||
}
|
||||
else if (matrix[x, y] != 1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (matrix[x, y] == 0)
|
||||
printedLine[y] = '*';
|
||||
|
||||
nextMinX = Math.Min(x, nextMinX);
|
||||
nextMaxX = Math.Max(x+1, nextMaxX);
|
||||
nextMinY = Math.Min(y, nextMinY);
|
||||
nextMaxY = Math.Max(y+1, nextMaxY);
|
||||
}
|
||||
Console.WriteLine(string.Join(separator: null, values: printedLine));
|
||||
}
|
||||
|
||||
// prints lines after
|
||||
for (int x = maxX + 1; x < MaxHeight; x++) // TODO test +1
|
||||
{
|
||||
Console.WriteLine();
|
||||
}
|
||||
Console.WriteLine();
|
||||
|
||||
minX = nextMinX;
|
||||
maxX = nextMaxX;
|
||||
minY = nextMinY;
|
||||
maxY = nextMaxY;
|
||||
|
||||
// TODO boundaries? review
|
||||
if (minX < 3)
|
||||
{
|
||||
minX = 3;
|
||||
isInvalid = true;
|
||||
}
|
||||
if (maxX > 22)
|
||||
{
|
||||
maxX = 22;
|
||||
isInvalid = true;
|
||||
}
|
||||
if (minY < 3)
|
||||
{
|
||||
minY = 3;
|
||||
isInvalid = true;
|
||||
}
|
||||
if (maxY > 68)
|
||||
{
|
||||
maxY = 68;
|
||||
isInvalid = true;
|
||||
}
|
||||
|
||||
// LINE 309
|
||||
ProcessPopulation();
|
||||
|
||||
void ProcessPopulation()
|
||||
{
|
||||
// var population = 0;
|
||||
for (int x = minX - 1; x < maxX + 2; x++) // TODO review indices
|
||||
{
|
||||
for (int y = minY - 1; y < maxY + 2; y++) // TODO review indices
|
||||
{
|
||||
if (counter == 3)
|
||||
var neighbors = 0;
|
||||
for (int i = x - 1; i < x + 2; i++) // TODO review indices
|
||||
{
|
||||
for (int j = y - 1; j < y + 2; j++) // TODO review indices
|
||||
{
|
||||
if (matrix[i, j] == 1 || matrix[i, j] == 2)
|
||||
neighbors++;
|
||||
}
|
||||
}
|
||||
// PrintMatrix(matrix);
|
||||
if (matrix[x, y] == 0)
|
||||
{
|
||||
if (neighbors == 3)
|
||||
{
|
||||
matrix[x, y] = 3;
|
||||
// population++;
|
||||
simulation.IncreasePopulation();
|
||||
}
|
||||
}
|
||||
else if (neighbors is < 3 or > 4)
|
||||
{
|
||||
matrix[x, y] = 2;
|
||||
population++;
|
||||
}
|
||||
else
|
||||
{
|
||||
// population++;
|
||||
simulation.IncreasePopulation();
|
||||
}
|
||||
}
|
||||
else if (counter is < 3 or > 4)
|
||||
{
|
||||
matrix[x, y] = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
population++;
|
||||
}
|
||||
}
|
||||
|
||||
// LINE 635
|
||||
minX--;
|
||||
minY--;
|
||||
maxX++;
|
||||
maxY++;
|
||||
}
|
||||
// PrintMatrix(matrix);
|
||||
ProcessGeneration();
|
||||
}
|
||||
PrintMatrix(matrix);
|
||||
|
||||
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
ProcessGeneration();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
public class Simulation
|
||||
{
|
||||
public int Generation { get; private set; }
|
||||
|
||||
public int Population { get; private set; }
|
||||
|
||||
public void StartNewGeneration()
|
||||
{
|
||||
Generation++;
|
||||
Population = 0;
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
ProcessGeneration();
|
||||
|
||||
|
||||
|
||||
|
||||
public void IncreasePopulation()
|
||||
{
|
||||
Population++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// int x1 = 1, y1 = 1;
|
||||
@@ -250,3 +310,28 @@ ProcessGeneration();
|
||||
|
||||
|
||||
|
||||
public class MatrixSpace
|
||||
{
|
||||
public int[,] Matrix { get; }
|
||||
|
||||
public MatrixSpace(int height, int width)
|
||||
{
|
||||
Matrix = new int[height, width];
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var stringBuilder = new StringBuilder();
|
||||
for (var x = 0; x < Matrix.GetLength(0); x++)
|
||||
{
|
||||
for (var y = 0; y < Matrix.GetLength(1); y++)
|
||||
{
|
||||
var character = Matrix[x, y] == 0 ? " ": Matrix[x, y].ToString();
|
||||
stringBuilder.Append(character);
|
||||
}
|
||||
|
||||
stringBuilder.AppendLine();
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user