Exemplo n.º 1
0
 /// <summary>
 /// Provides a deterministic way to create the MarsRovers instance.
 /// </summary>
 public static void CreateMarsRovers(bool force = false)
 {
     if (force || marsRovers == null)
     {
         marsRovers = new MarsRoverViewModel();
     }
 }
Exemplo n.º 2
0
        public IActionResult Index(MarsRoverViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ModelState.AddModelError("error", "Invalid form entry");
                    return(View(model));
                }

                var roverPositions = new List <DeployedRover>();
                var roverPosition  = new DeployedRover()
                {
                    RoverCoordinates = new Coordinates()
                    {
                        X = model.DeployedRoverX.Value,
                        Y = model.DeployedRoverY.Value
                    },
                    RoverOrientation     = model.RoverOrientation,
                    MovementInstructions = model.MovementInstructions
                };
                roverPositions.Add(roverPosition);
                var request = new MarsRoverRequest()
                {
                    MaxCoordinates = new Coordinates()
                    {
                        X = model.MaxCoordinatesX.Value,
                        Y = model.MaxCoordinatesY.Value
                    },
                    RoverPositions = roverPositions
                };
                MarsRoverResponse response = _clientService.MoverRover(request);
                if (response == null)
                {
                    ModelState.AddModelError("error", "An error occured");
                    return(View(model));
                }

                foreach (RoverFinalPosition _finalRover in response.FinalRoverPositions)
                {
                    model.FinalPosition = new FinalMarRoverPosition()
                    {
                        RoverCoordinateX = _finalRover.RoverCoordinates.X,
                        RoverCoordinateY = _finalRover.RoverCoordinates.Y,
                        RoverOrientation = _finalRover.RoverOrientation
                    };
                }
                return(View(model));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.StackTrace);
                ModelState.AddModelError("error", ex.Message);
            }
            return(View(model));
        }
Exemplo n.º 3
0
        public IActionResult MarsRover(MarsRoverViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            //Get rover count by counting the line breaks
            //since each rover has 2 lines of commands
            List <string> commandlines = vm.Command.Split('\n').ToList();

            vm.RoverCount = commandlines.Count / 2;

            //Create rovers per command
            int index = 0;

            foreach (string command in commandlines)
            {
                string tempCommand = command;

                //Create the rover
                if (index % 2 == 0)
                {
                    try
                    {
                        MarsRover marsRover = new MarsRover(index / 2);

                        //Parse rovers X coordinate and validate
                        marsRover.X = Int32.Parse(tempCommand.Substring(0, tempCommand.IndexOf(' ')));
                        tempCommand = tempCommand.Remove(0, tempCommand.IndexOf(' ') + 1);
                        if (marsRover.X > vm.GridSizeX)
                        {
                            ModelState.AddModelError("Command", "Invalid X coordinate for rover.");
                            return(View(vm));
                        }

                        //Parse rovers Y coordinate and validate
                        marsRover.Y = Int32.Parse(tempCommand.Substring(0, tempCommand.IndexOf(' ')));
                        tempCommand = tempCommand.Remove(0, tempCommand.IndexOf(' ') + 1);
                        if (marsRover.Y > vm.GridSizeY)
                        {
                            ModelState.AddModelError("Command", "Invalid Y coordinate for rover.");
                            return(View(vm));
                        }

                        //Parse rovers Direction and validate
                        marsRover.Direction = char.Parse(tempCommand.Substring(0, 1).ToUpper());
                        if (marsRover.Direction != 'N' && marsRover.Direction != 'E' && marsRover.Direction != 'S' && marsRover.Direction != 'W')
                        {
                            ModelState.AddModelError("Command", "Invalid direction for rover.");
                            return(View(vm));
                        }
                        vm.MarsRovers.Add(marsRover);
                    }
                    catch
                    {
                        ModelState.AddModelError("Command", "Oops! Something in your rover initialization(s) wasn't recognized. Please review and try again.");
                        return(View(vm));
                    }
                }
                //Send rovers movement commands
                else
                {
                    while (tempCommand.Length > 0)
                    {
                        char commandLetter;
                        switch (commandLetter = char.Parse(tempCommand.Substring(0, 1).ToUpper()))
                        {
                        case 'R':
                            vm.MarsRovers.LastOrDefault().rotateRight();
                            break;

                        case 'L':
                            vm.MarsRovers.LastOrDefault().rotateLeft();
                            break;

                        case 'M':
                            vm.MarsRovers.LastOrDefault().moveForward();
                            if (vm.MarsRovers.LastOrDefault().X > vm.GridSizeX || vm.MarsRovers.LastOrDefault().Y > vm.GridSizeY)
                            {
                                ModelState.AddModelError("Command", "Oops! One of your rovers went off Grid! Please review and try again.");
                                vm.MarsRovers.Clear();
                                return(View(vm));
                            }
                            break;

                        case '\r':
                            break;

                        default:
                            ModelState.AddModelError("Command", "Oops! Something in your rover command(s) wasn't recognized. Please review and try again.");
                            vm.MarsRovers.Clear();
                            return(View(vm));
                        }
                        tempCommand = tempCommand.Remove(0, 1);
                    }
                }
                index++;
            }
            return(View("MarsRover", vm));
        }
Exemplo n.º 4
0
        public IActionResult AddMarsRover(MarsRover rover)
        {
            MarsRoverViewModel vm = new MarsRoverViewModel(rover);

            return(View("MarsRover", vm));
        }
Exemplo n.º 5
0
 /// <summary>
 /// Provides a deterministic way to delete the MarsRovers instance.
 /// </summary>
 public static void ClearMarsRovers()
 {
     marsRovers.Cleanup();
     marsRovers = null;
 }