Skip to content

Instantly share code, notes, and snippets.

@DhavalDalal
Last active July 22, 2019 01:03
Show Gist options
  • Save DhavalDalal/211c3eebbb4fc744ab36 to your computer and use it in GitHub Desktop.
Save DhavalDalal/211c3eebbb4fc744ab36 to your computer and use it in GitHub Desktop.
Smelly Rover in C#

Smelly Mars Rover (C#)

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).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Rover
{
/**
* 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 MarsRoboRover
{
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
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(" ".ToCharArray());
x = Convert.ToInt32(positions[0], 10);
y = Convert.ToInt32(positions[1], 10);
dir = positions[2];
foreach (char command in commands.ToCharArray()) {
MarsRoboRover.Rove(command);
}
//Output Status and Result
Console.WriteLine("currentPosition..." + currentPosition);
Console.WriteLine("commands..." + commands);
Console.WriteLine("newPosition..." + x + " " + y + " " + dir);
}
/**
* All logic for movement is in this method
* @param command char
*/
private static void Rove(char command)
{
// Console.WriteLine("Start....." + x + " " + y + " " + direction + " " + command);
if (dir.ToUpper().Equals("N"))
{
switch (command)
{
case 'L':
dir = "W";
break;
case 'R':
dir = "E";
break;
case 'M':
y++;
break;
}
}
else if (dir.ToUpper().Equals("E"))
{
switch (command)
{
case 'L':
dir = "N";
break;
case 'R':
dir = "S";
break;
case 'M':
x++;
break;
}
}
else if (dir.ToUpper().Equals("S"))
{
switch (command)
{
case 'L':
dir = "E";
break;
case 'R':
dir = "W";
break;
case 'M':
y--;
break;
}
}
else if (dir.ToUpper().Equals("W"))
{
switch (command)
{
case 'L':
dir = "S";
break;
case 'R':
dir = "N";
break;
case 'M':
x--;
break;
}
}
// Console.WriteLine("End....." + x + " " + y + " " + direction + " " + command);
}
public static String Location()
{
String loc = "[" + x + ", " + "y" + ", " + dir + "]";
return loc;
}
public static int Y
{
get { return MarsRoboRover.y; }
set { MarsRoboRover.y = value; }
}
public static int X
{
get { return MarsRoboRover.x; }
set { MarsRoboRover.x = value; }
}
public static string Dir
{
get { return MarsRoboRover.dir; }
set { MarsRoboRover.dir = value; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment