55using ExplorandoMarte . Models ;
66using System ;
77using System . Collections . Generic ;
8+ using Microsoft . Extensions . DependencyInjection ;
9+ using Microsoft . Extensions . Hosting ;
10+ using ExplorandoMarte . View ;
11+ using ExplorandoMarte . Configuration ;
812
913namespace ExplorandoMarte
1014{
1115 public class Program
1216 {
1317 public static void Main ( string [ ] args )
1418 {
15- try
16- {
17- Controller . Instance . InicializarLog ( ) ;
18-
19- Controller . Instance . RegistrarLog ( "Iniciando a exploração de Marte..." ) ;
20-
21- Controller . Instance . RegistrarLog ( "Informe as Coordenadas do Planalto: " ) ;
22-
23- int upperRightX , upperRightY = 0 ;
24-
25- var plateauCoordinates = Console . ReadLine ( ) . Split ( ' ' ) ;
26-
27- try
28- {
29- upperRightX = int . Parse ( plateauCoordinates [ 0 ] ) ;
30- upperRightY = int . Parse ( plateauCoordinates [ 1 ] ) ;
31- }
32- catch ( Exception )
33- {
34- throw new Exception ( "Coordenadas Inválidas" ) ;
35- }
36-
37- var planalto = new Planalto ( upperRightX , upperRightY ) ;
38-
39- Controller . Instance . RegistrarLog ( "Coordenadas do Planalto: " + planalto . ObterCoordenadasSuperiores ( ) ) ;
40-
41- List < Rover > rovers = new List < Rover > ( ) ;
42-
43- while ( rovers . Count < 2 )
44- {
45- Controller . Instance . RegistrarLog ( "Informe a posição inicial do Rover: " ) ;
46-
47- Console . WriteLine ( "------------------------------------------------------" ) ;
48- Console . WriteLine ( "Exemplo de posição inicial válida: " ) ;
49- Console . WriteLine ( "1 2 N" ) ;
50- Console . WriteLine ( "X = 1, Y = 2, Direção = Norte" ) ;
51- Console . WriteLine ( "Direções válidas: N, S, E, W" ) ;
52-
53- var initialPosition = Console . ReadLine ( ) ;
54-
55- if ( string . IsNullOrEmpty ( initialPosition ) || string . IsNullOrWhiteSpace ( initialPosition ) )
56- {
57- throw new ArgumentException ( "Posição Inválida para o Rover." , initialPosition ) ;
58- }
59-
60- var positionParts = initialPosition . Split ( ' ' ) ;
61-
19+ //IHost _host = Host.CreateDefaultBuilder(args)
20+ // .ConfigureServices((hostContext, services) =>
21+ // {
22+ // services.ResolveDependencies();
23+ // })
24+ // .Build();
6225
63- int x = int . Parse ( positionParts [ 0 ] ) ;
64- int y = int . Parse ( positionParts [ 1 ] ) ;
26+ //var app = _host.Services.GetService<IApplication>();
6527
66- if ( planalto . PosicaoOcupada ( x : x , y : y ) )
67- {
68- throw new InvalidOperationException ( string . Format ( "Posição já ocupada por outra sonda Rover {0}." , planalto . ObterRoverPorLocalizacao ( x , y ) . Nome ) ) ;
69- }
28+ //app.Run();
7029
71- char direction = char . Parse ( positionParts [ 2 ] . ToUpperInvariant ( ) ) ;
30+ var builder = Host . CreateDefaultBuilder ( args ) ;
7231
73- var rover = new Rover ( x , y , direction ) ;
74-
75- rover . SetPlanalto ( planalto ) ;
76-
77- planalto . AdicionarRover ( rover ) ;
78-
79- Controller . Instance . RegistrarLog ( "Posição Inicial do Rover: " + rover . GetPosition ( ) ) ;
80-
81- Controller . Instance . RegistrarLog ( "Informe a sequência de instruções para o Rover: " ) ;
82-
83- var instructions = Console . ReadLine ( ) ;
84-
85- Controller . Instance . RegistrarLog ( "Instruções: " + instructions ) ;
86-
87- var invoker = new CommandInvoker ( ) ;
88-
89- foreach ( var instruction in instructions )
90- {
91- try
92- {
93- ICommand command = instruction switch
94- {
95- 'L' => new TurnLeftCommand ( rover ) ,
96- 'R' => new TurnRightCommand ( rover ) ,
97- 'M' => new MoveCommand ( rover ) ,
98- _ => throw new ArgumentException ( "Instrução inválida. As instruções válidas são: L, R, M." )
99- } ;
100-
101- invoker . AddCommand ( command ) ;
102-
103- invoker . ExecuteCommands ( ) ;
104-
105- Controller . Instance . RegistrarLog ( "Instrução executada: " + instruction ) ;
106- }
107- catch ( Exception ex )
108- {
109- Controller . Instance . RegistrarErro ( ex . Message ) ;
110- }
111- }
32+ builder . ConfigureServices ( ( services ) =>
33+ {
34+ services . ResolveDependencies ( ) ;
35+ } ) ;
11236
113- rovers . Add ( rover ) ;
114- }
37+ var app = builder . Build ( ) ;
11538
116- }
117- catch ( Exception ex )
118- {
119- Controller . Instance . RegistrarErro ( ex . Message ) ;
120- }
121- finally
122- {
123- Controller . Instance . RegistrarLog ( "Exploração de Marte finalizada." ) ;
124- }
39+ app . Services . GetService < IApplication > ( ) . Run ( ) ;
12540 }
41+
12642 }
12743}
0 commit comments