-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoverExplore.java
More file actions
69 lines (64 loc) · 2.95 KB
/
RoverExplore.java
File metadata and controls
69 lines (64 loc) · 2.95 KB
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
import java.util.Scanner;
public class RoverExplore {
public static void main(String args[]){
int topX, topY;
System.out.println("\t===========================================");
System.out.println("\t NASA MOON EXPLORATION BY ROBOTIC ROVERS");
System.out.println("\t===========================================");
//Input the top right coordinates of the moon plateau
System.out.println("Enter the top right coordinates(INTEGERS) of the moon plateau with a SINGLE space between the " +
"two coordinates (In such a manner:5 5)");
Scanner input = new Scanner(System.in);
String initialiseMoon = input.nextLine();
topX = initialiseMoon.charAt(0)-'0';
topY = initialiseMoon.charAt(2)-'0';
Plateau moon = new Plateau(topX,topY);
int x,y,numMoonRovers;
int num = 0;
char o;
//Determine the number of moon rovers that are to be deployed on the moon
System.out.println("How many moon rovers do you want to deploy(Enter INTEGER) ?");
numMoonRovers = input.nextInt();
input.nextLine();//Clears input stream
MoonRover[] rover = new MoonRover[numMoonRovers];
System.out.println("The Moon Rover's Position Comprises its X and Y coordinate(INTEGERS) " +
"and a letter representing orientation which is a Cardinal Point");
System.out.println("A Rover exploring the plateau may be given the following instructions:" +
" spin left use letter L, spin right use letter R or move use M\n");
do{
//Input the initial coordinates and orientation of the moon rover
System.out.println("Enter the start coordinates(INTEGERS) of the rover and its orientation(N/S/E/W)\n" +
"Put a SINGLE SPACE between the inputs(In such a manner:1 3 N");
String initialiseRover = input.nextLine();
x = initialiseRover.charAt(0)-'0';
y = initialiseRover.charAt(2)-'0';
o = initialiseRover.charAt(4);
rover[num] = new MoonRover(x, y, o);
//Input the instructions of how the rover is to explore the moon plateau
System.out.println("Enter the instruction(LETTERS) of how the rover is to explore\n " +
"LEAVE NO SPACE between the instructions(In such a manner:LMLMLMR)");
String explore = input.nextLine();
for(int i=0; i<explore.length(); i++){
if(explore.charAt(i)=='L')
rover[num].spinLeft();
else if(explore.charAt(i)=='R')
rover[num].spinRight();
else if(explore.charAt(i)=='M')
rover[num].move();
else
//when invalid entries are input
System.out.println("Error in explore command\n");
}
num++;
//Execute one rover at a time
}while(num < numMoonRovers);
for(int i=0;i<numMoonRovers;i++){
//Rovers are numbered from 0 to numMoonRovers-1
//Output Rovers Location and orientation
System.out.println("\t============================================");
System.out.println("\tFinal Coordinates and Heading of Moon Rovers");
System.out.println("\t============================================");
System.out.println("\t Rover " + i +" is at "+ rover[i].whereAreYou());
}
}
}