Ejemplo n.º 1
0
        public IActionResult MoveRover(RoverViewModel roverViewModel)
        {
            var _roverMovement = new RoverMovement(roverViewModel);

            try
            {
                _roverMovement.ValidatePlateau();
            }
            catch (Exception ex)
            {
                roverViewModel.Error = ex.Message;
                return(View("Index", roverViewModel));
            }

            try
            {
                _roverMovement.Navigate(roverViewModel.Rover1);
            }
            catch (Exception ex)
            {
                roverViewModel.Error = string.Concat("Rover 1: ", ex.Message);
                return(View("Index", roverViewModel));
            }

            try
            {
                _roverMovement.Navigate(roverViewModel.Rover2);
            }
            catch (Exception ex)
            {
                roverViewModel.Error = string.Concat("Rover 2: ", ex.Message);
            }

            return(View("Index", roverViewModel));
        }
Ejemplo n.º 2
0
        public RoverViewModel Process(RoverViewModel roverModel, string instructions)
        {
            foreach (var instruction in instructions)
            {
                switch (instruction)
                {
                case ('L'):
                case ('l'):
                    TurnLeft(roverModel);
                    break;

                case ('R'):
                case ('r'):
                    TurnRight(roverModel);
                    break;

                case ('M'):
                case ('m'):
                    Move(roverModel);
                    break;

                default:
                    throw new Exception($"Invalid command - value : {instruction}");
                }
            }

            return(roverModel);
        }
Ejemplo n.º 3
0
        public void Initialize()
        {
            IVehicle       rover          = new Rover(0, 0, Direction.North);
            RoverViewModel roverViewModel = new RoverViewModel(rover);

            var topViewController = Window.RootViewController as RoverViewController;

            topViewController.RoverViewModel = roverViewModel;
        }
Ejemplo n.º 4
0
        public ActionResult Index(RoverViewModel roverVM)
        {
            if (ModelState.IsValid)
            {
                TempData["RoverData"] = JsonConvert.SerializeObject(roverVM);
                return(RedirectToAction("Index", "Results"));
            }

            return(View(roverVM));
        }
Ejemplo n.º 5
0
        public IActionResult Create([FromBody] RoverModel model)
        {
            if (!_roverCaching.IsItemExists(model.Id))
            {
                var roverViewModel = new RoverViewModel
                {
                    Id   = model.Id,
                    Name = model.Name
                };
                _roverCaching.SetItem(model.Id, roverViewModel);
            }

            return(CreatedAtAction(nameof(Create), new { id = model.Id }, model));
        }
Ejemplo n.º 6
0
        public IActionResult Index()
        {
            List <Rover> rovers = new List <Rover>();

            for (int i = 0; i < _roverNumber; i++)
            {
                rovers.Add(new Rover());
            }

            RoverViewModel rvm = new RoverViewModel()
            {
                Rovers = rovers
            };

            return(View(rvm));
        }
Ejemplo n.º 7
0
        public void InitializeRover()
        {
            _roverModel = new RoverViewModel();

            _roverModel.PlateauSizeLatitude  = 5;
            _roverModel.PlateauSizeLongitude = 5;

            _roverModel.Rover1 = new Models.Rover()
            {
                Position = new Position(),
            };

            _roverModel.Rover2 = new Models.Rover()
            {
                Position = new Position(),
            };
        }
Ejemplo n.º 8
0
        public IActionResult Index()
        {
            RoverViewModel rvm = JsonConvert.DeserializeObject <RoverViewModel>(TempData["RoverData"].ToString());

            TempData["RoverData"] = JsonConvert.SerializeObject(rvm);

            InstructionHandler instructionHandler = new InstructionHandler();

            ResultsViewModel results = new ResultsViewModel()
            {
                Grid    = rvm.Grid,
                Rovers  = rvm.Rovers,
                Results = instructionHandler.GetUpdatedPosition(rvm)
            };

            return(View(results));
        }
Ejemplo n.º 9
0
        public List <string> GetUpdatedPosition(RoverViewModel rvm)
        {
            List <string> results = new List <string>();

            _xBoundry = rvm.Grid.X;
            _yBoundry = rvm.Grid.Y;

            foreach (Rover rover in rvm.Rovers)
            {
                StringBuilder curPos = new StringBuilder(rover.InitialPosition);

                _curX           = curPos[0] - '0';
                _curY           = curPos[1] - '0';
                _curOrientation = Array.FindIndex(Cardinals, row => row.Contains(char.ToUpper(curPos[2])));

                string instructions = rover.Instructions;

                for (int i = 0; i < instructions.Length; i++)
                {
                    switch (char.ToUpper(instructions[i]))
                    {
                    case 'R':
                        OrientRight();
                        break;

                    case 'L':
                        OrientLeft();
                        break;

                    case 'M':
                        Move();
                        break;

                    default:
                        Console.WriteLine("Invalid Character in Rover Instruction string");
                        break;
                    }
                }

                string result = _curX.ToString() + _curY.ToString() + Cardinals[_curOrientation];
                results.Add(result);
            }

            return(results);
        }
Ejemplo n.º 10
0
        private void Move(RoverViewModel roverModel)
        {
            switch (roverModel.CurrentDirection)
            {
            case Direction.N:
                roverModel.CurrentY++;
                break;

            case Direction.S:
                roverModel.CurrentY--;
                break;

            case Direction.E:
                roverModel.CurrentX++;
                break;

            case Direction.W:
                roverModel.CurrentX--;
                break;
            }
        }
Ejemplo n.º 11
0
        private void TurnRight(RoverViewModel roverModel)
        {
            switch (roverModel.CurrentDirection)
            {
            case Direction.N:
                roverModel.CurrentDirection = Direction.E;
                break;

            case Direction.S:
                roverModel.CurrentDirection = Direction.W;
                break;

            case Direction.E:
                roverModel.CurrentDirection = Direction.S;
                break;

            case Direction.W:
                roverModel.CurrentDirection = Direction.N;
                break;
            }
        }
Ejemplo n.º 12
0
        public void Setup()
        {
            var roverViewModel = new RoverViewModel
            {
                Id               = 1,
                Name             = "Rover-1",
                CurrentDirection = "N",
                CurrentY         = 0,
                CurrentX         = 0
            };

            _mockRover.Setup(x => x.Process(It.IsAny <RoverViewModel>(), It.IsAny <string>())).Returns(roverViewModel);
            _mockRoverCaching.Setup(x => x.GetItem(It.IsAny <object>())).Returns(roverViewModel);
            _mockRoverCaching.Setup(x => x.IsItemExists(It.IsAny <object>())).Returns(true);
            _roverController = new RoverController(_mockRoverCaching.Object, _mockRover.Object);
            roverModel       = new RoverModel
            {
                Id   = 1,
                Name = "Rover-1",
                MovementInstruction = "M"
            };
        }
Ejemplo n.º 13
0
        public void Process()
        {
            var expected = new RoverViewModel
            {
                Id               = 1,
                Name             = "Rover-1",
                CurrentDirection = "N",
                CurrentY         = 1,
                CurrentX         = 0
            };
            var roverViewModel = new RoverViewModel
            {
                Id               = 1,
                Name             = "Rover-1",
                CurrentDirection = "N",
                CurrentY         = 0,
                CurrentX         = 0
            };
            var actual = this._rover.Process(roverViewModel, "M");

            Assert.IsNotNull(actual);
            Assert.AreEqual(actual.CurrentY, expected.CurrentY);
        }
Ejemplo n.º 14
0
 public RoverMovement(RoverViewModel roverModel)
 {
     _roverModel = roverModel;
 }