コード例 #1
0
        /// <summary>
        ///  main function
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.CancelKeyPress += new ConsoleCancelEventHandler((object sender, System.ConsoleCancelEventArgs e)
                                                                    => {
                Console.WriteLine("exiting...");
                System.Environment.Exit(0);
            });

            try
            {
                string debugfile = GetConfigStringValue(Configurations.DebugLog);
                Logger.Init(debugfile);

                int tableRangeX = GetConfigIntValue(Configurations.TableLimitX, 5);
                int tableRangeY = GetConfigIntValue(Configurations.TableLimitY, 5);

                WelcomeMessage(tableRangeX, tableRangeY);
                ToyTable tb    = new ToyTable(tableRangeY, tableRangeX);
                ToyRobot robot = new ToyRobot(tb);
                StartUserInput(robot);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                Console.Error.WriteLine(ex.StackTrace);
                System.Environment.Exit(1);
            }
        }
コード例 #2
0
        /// <summary>
        /// Entry Point for the simulator
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            ToyRobot  tr        = new ToyRobot();
            TableTop  tp        = new TableTop(5, 5);
            Simulator simulator = new Simulator(tr, tp);

            Console.WriteLine("*********welcome to toy robot on a table of 5*5 **************");
            Console.WriteLine("Valid commands to place on table: PLACE X,Y,NORTH|SOUTH|EAST|WEST (example: place 1,2,north)");
            Console.WriteLine("Valid commands to move on table: MOVE|LEFT|RIGHT|REPORT|EXIT");

            bool keepRunning = true;

            while (keepRunning)
            {
                String inputString = Console.ReadLine();
                if (!string.IsNullOrWhiteSpace(inputString))
                {
                    if (String.Equals(Command.EXIT.ToString(), inputString, StringComparison.OrdinalIgnoreCase))
                    {
                        keepRunning = false;
                        Console.WriteLine("robot existing the table : press any key again to exit");
                        Console.ReadLine();
                    }
                    else
                    {
                        try
                        {
                            CommandModel commandModel = CommandParser.GetCommand(inputString);
                            if (commandModel != null)
                            {
                                if (commandModel.Command == Command.REPORT)
                                {
                                    Console.WriteLine(simulator.Report());
                                }
                                else
                                {
                                    simulator.Action(commandModel).Execute();
                                    Console.WriteLine(simulator.Report());
                                }
                            }
                            else
                            {
                                Console.WriteLine("command not accepted : " + inputString);
                                Console.WriteLine("Valid commands to place on table: PLACE X,Y,NORTH|SOUTH|EAST|WEST (example: place 1,2,north)");
                                Console.WriteLine("Valid commands to move on table: MOVE|LEFT|RIGHT|REPORT|EXIT");
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("log : " + e.Message.ToString());
                        }
                    }
                }
                else
                {
                    Console.WriteLine("entered input string is null of empty");
                }
            }
        }
コード例 #3
0
        private static ICommandReader InitializeCommandReader(
            int numRobots,
            ICommandParser commandParser)
        {
            var commandHandlers = new ICommandHandler[numRobots];

            for (var i = 0; i < numRobots; i++)
            {
                var initialRobotState = new NotPlacedRobotState();
                var toyRobot          = new ToyRobot.ToyRobot(initialRobotState);
                commandHandlers[i] = new CommandHandler(toyRobot, commandParser);
            }

            return(new CommandReader(commandHandlers));
        }
コード例 #4
0
 /// <summary>
 /// start reading standard input for robot commands
 /// </summary>
 /// <param name="robot">robot location</param>
 private static void StartUserInput(ToyRobot robot)
 {
     while (true)
     {
         string command = Console.ReadLine();
         try
         {
             robot.InterpretCommand(command);
             Logger.Log($"{command} succeeded. Robot current location: {robot.Location},{robot.CurrentDirection}");
         }
         // catch robot exceptions and log to debug log. else throw the exception.
         catch (RobotException ex)
         {
             Logger.Log(ex);
         }
     }
 }
コード例 #5
0
        public ToyRobotAssembly(ITextInputter textInputter, ITextOutputter textOutputter)
        {
            var initialRobotState        = new NotPlacedRobotState();
            var toyRobot                 = new ToyRobot(initialRobotState);
            var robotStateFactory        = new RobotStateFactory();
            var robotStateBuilderFactory = new RobotStateBuilderFactory();
            var leftOrientationTurner    =
                new LeftOrientationTurner(robotStateBuilderFactory);
            var rightOrientationTurner =
                new RightOrientationTurner(robotStateBuilderFactory);
            var moveStateTransformer =
                new MoveStateTransformer(robotStateBuilderFactory);
            var tableDimensions         = new TableDimensions();
            var moveAttempter           = new MoveAttempter(moveStateTransformer, tableDimensions);
            var commandPerformerFactory = new CommandPerformerFactory(
                robotStateFactory,
                leftOrientationTurner,
                rightOrientationTurner,
                moveAttempter,
                tableDimensions,
                textOutputter);
            var orientationFactory  = new OrientationParser();
            var reportCommandParser = new ReportCommandParser(commandPerformerFactory);
            var moveCommandParser   = new MoveCommandParser(commandPerformerFactory);
            var leftCommandParser   = new LeftCommandParser(commandPerformerFactory);
            var rightCommandParser  = new RightCommandParser(commandPerformerFactory);
            var placeCommandParser  = new PlaceCommandParser(
                commandPerformerFactory, orientationFactory);
            var commandParsers = new ICommandParser[]
            {
                reportCommandParser,
                moveCommandParser,
                leftCommandParser,
                rightCommandParser,
                placeCommandParser
            };
            var masterCommandParser = new MasterCommandParser(commandParsers);
            var commandHandler      = new CommandHandler(toyRobot, masterCommandParser);
            var commandReader       = new CommandReader(commandHandler);

            this.ToyRobotDriver = new ToyRobotDriver(textInputter, commandReader);
        }
コード例 #6
0
 /// <summary>
 /// Simulator Ctor
 /// </summary>
 /// <param name="toyRobot"></param>
 /// <param name="tableTop"></param>
 public Simulator(ToyRobot toyRobot, TableTop tableTop)
 {
     this.tr = toyRobot;
     this.tt = tableTop;
 }