public RoverBusinessLogic(IInputParser inputParser) { this.m_inputParser = inputParser; this.m_Rover = inputParser.Rover; this.m_nasaPlatue = inputParser.Platue; this.m_instructionList = inputParser.MovementInstructions; }
public void Move(IRover rover, int places) { switch (rover.Direction) { case Direction.North: rover.Direction = Direction.East; rover.MoveEast(places); break; case Direction.East: rover.Direction = Direction.South; rover.MoveSouth(places); break; case Direction.South: rover.Direction = Direction.West; rover.MoveWest(places); break; case Direction.West: rover.Direction = Direction.North; rover.MoveNorth(places); break; } }
public void Run(IRover rover) { var newDirection = new Direction(); switch (rover.RoverLocation.Direction) { case Direction.East: { newDirection = Direction.South; break; } case Direction.South: { newDirection = Direction.West; break; } case Direction.West: { newDirection = Direction.North; break; } case Direction.North: { newDirection = Direction.East; break; } } RoverLocation newLocation = RoverLocation.Set(rover.RoverLocation.X, rover.RoverLocation.Y, newDirection); rover.Move(newLocation); }
public void Move(IRover rover) { if (rover.GetPlateau().IsValidYCoordinate(rover.GetRoverPosition().Y - 1)) { rover.GetRoverPosition().Y--; } }
/// <summary> /// Returns the rover position and heading in string format /// </summary> /// <param name="rover"></param> /// <returns></returns> private string GetRoverPositionAndHeadingString(IRover rover) { var position = _roverService.GetRoverPosition(rover); var heading = _roverService.GetRoverHeading(rover); return($"{position.X} {position.Y} {heading.ToString().First()}"); }
// Application starting point public void Run() { //Plateau grid creation DividePlateauInGrid(); //Rover 1 //Setting initial position Console.WriteLine(); Console.WriteLine("~~~~~~~~~~~~~~~ Rover 1 ~~~~~~~~~~~~~~~"); rover1 = SetRoverPosition(); //Getting and processing instructions ProcessInstructions(rover1); //Rover 2 //Setting initial position Console.WriteLine(); Console.WriteLine("~~~~~~~~~~~~~~~ Rover 2 ~~~~~~~~~~~~~~~"); rover2 = SetRoverPosition(); //Getting and processing instructions ProcessInstructions(rover2); //Results Console.WriteLine(); Console.WriteLine("~~~~~~~~~~~~~~~ Final Rover Positions ~~~~~~~~~~~~~~~"); Console.WriteLine("Rover 1: " + rover1.GetRoverPositionInStringFormat()); Console.WriteLine("Rover 2: " + rover2.GetRoverPositionInStringFormat()); Console.ReadLine(); }
private static void ensureRoverIsDeployed(IRover rover) { if(!rover.IsDeployed()) { throw new ReportException("Cannot create report because one or more rovers are not deployed"); } }
/// <summary> /// This method accepts user instructions, checks validity of user instructions and moves the rover according to them /// </summary> /// <param name="rover"></param> /// <returns></returns> private void ProcessInstructions(IRover rover) { //Get user input Console.WriteLine(); Console.WriteLine("ENTER INSTRUCTIONS FOR THE ROVER TO MOVE ON THE PLATEAU:"); Console.WriteLine("(Note: Use L to turn left, R to turn right, M to move one position ahead in the direction of facing)"); string enteredInstructions = Console.ReadLine().ToUpper(); //Check format InputValidator validator = new InputValidator(); bool isValid = validator.IsInstructionsFormatValid(enteredInstructions); while (!isValid) { Console.WriteLine(); Console.WriteLine("ERROR! Please enter in valid format:"); Console.WriteLine("(Valid values: series of characters containing only L,R and M)"); enteredInstructions = Console.ReadLine(); isValid = validator.IsInstructionsFormatValid(enteredInstructions); } try { //Process instructions rover.ProcessInstructions(enteredInstructions); } catch (RoverOutOfRangeException ex) { Console.WriteLine("Operation aborted!! Rover is at the edge, can not move ahead."); } }
public void Test_Scenario_Max55_33E_MRRMMRMRRM() { ServiceProvider serviceProvider = new ServiceCollection() .AddTransient <IRover, Rover>() .BuildServiceProvider(); var maxLimits = new List <int>() { 5, 5 }; Coordinate coordinate = new Coordinate { Direction = Directions.E, X = 3, Y = 3 }; var moves = "MRRMMRMRRM"; IRover _rover = serviceProvider.GetService <IRover>(); var output = _rover.Move(maxLimits, moves, coordinate); var actual = $"{output.X} {output.Y} {output.Direction}"; var expected = "2 3 S"; Assert.AreEqual(expected, actual); }
public static void Handle(string command, IRover rover) { for (int i = 0; i < command.Length; i++) { HandlerFactory.GetHandler(command[i]).Handle(command[i], rover); } }
private static void ensureRoverIsDeployed(IRover rover) { if (!rover.IsDeployed()) { throw new ReportException("Cannot create report because one or more rovers are not deployed"); } }
static void Main(string[] args) { string inputStr = GenerateInput(); Input input = InputManager.GetInput(inputStr); IKernel kernal = new StandardKernel(); kernal.Load(Assembly.GetExecutingAssembly()); IPlateau plateau = kernal.Get <IPlateau>(); plateau.SetPoint(new Point(input.X, input.Y)); foreach (var inputRover in input.InputRovers) { IRover rover = kernal.Get <Rover>(); rover.SetRover(plateau, new Point(inputRover.X, inputRover.Y), inputRover.CurrentDirection); rover.SetCommandParams(inputRover.Commands); RoverInvoker roverInvorker = new RoverInvoker(rover); roverInvorker.Execute(); //Output Console.WriteLine(rover.ToString()); } Console.ReadKey(); }
public void Move(IRover rover) { if (rover.GetPlateau().IsValidXCoordinate(rover.GetRoverPosition().X + 1)) { rover.GetRoverPosition().X++; } }
public RoverTest() { var services = IoCBuilder.ConfigureServices(); var serviceProvider = services.BuildServiceProvider(); rover = serviceProvider.GetService <IRover>(); }
public void PrepareRover(RoverModel model) { if (model.ErrorTracer == null) { _rover = Rover.CreateRover(this); _rover.X = model.X; _rover.Y = model.Y; _rover.Direction = EnumConvertor.ConvertCharToDirection(model); _rover.Plateau = _plateaus; if (model.X < 0 || model.X < 0) { _rover.ErrorMessage = MessageConstant.RoverPlateauCoordinateFail; } if (model.Plateau.UpperRight < model.X || model.Plateau.LowerLeft < model.X) { _rover.ErrorMessage = MessageConstant.RoverPlateauCoordinateFail; } if (model.Plateau.UpperRight < model.Y || model.Plateau.LowerLeft < model.Y) { _rover.ErrorMessage = MessageConstant.RoverPlateauCoordinateFail; } } }
public void Get_Rover_Location_Without_Commands() { result = new Result(); rover = new Rover(new Position(2, 2), Direction.N); roverService = new RoverService(rover, new Plateau(5, 5, new Result()), new Result()); Assert.AreEqual(roverService.GetCurrentLocation(), "2 2 N"); }
static void Main(string[] args) { try { ServiceProvider serviceProvider = new ServiceCollection() .AddTransient <IRover, Rover>() .BuildServiceProvider(); Input input = new Input(); SetInputsFromConsole(input); List <int> maxLimits = new List <int>() { input.MaxX, input.MaxY }; Coordinate coordinate = new Coordinate { X = input.StartX, Y = input.StartY, Direction = (Directions)Enum.Parse(typeof(Directions), input.StartDirection) }; IRover _rover = serviceProvider.GetService <IRover>(); _rover.Move(maxLimits, input.Moves, coordinate); Console.WriteLine($"{coordinate.X} {coordinate.Y} {coordinate.Direction}"); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public int[] EastFacingRoverMoveMethodSubtractsAndAddsToCoordinatesCorrectly(Type roverType, int startX, int startY, char movement) { IRover rover = (IRover)Activator.CreateInstance(roverType, startX, startY); rover = rover.Move(movement); return(new int[] { rover.X, rover.Y }); }
private static void EnsureRoverIsCreated(IRover rover) { if (!rover.CheckCreateStatus()) { throw new RoverNotCreatedException("Cannot create report because one or more rovers are not created"); } }
public async Task <IRover> UpdateAsync(IRover rover) { _context.MarsRovers.Update((MarsRover)rover); await _context.SaveChangesAsync(); return(await GetAsync(rover.RoverId)); }
public void Create_Rover_Should_Throw_Exception() { result = new Result(); rover = new Rover(new Position(6, 2), Direction.N); roverService = new RoverService(rover, new Plateau(5, 5, new Result()), result); Assert.AreEqual(result.IsSuccess, false); }
public Direction TurnRight(IRover rover) { var direction = rover.Direction; switch (rover.Direction) { case Direction.North: direction = Direction.East; break; case Direction.East: direction = Direction.South; break; case Direction.South: direction = Direction.West; break; case Direction.West: direction = Direction.North; break; } return(direction); }
public static void MoveRover(IRover rover, IGrid grid, List <ICommand> commands) { Console.WriteLine("Starting rover position " + rover.Position.X.ToString() + " " + rover.Position.Y.ToString() + " " + "starting rover direction " + rover.Direction.ToString()); foreach (Command c in commands) { if (rover.Direction == 'N')//CONSIDER TO UPPER AND TO LOWER CASE IN THE CHAR { c.MoveFromNordDirection(rover, grid); } else if (rover.Direction == 'S') { c.MoveFromSudDirection(rover, grid); } else if (rover.Direction == 'O') { c.MoveFromOvestDirection(rover, grid); } else if (rover.Direction == 'E') { c.MoveFromEstDirection(rover, grid); } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("New rover position " + rover.Position.X.ToString() + " " + rover.Position.Y.ToString() + " new rover direction: " + rover.Direction.ToString()); Console.ResetColor(); } }
public void Create_Rover_Get_Success() { result = new Result(); rover = new Rover(new Position(2, 2), Direction.N); roverService = new RoverService(rover, new Plateau(5, 5, new Result()), result); Assert.AreEqual(result.IsSuccess, true); }
public Point Move(IRover rover) { var x = rover.Point.X; var y = rover.Point.Y; switch (rover.Direction) { case Direction.North: y++; break; case Direction.East: x++; break; case Direction.South: y--; break; case Direction.West: x--; break; } return(Points.FirstOrDefault(p => p.X == x && p.Y == y) ?? throw new Exception("Rover outside the plateau!")); }
public void Move(IRover rover) { var commands = rover.Commands.ToCharArray(); foreach (var command in commands) { switch (command) { case 'L': rover.TurnLeft(); break; case 'R': rover.TurnRight(); break; case 'M': if (CheckBoundaries(rover)) { rover.MoveForward(); } break; default: throw new CommandException(); } } }
/// <inheritdoc /> public override void UpdateGridPosition(IRover rover, Point previousCoordinates) { if (rover == null) { throw new ArgumentNullException(nameof(rover)); } if (previousCoordinates == null) { throw new ArgumentNullException(nameof(previousCoordinates)); } // No changes need to occur if the new coordinates are the same as the previous ones if (rover.Coordinates.Equals(previousCoordinates)) { return; } // Check that the coordinates are within the range of this planet if (rover.Coordinates.X < 0 || rover.Coordinates.X >= this.Width || rover.Coordinates.Y < 0 || rover.Coordinates.Y >= this.Height) { throw new ArgumentException($"Rover with coordinates: {rover.Coordinates.ToString()} is out of the grid dimensions"); } // Check if there is a conflict in positions if (this.Grid[rover.Coordinates]) { throw new InvalidOperationException($"A rover already exists at coordinates: {rover.Coordinates.ToString()}"); } this.Grid[previousCoordinates] = false; this.Grid[rover.Coordinates] = true; }
public Position Run(IRover rover) { Position position = new Position(); position = rover.Position; switch (rover.Position.Direction) { case Surface.Directions.E: position.Coordinate.CoordinateX++; break; case Surface.Directions.N: position.Coordinate.CoordinateY++; break; case Surface.Directions.S: position.Coordinate.CoordinateY--; break; case Surface.Directions.W: position.Coordinate.CoordinateX--; break; } // we are checking new position is valid for surface if (rover.Surface != null && rover.Surface.isValidCoordinate(position.Coordinate)) { return(position); } else { // We don't change rover position. return(rover.Position); } }
/// <summary> /// Move the rover according to instructions, verifying the move is within the grid boundaries /// </summary> /// <param name="rover"></param> /// <param name="roverInstructions"></param> /// <returns></returns> private string ProcessRoverInstruction(IRover rover, string roverInstructions) { foreach (var move in roverInstructions.ToUpperInvariant()) { switch (move) { case 'L': _roverService.RotateRoverLeft(rover); break; case 'R': _roverService.RotateRoverRight(rover); break; case 'M': if (IsMovingWithinBoundaries(rover)) { _roverService.MoveRoverForward(rover); } else { throw new OutOfBoundariesException($"Rover stepping out of the grid."); } break; default: // todo:? break; } } return(GetRoverPositionAndHeadingString(rover)); }
/// <summary> /// Places rover on plateau and process instructions /// </summary> /// <param name="rover"></param> /// <param name="instructions"></param> public void ProcessRover(IRover rover) { rover.OnRoverMoved += new RoverMoveEventHandler(rover_OnRoverMoved); this.Rovers.Add(rover); CheckPositions(rover); rover.Instruction.Execute(rover); }
public void Move(IRover rover) { char[] command = rover.Commands.ToCharArray(); foreach (var singleCommand in command) { switch (singleCommand) { case 'L': rover.TurnLeft(); break; case 'R': rover.TurnRight(); break; case 'M': StepForwardRover(rover); break; default: throw new CommandException(); } } }
public void EastFacingRoverReturnsCorrectDirectionalRoverAfterTurning(Type startType, char direction, Type endType) { IRover rover = (IRover)Activator.CreateInstance(startType, 0, 0); var rover2 = rover.Move(direction); Assert.IsInstanceOf(endType, rover2); }
private string ParseInstruct(char[] t, IRover rover) { foreach (var c in t) { switch (c) { case 'M': if (!CanMove(rover)) { return("invalid instructions:"); } rover.Move(); break; case 'L': rover.TurnLeft(); break; case 'R': rover.TurnRight(); break; } } return(""); }
private IRover MapCoordinates(IRover rover) { Type roverType = rover.GetType(); return (IRover)Activator.CreateInstance( roverType, Planet.ConvertXCoordinate(rover.X), Planet.ConvertYCoordinate(rover.Y)); }
public void ShouldBeInLimitCo_OrdinatesFromInput() { var rover = TestableRover.Create(); IPosition position = new RoverPosition(10, 10); rover.PrompterMock.Setup(x => x.IsValid()).Returns(true); _firstRover = new Rover(position, rover.PrompterMock.Object, DirectionType.N); Assert.IsFalse(_firstRover.IsValid()); }
/// <summary>Performs Move action on a rover.</summary> /// <param name="rover">The rover to use.</param> public void Execute(IRover rover) { var success = rover.Move(true); if (!success) { throw new Exception("Rover cannot move forward."); } }
public void Go_Should_Kill_Rover_If_It_Goes_Off_South_Edge_Plateau() { sut = new Rover(10, 0, "S", "M", 10, 10); sut.Go(); string result = sut.CurrentPosition(); Assert.AreEqual("DEAD", result); }
/* When inserting a Rover in the Plateau you need to insert the Position to avoid the Rover to * start in a invalid position in the Plateau. * Because of that I did not a overload method where you only insert the rover without the Position. */ public void InsertRover(IRover rover, Position roverPosition) { if (IsValidPosition(roverPosition)) rover.Position = roverPosition; else throw new Exception("Invalid coordinates."); Rovers.Add(rover); }
public void CheckPositions(IRover rover) { foreach (IPositionCheck positionCheck in this._positionChecks) { positionCheck.MovedRover = rover; positionCheck.ExistingRovers = this._rovers; positionCheck.Plateau = this._plateau; positionCheck.Check(); } }
public void ShouldBeInLimitCo_OrdinatesFromOutput() { var rover = TestableRover.Create(); IPosition position = new RoverPosition(1, 2); rover.PrompterMock.SetupProperty(x => x.Command, "MMMMMMMMMMMMMM"); rover.PrompterMock.Setup(x => x.IsValid()).Returns(true); _firstRover = new Rover(position, rover.PrompterMock.Object, DirectionType.N); _firstRover.Execute(); }
public IRover AddRover(IRover rover) { lock (dictLock) { rover.Id = Guid.NewGuid(); rovers.Add(rover.Id, rover); } Save(); return rover; }
public void ShouldBeValidEachLetter() { var rover = TestableRover.Create(); rover.PositionMock.SetupProperty(x => x.X, 1); rover.PositionMock.SetupProperty(x => x.Y, 2); rover.PositionMock.Setup(x => x.IsValid()).Returns(true); IPrompter prompter = new Prompter("KLMLMLMLM"); _firstRover = new Rover(rover.PositionMock.Object, prompter, DirectionType.N); Assert.IsFalse(_firstRover.IsValid()); }
public override void Given() { this.StubbedRover = stub_a<IRover>(); this.StubbedPlateau = stub_a<IPlateau>(); this.StubbedInstruction = stub_a<IInstruction>(); this.StubbedPositionChecks = stub_a<List<IPositionCheck>>(); this.PlateauController = new PlateauController(this.StubbedPlateau); this.PlateauController.PositionChecks = this.StubbedPositionChecks; this.StubbedRover.Instruction = this.StubbedInstruction; }
public bool MoveRover(char action) { var newRover = MapCoordinates(InternalRover.Move(action)); if (Planet.CanMoveTo(newRover.X, newRover.Y)) { InternalRover = newRover; return true; } Console.WriteLine("Cannot perform move, obstacle in the way!"); return false; }
public void Execute(string commands, IRover rover) { foreach (var command in commands) { if (command == 'M') rover.MoveForward(); else if (command == 'L') rover.RotateLeft(); else rover.RotateRight(); } }
public void ShouldBeSecondRoverValidOutput() { var rover = TestableRover.Create(); IPosition position = new RoverPosition(3, 3); rover.PrompterMock.SetupProperty(x => x.Command, "MMRMMRMRRM"); rover.PrompterMock.Setup(x => x.IsValid()).Returns(true); _secondRover = new Rover(position, rover.PrompterMock.Object, DirectionType.E); var result = _secondRover.Execute(); Assert.AreEqual("5 1 E", result); }
public void ShouldBeFirstRoverValidOutput() { var rover = TestableRover.Create(); IPosition position = new RoverPosition(1, 2); rover.PrompterMock.SetupProperty(x => x.Command, "LMLMLMLMM"); rover.PrompterMock.Setup(x => x.IsValid()).Returns(true); _firstRover = new Rover(position, rover.PrompterMock.Object, DirectionType.N); var result = _firstRover.Execute(); Assert.AreEqual("1 3 N", result); }
public IRover UpdateRover(IRover rover) { if (rover.Id == Guid.Empty) return AddRover(rover); // refresh the rover in the Dictionary // - we do this mainly because we can't be sure that the dictionary contains a reference // to the same object we're passing to it due to serialisation lock (dictLock) { rovers.Remove(rover.Id); rovers.Add(rover.Id, rover); } Save(); return rover; }
public override void Execute(IRover rover, Instruction instruction) { switch(instruction.GetInstruction()) { case Instruction.TurnLeft: rover.SetFacing(West); break; case Instruction.TurnRight: rover.SetFacing(East); break; case Instruction.MoveForward: rover.MoveNorth(); break; default: throw new ArgumentException("Incorrect facing code."); } }
public void Execute(IRover rover) { switch (rover.Cardinal) { case CardinalType.N: rover.Cardinal = CardinalType.E; break; case CardinalType.E: rover.Cardinal = CardinalType.S; break; case CardinalType.S: rover.Cardinal = CardinalType.W; break; case CardinalType.W: rover.Cardinal = CardinalType.N; break; } }
public void Execute(IRover rover) { switch (rover.Cardinal) { case CardinalType.N: rover.Y++; break; case CardinalType.E: rover.X++; break; case CardinalType.S: rover.Y--; break; case CardinalType.W: rover.X--; break; } }
public bool IsRoverWithinLimits(IRover rover) { return rover.Easting.Between(0, _eastBoundary) && rover.Northing.Between(0, _northBoundary); }
public void Go_Should_Not_Throw_When_Lower_Case_Input_Given() { sut = new Rover(5, 5, "n", "LrM", 10, 10); sut.Go(); }
public void SetUp() { sut = new Rover(4, 5, "N", "LRM", 10, 10); }
public void Rover_Stays_Dead_Even_If_Instructions_Bring_It_Back_On_To_Plateau() { sut = new Rover(10, 10, "N", "MLLM", 10, 10); sut.Go(); string result = sut.CurrentPosition(); Assert.AreEqual("DEAD", result); }
public ObstacleInTheWayException(IRover obstacle) { Obstacle = obstacle; }
public bool Equals(IRover rover) { return this.Id == rover.Id; }
public void Rover_Can_Be_At_Very_Edge_Of_Plateau() { sut = new Rover(10, 10, "N", "M", 10, 10); string result = sut.CurrentPosition(); Assert.AreEqual("10 10 N", result); }
public void SetReceiver(IRover aRover) { rover = aRover; }
/// <summary>Performs rotate right action on a rover.</summary> /// <param name="rover">The rover to use.</param> public void Execute(IRover rover) { rover.Rotate(true); }