-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
94 lines (81 loc) · 3.82 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Arroyo Calle, Adrián
Bazaco Velasco, Daniel*/
using System;
using System.Diagnostics;
using Microsoft.Extensions.CommandLineUtils;
using Avalonia;
using Conway.Matrix;
using Quadtree;
namespace Conway
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Conway - Game of Life");
var app = new CommandLineApplication();
app.Name = "Conway";
app.HelpOption("-?|-h|--help");
app.VersionOption("-v|--version","Conway 1.0.0");
var inputFile = app.Option("-i <inputfile>|--input <inputfile>","Input file",CommandOptionType.SingleValue);
var iterations = app.Option("-n <n>|--iter <n>","Number of iterations",CommandOptionType.SingleValue);
app.OnExecute(()=>{
// procesar entrada de comandos
try{
// algoritmo de Quadtree
if(!inputFile.HasValue())
throw new Exception("Please, specify an input file");
if(!System.IO.File.Exists(inputFile.Value()))
throw new Exception("Please, check that the specified file exists");
if(!iterations.HasValue())
throw new Exception("Please, specify a number of iterations");
var iter = Int32.Parse(iterations.Value());
// TODO: ser capaz de detectar el formato correcto
bool[][] matrix = null;
try{
matrix = new File.Rle(inputFile.Value()).GetMatrix();
}catch(Exception){
matrix = new File.Vaca(inputFile.Value()).GetMatrix();
};
if(matrix == null)
throw new Exception("File format error");
Cuadrante cuadrante = Cuadrante.crear(matrix);
Console.WriteLine($"{cuadrante}");
var sw = Stopwatch.StartNew();
for(var i=0;i<iter;i++){
while(!(cuadrante.isCentrado() && cuadrante.getCuadranteCentral().isCentrado())){
cuadrante = cuadrante.expandir();
}
cuadrante = cuadrante.generacionEtapa4();
}
sw.Stop();
var conway = new ConwayMatrix(cuadrante.GetMatrix());
var final = conway.GetFinalResult();
// report final
Console.WriteLine($"{iter} iteraciones");
Console.WriteLine($"{final.CeldasVivas} celdas vivas");
Console.WriteLine($"Dimensiones: {final.LimitedMatrix.Width} x {final.LimitedMatrix.Height}");
Console.WriteLine($"Tiempo: {sw.Elapsed.TotalMilliseconds} ms");
// guardar
Console.Write("Fichero de salida: ");
var filename = Console.ReadLine().Trim();
if(filename == string.Empty){
Console.WriteLine(final.LimitedMatrix);
}else{
var file = new File.Vaca();
file.ConwayMatrix = final.LimitedMatrix;
file.Save(filename);
Console.WriteLine($"Matriz final guardada en {filename}");
}
}catch(Exception e){
Console.WriteLine($"WARNING: {e.Message}");
Console.WriteLine("Starting UI");
// arrancar gui (algoritmo de ConwayMatrix)
AppBuilder.Configure<App>().UseSkia().UseX11().Start<GUI.MainWindow>();
}
return 0;
});
app.Execute(args);
}
}
}