Пример #1
0
        static void Main(string[] args)
        {
            Parser   parser = new Parser();
            Board    board  = new Board();
            ToyRobot robot  = new ToyRobot();

            // parse cmd line arguments
            Options options = new Options();

            CommandLine.Parser.Default.ParseArguments <Options>(args)
            .WithParsed <Options>(opts => options = opts);

            List <string> cmdList = parser.GetCmdList(@"../../Test Data/" + options.InputFile);

            // runs through each command and passes it to toy robot to execute
            foreach (var cmd in cmdList)
            {
                // captures result from execution and writes to file

                var    firstCmd = cmd.Split(' ')[0];
                string result   = "";

                if (ToyRobot.IsToyCommand(firstCmd))
                {
                    result = robot.Execute(cmd, board);
                }
                else if (Board.IsBoardCommand(firstCmd))
                {
                    result = board.Execute(cmd, robot);
                }

                parser.WriteToFile(options.OutputFile, result);

                // resets after exit condition is 'echo' as report and validate are inconsistent
                if (cmd.StartsWith("echo"))
                {
                    board = new Board();
                    robot = new ToyRobot();
                }
            }



            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
        /// <summary>
        /// this main function is running simulator for file processing
        /// </summary>
        /// <param name="fileCommandString">file command string, e.g. Sample.txt, Sample2.txt, D:\TestData\Sample.txt</param>
        private static void RunCommandFileProcessingSimulator(string fileCommandString)
        {
            // initializing a toy robot with  in the origin (0,0) facing to North
            var toyRobot = new ToyRobot(new Table(TableWidth, TableHeight));
            var commandFileProcessingSimulator = new CommandFileProcessingSimulator(toyRobot, FileExtension);

            Console.WriteLine("");
            Console.WriteLine("Processing input command files.");
            Console.WriteLine($"You are running a table size is {toyRobot.Table?.Width ?? 0} units x {toyRobot.Table?.Height ?? 0} units.");
            Console.WriteLine("");
            Console.WriteLine("Result is here:");
            Console.WriteLine("");

            try
            {
                commandFileProcessingSimulator.Execute(fileCommandString, CmdLineStringSeparator, IgnoreCase);
            }
            // we catch specific exception from file simulator, then just console log to cmd but not stop application
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        /// <summary>
        /// this main function is running interactive simulator
        /// </summary>
        private static void RunInteractiveSimulator()
        {
            // initializing a toy robot in the origin (0,0) facing to North
            var toyRobot             = new ToyRobot(new Table(TableWidth, TableHeight));
            var interactiveSimulator = new InteractiveSimulator(toyRobot);

            // showing description text
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("These are valid command in the interactive mode");
            Console.WriteLine("PLACE x,y,f");
            Console.ResetColor();
            Console.WriteLine("- Where x and y is coordinates and f (facing) must be either NORTH, SOUTH, WEST or EAST");
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("MOVE");
            Console.ResetColor();
            Console.WriteLine("- Will move the robot one unit in current direct");
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("LEFT");
            Console.ResetColor();
            Console.WriteLine("- Will rotate the robot 90 degrees to the left");
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("RIGHT");
            Console.ResetColor();
            Console.WriteLine("- Will rotate the robot 90 degrees to the right");
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("REPORT");
            Console.ResetColor();
            Console.WriteLine("- The robot will say the current position and facing direction");

            Console.WriteLine();
            Console.WriteLine("--------------------------------------------Constraints--------------------------------------------------------");
            Console.WriteLine();
            Console.WriteLine("- The toy robot must not fall off the table during movement. This also includes the initial placement of the toy robot");
            Console.WriteLine("- Any move that would cause the robot to fall must be ignored");
            Console.WriteLine();
            Console.WriteLine("---------------------------------------------------------------------------------------------------------------");
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("--------------------------------------------Sample command-----------------------------------------------------");
            Console.WriteLine();
            Console.WriteLine("PLACE 1,1,NORTH MOVE MOVE RIGHT REPORT");
            Console.WriteLine("PLACE 1,2,NORTH MOVE MOVE RIGHT REPORT PLACE 3,2,EAST REPORT");
            Console.WriteLine("PLACE 2,3,NORTH MOVE MOVE LEFT PLACE 0,0,EAST REPORT");
            Console.WriteLine("");
            Console.WriteLine("Or you can just one by one input then press enter");
            Console.WriteLine("PLACE 1,1,NORTH");
            Console.WriteLine("MOVE");
            Console.WriteLine("RIGHT");
            Console.WriteLine("REPORT");
            Console.WriteLine();
            Console.WriteLine("---------------------------------------------------------------------------------------------------------------");
            Console.WriteLine("");
            Console.WriteLine($"Now you have a table size of {toyRobot.Table?.Width ?? 0} units x {toyRobot.Table?.Height ?? 0} units to play with... It's time to try some commands or you can input 'q' to quit in anytime");
            Console.ResetColor();
            Console.WriteLine("");

            while (true)
            {
                var commandString = Console.ReadLine();

                // if user wanna quit in the cmd
                if (string.Equals(commandString, "q", StringComparison.OrdinalIgnoreCase) ||
                    string.Equals(commandString, "quit", StringComparison.OrdinalIgnoreCase) ||
                    string.Equals(commandString, "exit", StringComparison.OrdinalIgnoreCase))
                {
                    break;
                }

                interactiveSimulator.Execute(commandString, CmdLineStringSeparator, IgnoreCase);
            }
        }