Skip to content

Instantly share code, notes, and snippets.

@DhavalDalal
Last active March 3, 2023 06:51
Show Gist options
  • Save DhavalDalal/4b26b55d6b4435a3df2e to your computer and use it in GitHub Desktop.
Save DhavalDalal/4b26b55d6b4435a3df2e to your computer and use it in GitHub Desktop.
Smelly Mars Rover in Java

Smelly Mars Rover (Java)

A squad of robotic rovers is to be landed by NASA on a plateau on Mars. This plateau, which is curiously rectangular, must be navigated by the rover so that its on-board cameras can get a complete view of the surrounding terrain to send back to Earth.

A rover's position and location is represented by a combination of x and y co-ordinates and a letter representing one of the four cardinal compass points. The plateau is divided up into a grid to simplify navigation. An example position might be 0, 0, N, which means the rover is in the bottom left corner and facing North.

In order to control the rover, NASA sends a simple string of letters. The possible letters are 'L', 'R' and 'M'. 'L' and 'R' makes the rover spin 90 degrees left or right respectively, without moving from its current spot. 'M' means move forward one grid point, and maintain the same heading.

Assume that the square directly North from (x, y) is (x, y+1).

package explorer;
import java.util.logging.Logger;
/**
* This program implements a mission critical application that God alone know its real intent.
* It feeds me, a sole reason for me to stick to this project. Long live such guess-good-for-what-projects.
*
* @author Chitti Bush
* @version 1.0
* @since 2000-03-31
*/
public class MarsRover {
private static int x = 0; //Position on x-axis
private static int y = 0; //Position on y-axis
private static String dir = ""; //Direction the rover is facing
//Logger to log information
private Logger logger = Logger.getLogger("MarsRoboRover") ;
public static void main(String[] args) {
//Other Test Inputs
// String currentPosition = "1 2 N";
// String commands = "LMLMLMLMM";
String currentPosition = "3 3 E";
String commands = "MMRMMRMRRM";
String[] positions = currentPosition.split(" ");
x = Integer.valueOf(positions[0]);
y = Integer.valueOf(positions[1]);
dir = positions[2];
for (char command : commands.toCharArray()) {
rove(command);
}
//Output Status and Result
System.out.println("currentPosition..." + currentPosition);
System.out.println("commands..." + commands);
System.out.println("newPosition..." + x + " " + y + " " + dir);
}
/**
* All logic for movement is in this method
* @param command char
*/
private static void rove(char command) {
// System.out.println("Start....." + x + " " + y + " " + direction + " " + command);
if (dir.equalsIgnoreCase("N")) {
switch (command) {
case 'L':
dir = "W";
break;
case 'R':
dir = "E";
break;
case 'M':
y++;
break;
}
} else if (dir.equalsIgnoreCase("E")) {
switch (command) {
case 'L':
dir = "N";
break;
case 'R':
dir = "S";
break;
case 'M':
x++;
break;
}
} else if (dir.equalsIgnoreCase("S")) {
switch (command) {
case 'L':
dir = "E";
break;
case 'R':
dir = "W";
break;
case 'M':
y--;
break;
}
} else if (dir.equalsIgnoreCase("W")) {
switch (command) {
case 'L':
dir = "S";
break;
case 'R':
dir = "N";
break;
case 'M':
x--;
break;
}
}
// System.out.println("End....." + x + " " + y + " " + direction + " " + command);
}
public static String location() {
String loc = "[" + x + ", " + "y" + ", " + dir + "]";
return loc;
}
public static int getX() {
return x;
}
public static void setX(int x) {
MarsRover.x = x;
}
public static int getY() {
return y;
}
public static void setY(int y) {
MarsRover.y = y;
}
public static String getDir() {
return dir;
}
public static void setDir(String dir) {
MarsRover.dir = dir;
}
public Logger getLogger() {
return logger;
}
public void setLogger(Logger logger) {
this.logger = logger;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment