static void Main(string[] args) { var robot = new ToyRobot(); Dictionary <string, Type> availableCommands = new Dictionary <string, Type> { { "PLACE", typeof(PlaceCommand) }, { "MOVE", typeof(MoveCommand) }, { "LEFT", typeof(LeftCommand) }, { "RIGHT", typeof(RightCommand) }, { "REPORT", typeof(ReportCommand) } }; while (true) { var input = Console.ReadLine(); var command = string.Empty; var arguments = string.Empty; if (!String.IsNullOrEmpty(input)) { var inputSplitted = input.Split(' '); if (inputSplitted.Length > 0) { command = inputSplitted[0]; } if (inputSplitted.Length == 2) { arguments = inputSplitted[1]; } if (!availableCommands.ContainsKey(command.ToUpper()) || !availableCommands.TryGetValue(command.ToUpper(), out Type value)) { continue; } try { dynamic cmd = Activator.CreateInstance(value, arguments); robot.Execute(cmd); } catch (Exception ex) { if (ex.InnerException != null) { Console.WriteLine(ex.InnerException.Message); } else { Console.WriteLine(ex.Message); } } } } }
private static void ExecuteCommandsFrom(string path) { var toyRobot = new ToyRobot(); var simulator = new Simulator(toyRobot); using (var file = new StreamReader(path)) { string command; while ((command = file.ReadLine()) != null) { Console.WriteLine("Executing command: " + command); simulator.Execute(command); } } }
public Simulator(ToyRobot toyRobot) { _toyRobot = toyRobot; }
static void Main(string[] args) { Console.WriteLine("Welcome to the Toy Robot Simulator"); Console.WriteLine(); Console.WriteLine("The application is a simulation of a toy robot moving on a square tabletop, of dimensions 5 units x 5 units."); Console.WriteLine("There are no other obstructions on the table surface."); Console.WriteLine("The origin (0,0) can be considered to be the SOUTH WEST most corner"); Console.WriteLine(); Console.WriteLine("The following commands can be used to interact with the robot:"); Console.WriteLine(" - PLACE X,Y,F => PLACE will put the toy robot on the table in position X,Y and facing NORTH, SOUTH, EAST or WEST"); Console.WriteLine(" - MOVE => MOVE will move the toy robot one unit forward in the direction it is currently facing"); Console.WriteLine(" - LEFT / RIGHT => LEFT and RIGHT will rotate the robot 90 degrees in the specified direction without changing the position of the robot"); Console.WriteLine(" - REPORT => REPORT will announce the X,Y and F of the robot"); Console.WriteLine(" - EXIT => REPORT will exit and close the simulator"); var toyRobot = new ToyRobot(); while (true) { var input = Console.ReadLine().ToUpper().Trim(); if (input.Equals("exit", StringComparison.OrdinalIgnoreCase)) { break; } try { switch (input) { case var someVal when new Regex(@"(PLACE) [0-4],[0-4],(NORTH|SOUTH|EAST|WEST)").IsMatch(someVal): string parameters = someVal.Replace("PLACE ", string.Empty); int x = int.Parse(parameters.Substring(0, 1)); int y = int.Parse(parameters.Substring(2, 1)); string facing = parameters.Substring(4); switch (facing) { case "NORTH": toyRobot.Place(x, y, Facing.North); break; case "SOUTH": toyRobot.Place(x, y, Facing.South); break; case "EAST": toyRobot.Place(x, y, Facing.East); break; case "WEST": toyRobot.Place(x, y, Facing.West); break; } break; case "MOVE": toyRobot.Move(); break; case "LEFT": toyRobot.Left(); break; case "RIGHT": toyRobot.Right(); break; case "REPORT": Console.WriteLine(toyRobot.Report()); break; default: Console.WriteLine("Not a valid command! Please try again."); break; } } catch (NoValidPlaceCommandExecutedException) { Console.WriteLine("No valid Place command previously executed! Please try again"); } catch (RobotWillFallException) { Console.WriteLine("This move will make the robot fall! Please try again"); } } }
static void Main(string[] args) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Welcome to TOY ROBOT SIMULATOR"); sb.AppendLine(""); sb.AppendLine("Instructions for standard input: "); sb.AppendLine(""); sb.AppendLine("1. Place the toy robot within a 5 x 5 table "); sb.AppendLine(" surface using the following command (case-sensitive):"); sb.AppendLine(""); sb.AppendLine(" PLACE X,Y,F (where X and Y are the coordinates "); sb.AppendLine(" and F (Facing) must be either NORTH, SOUTH,"); sb.AppendLine(" EAST OR WEST. Example: PLACE 2,3,NORTH"); sb.AppendLine(""); sb.AppendLine("2. After placing toy robot, you can do the following"); sb.AppendLine(" actions or commands (case-sensitive):"); sb.AppendLine(""); sb.AppendLine(" MOVE - Moves the toy robot one unit forward in the"); sb.AppendLine(" direction it is currently facing."); sb.AppendLine(" LEFT - Shift the toy robot 90 degrees counter-clockwise"); sb.AppendLine(" without changing the location."); sb.AppendLine(" RIGHT - Shift the toy robot 90 degrees clockwise"); sb.AppendLine(" without changing the location."); sb.AppendLine(" REPORT - displays the location of the toy robot."); sb.AppendLine(""); sb.AppendLine("Instructions for command text file processing: "); sb.AppendLine(""); sb.AppendLine("1. FILE - Opens up location to enter the full path and filename"); sb.AppendLine(""); sb.AppendLine("2. Provide the location path and name of the command file"); sb.AppendLine(@" (e.g. Enter Full Path and Filename: C:\CommandFiles\CmdBatchFile1.txt"); sb.AppendLine(); sb.AppendLine(); Console.WriteLine(sb.ToString()); IToyRobot robot = new ToyRobot(new TableSurface(5, 5)); while (true) { Console.Write("Enter Command: "); string command = Console.ReadLine(); if (!command.Contains("FILE")) { if (string.IsNullOrEmpty(command)) { continue; } try { string output = robot.DoCommand(command); if (!string.IsNullOrEmpty(output)) { Console.WriteLine(output); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } else { //Process File Console.Write("Enter Full Path and Filename: "); string fileName = Console.ReadLine(); string line; if (File.Exists(fileName)) { StreamReader file = new StreamReader(fileName); while ((line = file.ReadLine()) != null) { if (string.IsNullOrEmpty(line.Trim())) { continue; } try { Console.WriteLine(line.Trim()); string output = robot.DoCommand(line.Trim()); if (!string.IsNullOrEmpty(output)) { Console.WriteLine(output); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } else { Console.WriteLine("Command file does not exists. Try again."); } } } }