コード例 #1
0
        public string Simulate()
        {
            IMovable currMovable          = null;
            var      movableIdIncrementer = 1;

            while (DataFromMissionControl.Any())
            {
                var instruction = DataFromMissionControl.Pop();
                if (Parser.IsMovable(instruction))
                {
                    try
                    {
                        var tmp = Factory.Rover().Init(movableIdIncrementer++, instruction);
                        Movables.Add(tmp);
                        currMovable = tmp;
                        Logger.Debug("Deploying Rover: " + tmp.ToString());
                        Logger.Debug(AreaToExplore.ToString());
                    }
                    catch
                    {
                        currMovable = null; //ensure that subsequent movement commands are aborted
                    }
                }
                else if (currMovable != null)
                {
                    currMovable.Move(instruction);
                }
                else
                {
                    Logger.Error("Unrecognized Instruction: " + instruction);
                }
            }
            return(AreaToExplore.ToString());
        }
コード例 #2
0
        public MarsSimulator Init(string inputFromNASA)
        {
            Movables = new List <IMovable>();

            var data = inputFromNASA.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).Reverse();

            DataFromMissionControl = new Stack <string>(data);

            var gridBoundaries = DataFromMissionControl.Pop();

            if (!Parser.IsEnvironmentBoundary(gridBoundaries))
            {
                var error = "Error Invalid Environment Boundaries: " + gridBoundaries;
                Logger.Fatal(error);
                throw new Exception(error);
            }

            AreaToExplore.Init(gridBoundaries);
            return(this);
        }