116 lines
4.5 KiB
C#
116 lines
4.5 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using GameOfLife.Entities;
|
|
using GameOfLife.IO;
|
|
using Timer = System.Timers.Timer;
|
|
|
|
namespace GameOfLife
|
|
{
|
|
static class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
//var rleParser = new RleDecoder(@"PatternProjections/honey-farm-hassler-147.rle");
|
|
//var rleParser = new RleDecoder(@"PatternProjections/glider-stream-crystal.rle");
|
|
//var rleParser = new RleDecoder(@"PatternProjections/glider.rle");
|
|
var rleParser = new RleDecoder(@"PatternProjections/blom.rle");
|
|
|
|
var pattern = rleParser.Pattern;
|
|
|
|
var paddingX = 3;
|
|
var paddingY = 3;
|
|
|
|
var life = new LifeHashSet(rleParser.Pattern);
|
|
var patternLibrary = new PatternLibrary();
|
|
|
|
var timer = new Stopwatch();
|
|
timer.Start();
|
|
do
|
|
{
|
|
life.IncrementGeneration();
|
|
|
|
Console.WriteLine($"{life.Generation} + {life.Population}");
|
|
foreach (var p in patternLibrary.MatchLibraryPatterns(life.LivingCells))
|
|
{
|
|
Console.WriteLine($"\t\t\tFound {p.Item1.Name} {p.Item2.Length}");
|
|
}
|
|
|
|
|
|
//Console.Clear();
|
|
//OutputBoard(sim);
|
|
//Console.ReadKey();
|
|
|
|
//Thread.Sleep(100);
|
|
} while (life.Population > 0 && life.Generation < 3000);
|
|
timer.Stop();
|
|
Console.WriteLine($"Final: Generation:{life.Generation}, Pop:{life.Population}, Time:{timer.ElapsedMilliseconds}");
|
|
Console.ReadKey();
|
|
}
|
|
|
|
private static void OutputBoard(LifeBase sim)
|
|
{
|
|
var neighborField = Neighborhood.GetNeighborField(sim.LivingCells);
|
|
|
|
for (short x = 0; x < 40 /*sim.SizeX*/; x++)
|
|
{
|
|
for (short y = 0; y < 120 /*sim.SizeY*/; y++)
|
|
{
|
|
var cell = new Cell(x, y);
|
|
|
|
var neighbors = neighborField.FirstOrDefault(n => n.Item1.Equals(cell));
|
|
|
|
Console.BackgroundColor = ConsoleColor.Black;
|
|
Console.Write(" ");
|
|
var isCellAlive = sim.IsCellAlive(cell);
|
|
|
|
if (neighbors != null)
|
|
switch (neighbors.Item2)
|
|
{
|
|
case 0:
|
|
Console.BackgroundColor = ConsoleColor.Black;
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
break;
|
|
case 1 when isCellAlive:
|
|
Console.BackgroundColor = ConsoleColor.DarkMagenta;
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
break;
|
|
case 1:
|
|
Console.BackgroundColor = ConsoleColor.DarkGreen;
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
break;
|
|
|
|
case 2 when isCellAlive:
|
|
Console.BackgroundColor = ConsoleColor.DarkGray;
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
break;
|
|
case 2:
|
|
Console.BackgroundColor = ConsoleColor.DarkGreen;
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
break;
|
|
|
|
|
|
case 3 when isCellAlive:
|
|
Console.BackgroundColor = ConsoleColor.DarkGray;
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
break;
|
|
case 3:
|
|
Console.BackgroundColor = ConsoleColor.Green;
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
break;
|
|
|
|
default:
|
|
Console.BackgroundColor = ConsoleColor.DarkRed;
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
break;
|
|
}
|
|
|
|
Console.Write(isCellAlive ? "O" : ".");
|
|
}
|
|
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
}
|
|
}
|