//Checks for user input validity. public LaunchInput ValidateInput(List <string> RawData) { LaunchInput Data = new LaunchInput(); try{ if (RawData.Count == 6) { try{ if (IsPositiveNumber(Int32.Parse(RawData[0]), Int32.Parse(RawData[1]), Int32.Parse(RawData[2]), Int32.Parse(RawData[3]))) { Data.x = Int32.Parse(RawData[2]); Data.y = Int32.Parse(RawData[3]); Data.Limit_x = Int32.Parse(RawData[0]); Data.Limit_y = Int32.Parse(RawData[1]); Data.Direction = (RawData[4].ToUpper()).ToCharArray()[0]; Data.Instructions = RawData[5].ToUpper(); } else { throw new System.ArgumentException(); } if (Data.x > Data.Limit_x || Data.y > Data.Limit_y) { ValidationFeedback("bounds"); return(null); } } catch { ValidationFeedback("coordinate"); return(null); } foreach (char move in Data.Instructions) { if (move != 'L' && move != 'M' && move != 'R') { Console.WriteLine(move); ValidationFeedback("instructions"); return(null); } } if (!("ESWN").Contains(Data.Direction)) { ValidationFeedback("direction"); return(null); } } return(Data); }catch (Exception) { return(null); } }
/* private static void AcceptInput() * { * ConsoleKeyInfo key = Console.ReadKey(); * * switch (key.Key) * { * case ConsoleKey.LeftArrow: * _left--; * break; * case ConsoleKey.RightArrow: * _left++; * break; * case ConsoleKey.UpArrow: * _top--; * break; * case ConsoleKey.DownArrow: * _top++; * break; * * } * } * private static void DrawScreen() * { * Console.Clear(); * Console.SetCursorPosition(_left, _top); * Console.Write('*'); * } */static void Main(string[] args) { //Reads arguments from the stadard output. List <string> RawData = new List <string>(); RawData.AddRange(args); Console.Clear(); //validates User input UserInputs Transmission = new UserInputs(); LaunchInput Data = Transmission.ValidateInput(RawData); if (Data != null) { //Impliments the the valid input. Navigation connection = new Navigation(); //if (Data.Count() < 6) Console.WriteLine(connection.NavigateRover(Data)); } }
public string NavigateRover(LaunchInput Transmission) { try{ //Map dimensions int xLimit = Transmission.Limit_x, yLimit = Transmission.Limit_y; //Starting position int x = Transmission.x, y = Transmission.y; char direction = Transmission.Direction; //User Input string instruction = Transmission.Instructions; foreach (char move in instruction) { if (move == 'M') { //If the rover is moving on the along the X-axis (Inner Array), Moves Left/Right if (direction == 'E' || direction == 'W') { switch (direction) { case 'E': x++; break; case 'W': x--; break; } } //If the rover is moving on the along the Y-axis (Main Array), Moves Up/Down. else if (direction == 'N' || direction == 'S') { switch (direction) { case 'S': y++; break; case 'N': y--; break; } } // Check if new position is still within bounds. if (x >= xLimit || y >= yLimit) { Console.WriteLine("You have driven the Rover out of setalite range..."); return(" --- Mission Failed !! ---"); } } else { direction = ChangeDirection(direction, move); } } Console.WriteLine("******** Successful transmission... ********"); return("\tCurrent Rover position - " + x + " " + y + " " + direction + "\n"); } catch (Exception) { Console.WriteLine("Input Error!\n \t Example input : 25 25 12 19 S MMRLMMMMML"); return("Transmission to the Mars Rover has Failed!\n\n"); } }