public static RoverPositionModel RoverPosition(string roverPosition) { var roverPositionArray = roverPosition.Split(" "); if (roverPositionArray.Length == 3) //checker { string position = roverPositionArray[2].ToUpper(); try { RoverPositionModel roverModel = new RoverPositionModel() { PositionX = int.Parse(roverPositionArray[0]), PositionY = int.Parse(roverPositionArray[1]), Direction = (DirectionEnum)Enum.Parse(typeof(DirectionEnum), position) }; return(roverModel); } catch (Exception) { return(null); } } return(null); }
public RoverPositionModel Move(RoverPositionModel roverPosition) { if (roverPosition.Orientation == Orientations.N) { roverPosition.Y++; } else if (roverPosition.Orientation == Orientations.E) { roverPosition.X++; } else if (roverPosition.Orientation == Orientations.S) { roverPosition.Y--; } else if (roverPosition.Orientation == Orientations.W) { roverPosition.X--; } return(new RoverPositionModel { X = roverPosition.X, Y = roverPosition.Y, Orientation = roverPosition.Orientation }); }
private static bool IsValid(RoverPositionModel roverPositionModel, PlateauModel plateauModel) { var isValidX = roverPositionModel.X >= 0 && roverPositionModel.X <= plateauModel.Width; var isValidY = roverPositionModel.Y >= 0 && roverPositionModel.Y <= plateauModel.Height; return(isValidX && isValidY); }
public void AddMarsRover_Should_Add_Mars_Rover_Plateau_When_Second_Expected_Value() { //Arrange var plateauService = new PlateauService(_loggerMock); plateauService.Create(6, 6); plateauService.AddMarsRover(new RoverPositionModel { Direction = Direction.East, X = 5, Y = 3 }); var roverPositionModel = new RoverPositionModel { Direction = Direction.East, X = 1, Y = 2 }; //Act plateauService.AddMarsRover(roverPositionModel); //Assert plateauService.GetMarsRovers().Count.Should().Be(2); plateauService.GetMarsRovers().Last().X.Should().Be(roverPositionModel.X); plateauService.GetMarsRovers().Last().Y.Should().Be(roverPositionModel.Y); plateauService.GetMarsRovers().Last().Direction.Should().Be(roverPositionModel.Direction); }
public void CreateMarsRover_Should_Throw_InvalidPosition_When_Negative_Or_Extreme(int x, int y, Direction direction) { //Arrange var plateauService = new Mock <IPlateauService>(); plateauService.Setup(foo => foo.GetCurrentPlateau()).Returns(new PlateauModel { Width = 5, Height = 5 }); var roverPositionModel = new RoverPositionModel { X = x, Y = y, Direction = direction }; var marsRoverService = new MarsRoverService(_loggerMock); //Act void Action() { marsRoverService.CreateMarsRover(roverPositionModel, plateauService.Object); } //Assert Assert.Throws <ValidateMarsRoverPositionException>((Action)Action); }
public void ChangePosition_Should_Change_Position_When_Expected_Position() { //Arrange var plateauService = new Mock <IPlateauService>(); plateauService.Setup(foo => foo.GetCurrentPlateau()).Returns(new PlateauModel { Width = 5, Height = 5 }); var roverPositionModel = new RoverPositionModel { X = 2, Y = 4, Direction = Direction.East }; //Act var marsRoverService = new MarsRoverService(_loggerMock); marsRoverService.ChangePosition(roverPositionModel); var currentRoverPositionModel = marsRoverService.GetCurrentRover(); //Assert currentRoverPositionModel.X.Should().Be(roverPositionModel.X); currentRoverPositionModel.Y.Should().Be(roverPositionModel.Y); currentRoverPositionModel.Direction.Should().Be(roverPositionModel.Direction); }
public bool IsRoverInsidePlateau(RoverPositionModel roverPosition, PlateauModel plateau) { bool isInside = false; if (roverPosition.X < plateau.X || roverPosition.Y < plateau.Y) { isInside = true; } return(isInside); }
public void Setup() { //Given _service = new RotateEastService(); _roverPositionModel = new RoverPositionModel { Direction = Direction.North, X = 1, Y = 1 }; }
public RightCommandTest() { //Given _roverService = new Mock <IRoverService>(); _directionService = new Mock <IDirectionService>(); _roverPositionModel = new RoverPositionModel { Direction = Direction.North, X = 1, Y = 1 }; }
public PlateauServiceTest() { //Given _loggerMock = new Mock <ILogger <PlateauService> >(); _plateauService = new PlateauService(_loggerMock.Object); _roverPositionModel = new RoverPositionModel { Direction = Direction.North, X = 1, Y = 1 }; }
public CreateRoverCommandTest() { //Given _plateauService = new Mock <IPlateauService>(); _roverService = new Mock <IRoverService>(); _roverPositionModel = new RoverPositionModel { Direction = Direction.North, X = 1, Y = 1 }; }
public void MoveForward_Should_Move_Forward_Method_When_Expected_Value(int x, int y, int newX, int newY) { //Arrange var roverPositionModel = new RoverPositionModel { X = x, Y = y, Direction = Direction.East }; var eastStrategy = new EastStrategy(); //Act var result = eastStrategy.MoveForward(roverPositionModel); //Assert result.X.Should().Be(newX); result.Y.Should().Be(newY); }
public void Setup() { //Given _service = new DirectionService(); _roverPositionModel = new RoverPositionModel { Direction = Direction.North, X = 1, Y = 1 }; _directionService = new Dictionary <Direction, IRotateService> { { Direction.North, new RotateNorthService() }, { Direction.South, new RotateSouthService() }, { Direction.West, new RotateWestService() }, { Direction.East, new RotateEastService() } }; }
public ActionResult MoveRover(string position, string plateau, string commands) { position = position.ToUpper(); commands = commands.ToUpper(); RoverPositionModel finalPosition = new RoverPositionModel { X = Convert.ToInt32(position.Substring(0, 1)), Y = Convert.ToInt32(position.Substring(1, 1)), Orientation = position.Substring(2, 1) == "N" ? Orientations.N : position.Substring(2, 1) == "E" ? Orientations.E : position.Substring(2, 1) == "S" ? Orientations.S : Orientations.W }; PlateauModel plateauModel = new PlateauModel { X = Convert.ToInt32(plateau.Substring(0, 1)), Y = Convert.ToInt32(plateau.Substring(1, 1)) }; foreach (var command in commands) { switch (command) { case ('L'): finalPosition.Orientation = Rover.TurnLeft(finalPosition.Orientation); break; case ('R'): finalPosition.Orientation = Rover.TurnRight(finalPosition.Orientation); break; case ('M'): finalPosition = Rover.Move(finalPosition); break; default: throw new ArgumentException(string.Format("Invalid value: {0}", command)); } } if (Rover.IsRoverInsidePlateau(finalPosition, plateauModel)) { return(View("Result", finalPosition)); } ViewBag.Error = "Rover is out of plateau boundaries"; return(View("OutOfBoundaries")); }
public void CreateMarsRover(RoverPositionModel roverPositionModel, IPlateauService plateauService) { var plateauModel = plateauService.GetCurrentPlateau(); if (!IsValid(roverPositionModel, plateauModel)) { var exception = new ValidateMarsRoverPositionException(); _logger.LogError(exception.Message); throw exception; } _roverPositionModel = new RoverPositionModel { X = roverPositionModel.X, Y = roverPositionModel.Y, Direction = roverPositionModel.Direction }; plateauService.AddMarsRover(_roverPositionModel); _logger.LogInformation($"Created Mars Rover {_roverPositionModel.X}x{_roverPositionModel.Y} - {_roverPositionModel.Direction}."); }
public RoverPositionModel MoveForward(RoverPositionModel roverPositionModel) { roverPositionModel.Y += 1; return(roverPositionModel); }
public void AddMarsRover(RoverPositionModel roverPositionModel) { _roverPositionModels.Add(roverPositionModel); }
public RoverPositionModel Move(RoverPositionModel roverPositionModel) { return(_directionService[roverPositionModel.Direction].Move(roverPositionModel)); }
public RoverPositionModel Move(RoverPositionModel roverPositionModel) { roverPositionModel.Y -= 1; return(roverPositionModel); }
public RoverPositionModel MoveForward(RoverPositionModel roverPositionModel) { return(_directionStrategies[roverPositionModel.Direction].MoveForward(roverPositionModel)); }
public void ChangePosition(RoverPositionModel roverPositionModel) { _roverPositionModel = roverPositionModel; _logger.LogInformation($"Rover Position is {_roverPositionModel.X}x{_roverPositionModel.Y} - {_roverPositionModel.Direction}."); }