/// <summary> /// Sets the instructions /// </summary> /// <returns></returns> public static InstructionInput SetInstructions() { var isPatternMatched = false; var instructionSet = new List <char>(); do { Console.WriteLine("\n>> Enter instructions (L|R|M)+:"); var input = Console.ReadLine().ToUpper(); var regex = new Regex(@"^[L|R|M]+$"); Match match = regex.Match(input); isPatternMatched = match.Success; if (!isPatternMatched) { Console.WriteLine("!! Incorrect input pattern! Please try again with proper input values...\n"); continue; } input.ToCharArray().ToList().ForEach(x => instructionSet.Add(x)); }while (!isPatternMatched); var instructions = new InstructionInput() { InstructionSet = instructionSet }; return(instructions); }
/// <summary> /// Executes instruction set for the specified rover /// </summary> /// <param name="position"></param> /// <param name="instructionInput"></param> public static void ExecuteInstructions(Position position, InstructionInput instructionInput) { foreach (var instruction in instructionInput.InstructionSet) { switch (instruction) { case 'M': Move(position); break; default: Rotate(position, instruction); break; } } Console.WriteLine($"\n>> Rover's current position: {position.X} {position.Y} {position.Direction}"); Console.WriteLine("\n\n----------PRESS ANY KEY FOR ANOTHER INSTRUCTION----------"); Console.ReadLine(); }