-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarsRovers.java
165 lines (131 loc) · 4.29 KB
/
MarsRovers.java
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.io.*;
public class MarsRovers{
public static void main(String[] args) {
try{
String line;
String nav;
String tokens[];
int i, plateau;
int xmax, ymax;
int rx, ry;
char limit;
char rd, rdnew;
//leitura do arquivo de entrada
FileInputStream fs = new FileInputStream("mars_rovers.txt");
DataInputStream in = new DataInputStream(fs);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//abrir arquivo para gravação de resultados
FileWriter fw = new FileWriter("mars_rovers_results.txt");
PrintWriter pw = new PrintWriter(fw);
line = br.readLine();
tokens = line.split(" ");
//leitura da primeira linha (valores máximos de navegação)
xmax = Integer.parseInt(tokens[0]);
ymax = Integer.parseInt(tokens[1]);
System.out.println("--------------------------------------------------");
System.out.println(" Mars Rovers Navigation" );
System.out.println("--------------------------------------------------");
System.out.println("Low-left coordinates: (0,0)");
System.out.println("Upper-right coordinates: ("+ xmax + ","+ ymax + ")");
System.out.println();
plateau = (xmax + 1) * (ymax + 1); // tamanho do tabuleiro
System.out.println("Paltaeu: " + plateau + " grids.");
System.out.println();
while((line = br.readLine()) != null){
tokens = line.split(" ");
rx = Integer.parseInt(tokens[0]);
ry = Integer.parseInt(tokens[1]);
rd = tokens[2].charAt(0);
System.out.println("--------------------------------------------------");
System.out.println("Starting navigation...");
System.out.println();
System.out.println("Initial rover's position: ("+ rx +","+ ry +")");
System.out.println("Rover's orietation: " + rd);
System.out.println();
pw.printf("--------------------------------------------------\n");
pw.printf("Starting navigation...\n");
pw.println();
pw.printf("Initial rover's position: (%d, %d)\n", rx, ry);
pw.printf("Rover's orietation: %c", rd);
pw.println();
nav = br.readLine();
System.out.println("String of navigation: " + nav);
for (i = 0; i < nav.length() ; i++) {
rdnew = ' ';
switch(nav.charAt(i)){
case 'L':
if(rd == 'E') rdnew = 'N';
if(rd == 'N') rdnew = 'W';
if(rd == 'W') rdnew = 'S';
if(rd == 'S') rdnew = 'E';
rd = rdnew;
System.out.println("Orietation changed!");
System.out.println();
break;
case 'R':
if(rd == 'E') rdnew = 'S';
if(rd == 'N') rdnew = 'E';
if(rd == 'W') rdnew = 'N';
if(rd == 'S') rdnew = 'W';
rd = rdnew;
System.out.println("Orietation changed!");
System.out.println();
break;
case 'M':
limit = 'N';
if(rd == 'E'){
if(rx == xmax)
limit = 'S';
else
rx = rx + 1;
}
if(rd == 'N'){
if(ry == ymax)
limit = 'S';
else
ry = ry + 1;
}
if(rd == 'W'){
if(rx == 0)
limit = 'S';
else
rx = rx - 1;
}
if(rd == 'S'){
if(ry == 0)
limit = 'S';
else
ry = ry - 1;
}
if (limit == 'N'){
Thread.sleep(1000);
System.out.println("Moving completed!");
Thread.sleep(500);
System.out.println();
} else {
System.out.println("Movement not realized! Limit of the plateau found.");
Thread.sleep(1500);
System.out.println();
}
break;
}
System.out.println("Rover's position: X = " + rx + " Y = " + ry + " orietation = "+ rd);
}
System.out.println();
System.out.println("Final rover's position: ("+ rx +","+ ry +")");
System.out.println("Final rover's orietation: " + rd);
pw.printf("Final rover's position: (%d,%d)\n", rx, ry);
pw.printf("Final rover's orietation: %c\n", rd);
}
// fecha arquivo
System.out.println("-------------------------------------------------------------");
System.out.println("The End.");
System.out.println();
in.close();
fw.close();
}
catch(Exception e){
System.out.println("Error: " + e);
}
}
}