Last active
May 8, 2020 06:06
-
-
Save raviyasas/1fc350f97a80fcc1d0ebdc839fbec285 to your computer and use it in GitHub Desktop.
The solution for the Mars Rover problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.eyepax; | |
public class Constants { | |
public static final Integer N = 1; | |
public static final Integer E = 2; | |
public static final Integer S = 3; | |
public static final Integer W = 4; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.eyepax; | |
import java.util.Objects; | |
public class MarsRoverProblem { | |
Integer x = 0; | |
Integer y = 0; | |
Integer front = 1; | |
public void setRoverPosition(Integer x, Integer y, Integer front) { | |
this.x = x; | |
this.y = y; | |
this.front = front; | |
} | |
// print the rover position | |
public void printRoverPosition() { | |
char direction = 'N'; | |
switch (front) { | |
case 1: | |
direction = 'N'; | |
break; | |
case 2: | |
direction = 'E'; | |
break; | |
case 3: | |
direction = 'S'; | |
break; | |
case 4: | |
direction = 'W'; | |
break; | |
default: | |
break; | |
} | |
System.out.println(x + " " + y + " " + direction); | |
} | |
// turn the rover left | |
private void turnRoverLeft() { | |
if (front - 1 < Constants.N) | |
front = Constants.W; | |
else | |
front = front - 1; | |
} | |
// turn the rover right | |
private void turnRoverRight() { | |
if (front + 1 > Constants.W) | |
front = Constants.N; | |
else | |
front = front + 1; | |
} | |
// move the rover | |
private void move() { | |
if (Objects.equals(front, Constants.N)) { | |
this.y++; | |
} else if (Objects.equals(front, Constants.E)) { | |
this.x++; | |
} else if (Objects.equals(front, Constants.S)) { | |
this.y--; | |
} else if (Objects.equals(front, Constants.W)) { | |
this.x--; | |
} | |
} | |
public void process(String input) { | |
if (input != null) { | |
for (int i = 0; i < input.length(); i++) { | |
process(input.charAt(i)); | |
} | |
} | |
} | |
// process the rover position | |
private void process(Character command) { | |
if (command.equals('L')) { | |
turnRoverLeft(); | |
} else if (command.equals('R')) { | |
turnRoverRight(); | |
} else if (command.equals('M')) { | |
move(); | |
} else { | |
System.err.println("Error occurred"); | |
} | |
} | |
public static void main(String args[]) { | |
MarsRoverProblem roverProblem = new MarsRoverProblem(); | |
roverProblem.setRoverPosition(1, 2, Constants.N); | |
roverProblem.process("LMLMLMLMM"); | |
roverProblem.printRoverPosition(); | |
roverProblem.setRoverPosition(3, 3, Constants.E); | |
roverProblem.process("MMRMMRMRRM"); | |
roverProblem.printRoverPosition(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment