示例#1
0
            // find a path to a point in an unknown environment. Then find the shortest path
            // from the origin to that point.
            public int Run(string input = Input)
            {
                var memory = input.Split(',').Select(int.Parse);
                var cpu    = new IntCodeStateMachine(memory);

                //ICommandInput commandInput = new ConsoleInput();
                ICommandInput commandInput = new AiInput(cpu);
                var           repairDroid  = new RepairDroid(cpu, commandInput);

                var consoleView = new ConsoleView((40, 40));
                var rcp         = new RemoteControlProgram(repairDroid, commandInput, consoleView);

                var finder = new RemoteDroidPathFinder(repairDroid, rcp);

                int shortestPath = -1;

                finder.ShortestPathFound += (_, steps) =>
                {
                    shortestPath = steps;
                    rcp.Stop();
                };

                rcp.FoundOxygenSystem += (_, coord) =>
                {
                    Console.SetCursorPosition(0, 2);
                    Console.WriteLine("Found oxygen tank at {0}. Navigating back to origin.", coord);
                };

                Console.WriteLine("1=N, 2=S, 3=W, 4=E");

                rcp.Run();

                return(shortestPath);
            }
        public RemoteControlProgram(RepairDroid repairDroid, ICommandInput commandInput, IView consoleView)
        {
            _relativePosition = Coordinate.Origin;
            _lastPosition     = _relativePosition;

            _movementCommand            = (MovementCommand)(-1);
            commandInput.InputReceived += (_, v) => _movementCommand = (MovementCommand)v;

            repairDroid.OnReply += (_, msg) =>
            {
                switch (msg)
                {
                case RepairDroidStatusCode.HitWall:
                    consoleView.Write(GetNewLocation(_relativePosition), '#');

                    break;

                case RepairDroidStatusCode.Moved:
                    consoleView.Write(_relativePosition, '.');
                    Move();
                    consoleView.Write(_relativePosition, 'D');
                    break;

                case RepairDroidStatusCode.MovedAndFoundOxygenSystem:
                    Move();
                    FoundOxygenSystem?.Invoke(this, _relativePosition);
                    _run = false;
                    break;
                }
                ;
            };
            _repairDroid  = repairDroid;
            _commandInput = commandInput;
        }