public void PlexisChallenge() { Surface MySurface = new Surface(new Size(5, 5)); Rover Spirit = new Rover(MySurface, new Point(1, 2), PhysicalObject.Heading.North); Spirit.Rotate(PhysicalObject.Rotation.Left); Spirit.Move(); Spirit.Rotate(PhysicalObject.Rotation.Left); Spirit.Move(); Spirit.Rotate(PhysicalObject.Rotation.Left); Spirit.Move(); Spirit.Rotate(PhysicalObject.Rotation.Left); Spirit.Move(); Spirit.Move(); Assert.AreEqual(new Point(1, 3), MySurface.GetPosition(Spirit)); Assert.AreEqual(PhysicalObject.Heading.North, Spirit.Direction); Rover Opportunity = new Rover(MySurface, new Point(3, 3), PhysicalObject.Heading.East); Opportunity.Move(); Opportunity.Move(); Opportunity.Rotate(PhysicalObject.Rotation.Right); Opportunity.Move(); Opportunity.Move(); Opportunity.Rotate(PhysicalObject.Rotation.Right); Opportunity.Move(); Opportunity.Rotate(PhysicalObject.Rotation.Right); Opportunity.Rotate(PhysicalObject.Rotation.Right); Opportunity.Move(); Assert.AreEqual(new Point(5, 1), MySurface.GetPosition(Opportunity)); Assert.AreEqual(PhysicalObject.Heading.East, Opportunity.Direction); }
public void XDimensionMoveTest() { var testRover = new Rover("test1", new Position() { X = 2, Y = 2 }, OrientationEnum.W, new MockComModule(), new MockTerrain() { Grid = new IRover[5, 5] }); testRover.Move(); var expectedPosition = new Position() { X = 1, Y = 2 }; Assert.AreEqual(testRover.Position, expectedPosition); testRover.Orientation = OrientationEnum.E; testRover.Move(); expectedPosition.X = 2; Assert.AreEqual(testRover.Position, expectedPosition); }
public void Should_Be_Blocked_By_Obstacle() { bool[,] map = create_map(1,0); Rover rover = new Rover(0, 0, Direction.North); Assert.False(rover.Move(Command.f, map).IsOperationSucceeded()); Assert.AreEqual(new int[] { 0, 0 }, rover.GetLocation()); Assert.True(rover.Move(Command.r, map).IsOperationSucceeded()); Assert.True(rover.Move(Command.f, map).IsOperationSucceeded()); Assert.AreEqual(new int[] { 0, 1 }, rover.GetLocation()); }
public void Should_Move_One_Step_Further() { bool[,] map = create_map(); Rover rover = new Rover(0, 0, Direction.North); //move forward Assert.True(rover.Move(Command.f,map).IsOperationSucceeded()); Assert.AreEqual(new int[] { 1, 0 }, rover.GetLocation()); //move backward Assert.True(rover.Move(Command.b, map).IsOperationSucceeded()); Assert.AreEqual(new int[] { 0, 0 }, rover.GetLocation()); }
public void YDimensionMoveTest() { var testRover = new Rover("test1", new Position() { X = 2, Y = 2 }, OrientationEnum.N, new MockComModule(), new MockTerrain() { Grid = new IRover[5, 5] }); //Test movement on the Y dimension testRover.Move(); var expectedPosition = new Position() { X = 2, Y = 3 }; Assert.AreEqual(testRover.Position, expectedPosition); testRover.Orientation = OrientationEnum.S; testRover.Move(); expectedPosition.Y = 2; Assert.AreEqual(testRover.Position, expectedPosition); }
public void TestMove(string movements, char direction, int startX, int startY, int finalX, int finalY) { Grid world = new Grid(10, 10); Rover rover = new Rover(startX, startY, direction); world.GridCellAt(3, 5).ContainsObstacle = true; world.GridCellAt(3, 6).ContainsObstacle = true; world.GridCellAt(1, 8).ContainsObstacle = true; world.GridCellAt(7, 4).ContainsObstacle = true; world.GridCellAt(6, 2).ContainsObstacle = true; rover.World = world; foreach (char movement in movements) { if (!rover.Move(movement)) { break; } } Console.WriteLine("Final Location: " + rover.CurrentLocation.X + " " + rover.CurrentLocation.Y); Assert.AreEqual(finalX, rover.CurrentLocation.X); Assert.AreEqual(finalY, rover.CurrentLocation.Y); }
public void MoveTest() { Surface MySurface = new Surface(new Size(5, 5)); Rover Rover1 = new Rover(MySurface, new Point(0, 0), PhysicalObject.Heading.North); Rover1.Move(); Assert.AreEqual(new Point(0, 1), MySurface.GetPosition(Rover1)); }
static void Main(string[] args) { var grid = new Grid(10, 5); IPosition initialPosition = new Position(grid); var myRover = new Rover(initialPosition); myRover.TurnRight(); myRover.Move(); myRover.Move(); myRover.Move(); myRover.Move(); myRover.TurnLeft(); myRover.Move(); Console.WriteLine($"{myRover.Point.x} {myRover.Point.y} {myRover.Direction.Name}"); // TODO: Prep parser for external commands }
public void GetForwardFromInitialPositionAndDirectionv2() { var rover = new Rover(new Point(1, 0), Direction.North); rover.Move(Move.Forward); Assert.Equal(new Point(1, 1), rover.Point); Assert.Equal(Direction.North, rover.Direction); }
public void Runaway() { var x = new Position(0, 3); var y = new Position(1, 3); var rover = new Rover(x, y, FacingDirection.N); rover.Move(MoveDirection.B); Assert.Equal(0, rover.Y); }
static void Main(string[] args) { Rover rover = new Rover(5, 5); rover.SetInitialCoordinate(new Location { XPoint = 1, YPoint = 2, Direction = Directions.N }); rover.Move("LMLMLMLMM"); Console.WriteLine("X Point: " + rover.Location.XPoint + " Y Point: " + rover.Location.YPoint + " Direction: " + rover.Location.Direction); rover.SetInitialCoordinate(new Location { XPoint = 3, YPoint = 3, Direction = Directions.E }); rover.Move("MMRMMRMRRM"); Console.WriteLine("X Point: " + rover.Location.XPoint + " Y Point: " + rover.Location.YPoint + " Direction: " + rover.Location.Direction); Console.Read(); }
private Rover Move(Rover rover) { rover.Move(); if (rover.Position.X > _bounds.X || rover.Position.X < 0 || rover.Position.Y > _bounds.Y || rover.Position.Y < 0) { throw new InvalidOperationException("Out of bounds"); } return(rover); }
/// <summary> /// gets the movement instructions from user interaction and moves the rover on the plateau /// </summary> /// <param name="p"></param> /// <param name="rover"></param> private static void getInstructions(Plateau p, Rover rover) { Console.WriteLine("enter instructions:"); string instruction = Console.ReadLine(); foreach (var c in instruction) { rover.Move(c, p); } }
public void RoverMovesForwardFacingNorth() { rover = new Rover(new Coordinate(1, 2), Direction.North, grid); var commands = new char[] { Command.Forward }; rover.Move(commands); Assert.AreEqual(1, rover.Position.X); Assert.AreEqual(3, rover.Position.Y); Assert.AreEqual(Direction.North, rover.Direction); }
public void RoverMovesBackwardFacingEast() { rover = new Rover(new Coordinate(1, 2), Direction.East, grid); var commands = new char[] { Command.Backward }; rover.Move(commands); Assert.AreEqual(0, rover.Position.X); Assert.AreEqual(2, rover.Position.Y); Assert.AreEqual(Direction.East, rover.Direction); }
public string Explore() { Rover rover = new Rover(plateauSize, roverCoordinates); if (!string.IsNullOrWhiteSpace(InputParser.error)) { return(InputParser.error); } string result = rover.Move(roverMovement); return(result); }
private bool CheckRoverMovedPosition(Rover marsRovel) { var rover = new Rover(marsRovel); rover.Move(); var isOutsidePlateau = IsOutsidePlateau(rover); var isHaveRoverInWillPosition = isOutsidePlateau || RoverList.Any(x => x.RoverPosition.X.Equals(rover.RoverPosition.X) && x.RoverPosition.Y.Equals(rover.RoverPosition.Y)); return(isHaveRoverInWillPosition); }
public string SendCommand(string commandStr) { if (string.IsNullOrWhiteSpace(commandStr)) { throw new ApplicationException("Command must be initialized"); } string[] commandLines = commandStr.Split(new string[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries); if (!(commandLines.Length >= 3 && commandLines.Length % 2 == 1)) { throw new ApplicationException("CommandStr lines must be bigger than 3 and odd"); } string loverLeftCoordinate = "00"; Surface surface = new Surface(loverLeftCoordinate, commandLines[0]); StringBuilder builder = new StringBuilder(); for (int i = 1; i < commandLines.Length; i = i + 2) { Rover rover = new Rover(commandLines[i], commandLines[i + 1]); validateWhetherThereIsARoverInThisPosition(surface, rover); rover.Move(); if (rover.PositionX > surface.EastEdge) { throw new ApplicationException("Surface EastEdge could not be passed"); } if (rover.PositionX < surface.WestEdge) { throw new ApplicationException("Surface WestEdge could not be passed"); } if (rover.PositionY > surface.NorthEdge) { throw new ApplicationException("Surface NorthEdge could not be passed"); } if (rover.PositionY < surface.SouthEdge) { throw new ApplicationException("Surface SouthEdge could not be passed"); } validateWhetherThereIsARoverInThisPosition(surface, rover); surface.PlaceRover(rover); builder.AppendLine(rover.PositionX.ToString() + rover.PositionY.ToString() + rover.Direction.ToString()); } return(builder.ToString()); }
public void PerformProcessing(Rover rover) { foreach (var instruction in Instructions) { if (instruction == Instruction.L || instruction == Instruction.R) { rover.Compass.ChangeDirection(rover, instruction); } if (instruction == Instruction.M) { rover.Move(); } } }
static void Main(string[] args) { //string startLocation; string moveCommand; string startLocation; //Enter starting location for rover1 Console.Write("First Rover: Enter starting location: "); startLocation = Console.ReadLine(); //Enter the instruction string for rover1 Console.Write("First Rover: Enter instruction string: "); moveCommand = Console.ReadLine(); //Visual line break for separating rovers in the console Console.WriteLine(); //Create rover1 object Rover rover1 = new Rover(startLocation); //Launching the Move function of rover1 with the moveCommand string rover1.Move(moveCommand); //Enter starting location for rover2 Console.Write("Second Rover: Enter starting location: "); startLocation = Console.ReadLine(); //Enter the instruction string for rover2 Console.Write("Second Rover: Enter instruction string: "); moveCommand = Console.ReadLine(); //Visual line break for separating rovers in the console Console.WriteLine(); //Create rover2 object Rover rover2 = new Rover(startLocation); //Launching the Move function of rover2 with the moveCommand string rover2.Move(moveCommand); //Reporting the final position of rover1 Console.WriteLine("Rover 1 final position: " + rover1.newLocation); //Reporting the final position of rover2 Console.WriteLine("Rover 2 final position: " + rover2.newLocation); // End Main }
public void Should_Not_Be_Blocked_On_The_Edge() { bool[,] map = create_map(); Rover rover = new Rover(0, 0, Direction.North); Assert.True(rover.Move(Command.b, map).IsOperationSucceeded()); Assert.AreEqual(new int[] { 99, 0 }, rover.GetLocation()); rover = new Rover(0, 0, Direction.West); Assert.True(rover.Move(Command.f, map).IsOperationSucceeded()); Assert.AreEqual(new int[] { 0, 99 }, rover.GetLocation()); rover = new Rover(99, 99, Direction.North); Assert.True(rover.Move(Command.f, map).IsOperationSucceeded()); Assert.AreEqual(new int[] { 0, 99 }, rover.GetLocation()); rover = new Rover(99, 99, Direction.East); Assert.True(rover.Move(Command.f, map).IsOperationSucceeded()); Assert.AreEqual(new int[] { 99, 0 }, rover.GetLocation()); }
static void Main(string[] args) { var rover = new Rover(); Console.WriteLine("Enter the initial position of the Rover in the form X ,Y ,D "); var initialLocation = Console.ReadLine().Trim().ToUpper(); string[] location = initialLocation.Split(','); if (location.Length == 3) { rover.X = Int32.Parse(location[0]); rover.Y = Int32.Parse(location[1]); rover.direction = (Directions)Enum.Parse(typeof(Directions), location[2]); } Console.WriteLine("Enter the moves of the Rover"); var moves = Console.ReadLine().Trim().ToUpper(); rover.Move(moves); Console.ReadLine(); }
static void play(Rover rover, Position EndOfPlatau) { try { Console.WriteLine("Please choose a command: 'L' or 'R' to change direction, or 'M' to move forward.\n"); string input; if ((input = Console.ReadLine()) != "exit") { rover.Move(input, EndOfPlatau); PrintCurrentLocation(rover); Console.WriteLine("Please choose a command: 'L' or 'R' to change direction, or 'M' to move forward.\n"); } } catch (InvalidInputException ex) { Console.WriteLine(ex.Message); play(rover, EndOfPlatau); } }
public static void Main(string[] args) { int xLoc = AskForXLocation(); int yLoc = AskForYLocation(); char direction = AskForDirection(); string movements = AskForMovements(); Rover rover = new Rover(xLoc, yLoc, direction); rover.World = CreateWorld(); foreach (char movement in movements) { if (!rover.Move(movement)) { break; } } Console.WriteLine("Final Location: " + rover.CurrentLocation.X + " " + rover.CurrentLocation.Y); }
static void Main(string[] args) { try { Plateau plateau = Plateau.FormSize(5, 5); RoverBase rover = new Rover(plateau, "1 2 N", "rover1"); rover.Move("LMLMLMLMM"); Console.WriteLine(String.Format("Position {0}: {1} --> {2}", rover.Name, rover.BaseLocation, rover.CurrentLocation)); // Second rover rover = new Rover(plateau, "3 3 E", "rover2"); rover.Move("MMRMMRMRRM"); Console.WriteLine(String.Format("Position {0}: {1} --> {2}", rover.Name, rover.BaseLocation, rover.CurrentLocation)); } catch (Exception ex) { // If there were any problems in movements or in the base location of the rover, then display it on the screen. Console.WriteLine(ex.Message); } // Wait for a key stroke from user. Console.ReadKey(); }
public string SendCommand(Rover rover, char commandSignal) { if (!commandsMap.ContainsKey(commandSignal)) { throw new Exception($"Unknown command {commandSignal}"); } lock (moveLock) { var command = commandsMap[commandSignal]; switch (command) { case Commands.Left: rover.TurnLeft(); break; case Commands.Right: rover.TurnRight(); break; case Commands.Move: if (IsGridPoint(rover.Next) && !grid[rover.Next.X, rover.Next.Y] ) { grid[rover.Current.X, rover.Current.Y] = false; rover.Move(); grid[rover.Current.X, rover.Current.Y] = true; } break; } } return(rover.State); }
public void Should_Turn_Left_Or_Right_Correctly() { bool[,] map = create_map(); Rover rover = new Rover(0,0,Direction.North); Assert.True(rover.Move(Command.l, map).IsOperationSucceeded()); Assert.AreEqual(Direction.West,rover.GetDirection()); Assert.True(rover.Move(Command.l,map).IsOperationSucceeded()); Assert.AreEqual(Direction.South, rover.GetDirection()); Assert.True(rover.Move(Command.l, map).IsOperationSucceeded()); Assert.AreEqual(Direction.East, rover.GetDirection()); Assert.True(rover.Move(Command.l, map).IsOperationSucceeded()); Assert.AreEqual(Direction.North, rover.GetDirection()); Assert.True(rover.Move(Command.r, map).IsOperationSucceeded()); Assert.AreEqual(Direction.East, rover.GetDirection()); Assert.True(rover.Move(Command.r, map).IsOperationSucceeded()); Assert.AreEqual(Direction.South, rover.GetDirection()); Assert.True(rover.Move(Command.r, map).IsOperationSucceeded()); Assert.AreEqual(Direction.West, rover.GetDirection()); Assert.True(rover.Move(Command.r, map).IsOperationSucceeded()); Assert.AreEqual(Direction.North, rover.GetDirection()); }
public void RotateTest() { Surface MySurface = new Surface(new Size(5, 5)); Rover Rover1 = new Rover(MySurface, new Point(0, 0), PhysicalObject.Heading.North); Rover1.Move(); Assert.AreEqual(new Point(0, 1), MySurface.GetPosition(Rover1)); Rover1.Rotate(PhysicalObject.Rotation.Right); Rover1.Move(); Assert.AreEqual(new Point(1, 1), MySurface.GetPosition(Rover1)); Rover1.Rotate(PhysicalObject.Rotation.Right); Rover1.Move(); Assert.AreEqual(new Point(1, 0), MySurface.GetPosition(Rover1)); Rover1.Rotate(PhysicalObject.Rotation.Right); Rover1.Move(); Assert.AreEqual(new Point(0, 0), MySurface.GetPosition(Rover1)); Rover1.Rotate(PhysicalObject.Rotation.Right); Rover1.Move(); Assert.AreEqual(new Point(0, 1), MySurface.GetPosition(Rover1)); Rover1.Rotate(PhysicalObject.Rotation.Right); Rover1.Move(); Assert.AreEqual(new Point(1, 1), MySurface.GetPosition(Rover1)); Rover1.Rotate(PhysicalObject.Rotation.Left); Rover1.Move(); Assert.AreEqual(new Point(1, 2), MySurface.GetPosition(Rover1)); Rover1.Rotate(PhysicalObject.Rotation.Left); Rover1.Move(); Assert.AreEqual(new Point(0, 2), MySurface.GetPosition(Rover1)); Rover1.Rotate(PhysicalObject.Rotation.Left); Rover1.Move(); Assert.AreEqual(new Point(0, 1), MySurface.GetPosition(Rover1)); Rover1.Rotate(PhysicalObject.Rotation.Left); Rover1.Move(); Assert.AreEqual(new Point(1, 1), MySurface.GetPosition(Rover1)); Rover1.Rotate(PhysicalObject.Rotation.Left); Rover1.Move(); Assert.AreEqual(new Point(1, 2), MySurface.GetPosition(Rover1)); Rover1.Rotate(PhysicalObject.Rotation.Left); Rover1.Move(); Assert.AreEqual(new Point(0, 2), MySurface.GetPosition(Rover1)); }
public void MoveRover(Rover rover) { rover.Move(); }
static void Main(string[] args) { Console.Write("Enter the plateau's upper-right coordinates (ex. 5 5) :"); string line = Console.ReadLine(); Coordinate upperRightCoordinate = MarsRoverCommandHelper.PopulateCoordinate(line); if (upperRightCoordinate is default(Coordinate)) { Console.WriteLine("The input is not valid."); return; } // generate the rover and plateau Coordinate lowerLeftCoordinate = new Coordinate(); PlateauBounds plateauBounds = new PlateauBounds(upperRightCoordinate, lowerLeftCoordinate); IPlateau plateau = new Plateau(plateauBounds); Coordinate roverCurrentPosition = new Coordinate(); RoverPosition roverPosition = new RoverPosition(roverCurrentPosition, RoverDirectionEnum.North); IRover rover = new Rover(plateau, roverPosition); while (true) { Console.Clear(); Console.WriteLine(string.Format("Plateau's bounds are: Upper-Right = ({0},{1}) Lower-Left = ({2},{3})" + Environment.NewLine, plateau.Bounds.UpperRight.X, plateau.Bounds.UpperRight.Y, plateau.Bounds.LowerLeft.X, plateau.Bounds.LowerLeft.Y)); Console.Write("Enter the rover's start position (ex. 1 1 N) :"); line = Console.ReadLine(); rover.CurrentPosition = MarsRoverCommandHelper.PopulateRoverPosition(line); if (rover.CurrentPosition is default(RoverPosition)) { Console.WriteLine("The input is not valid."); return; } Console.Write("Enter rover's moving sequence (ex. LMRMRMM) :"); line = Console.ReadLine(); IEnumerable <MovingDirectionEnum> movingDirectionList = MarsRoverCommandHelper.PopulateMovingDirectionList(line); if (movingDirectionList.Any() == false) { Console.WriteLine("The input is not valid."); return; } foreach (MovingDirectionEnum direction in movingDirectionList) { rover.Move(direction); } Console.WriteLine(string.Format(Environment.NewLine + "Current Position is: {0} {1} {2}", rover.CurrentPosition.Coordinate.X, rover.CurrentPosition.Coordinate.Y, rover.CurrentPosition.Direction)); Console.Write(Environment.NewLine + "0: Exit \n1: Retry\nSelect :"); ConsoleKeyInfo key = Console.ReadKey(); if (key.Key != ConsoleKey.D1 && key.Key != ConsoleKey.NumPad1) { return; } } }
public void TestOutOfBounds() { var testRover = new Rover("test1", new Position() { X = 4, Y = 4 }, OrientationEnum.W, new MockComModule(), new MockTerrain() { Grid = new IRover[5, 5] }); try { testRover.Move(); } catch (Exception ex) { Assert.AreEqual(ex.GetType(), typeof(RoverOutOfBoundsException)); } }
public void TMove() { try { MarsRover.Plateau plateau = Plateau.FormSize(5, 6); string baseLocation = string.Empty; string name = string.Empty; MarsRover.Rover target = new MarsRover.Rover(plateau, baseLocation, name); // TODO: Initialize to an appropriate value string movements = string.Empty; // TODO: Initialize to an appropriate value target.Move(movements); } catch (Exception ex) { Assert.Fail(ex.Message); } }
public void RoverTurnsLeftFacingNorth() { rover = new Rover(new Coordinate(1, 2), Direction.North, grid); var commands = new char[] { Command.TurnLeft }; rover.Move(commands); Assert.AreEqual(Direction.West, rover.Direction); }
public void Move_CanMoveSouth(int distance, int expectedPosition) { _rover.Move(distance); Assert.AreEqual(expectedPosition, _rover.GridReference); }
public void RoverTurnsRightFacingEast() { rover = new Rover(new Coordinate(1, 2), Direction.East, grid); var commands = new char[] { Command.TurnRight }; rover.Move(commands); Assert.AreEqual(Direction.South, rover.Direction); }
static void Main(string[] args) { HashSet<Rover> Rovers = new HashSet<Rover>(); String[] Input; Console.WriteLine("Please specify the size of the Mars Plateu:"); Console.WriteLine("The first number is the width, and the second number is the height."); Input = Console.ReadLine().Split(' '); Surface MySurface = new Surface(new Size(Convert.ToInt32(Input[0]), Convert.ToInt32(Input[1]))); while (true) { Console.WriteLine(""); Console.WriteLine("Please specify the starting position and direction of the rover:"); Console.WriteLine("The first number is the horizontal coordinate, the second number is the vertical coordinate"); Console.WriteLine("The third value is a single character representing a Cardinal Point (N, S, E, or W)"); Console.WriteLine("Please enter a blank line to indicate you have no more rovers to enter."); Input = Console.ReadLine().Split(' '); try { PhysicalObject.Heading Direction = PhysicalObject.Heading.North; switch (Input[2].ToUpper()) { case "N": Direction = PhysicalObject.Heading.North; break; case "E": Direction = PhysicalObject.Heading.East; break; case "S": Direction = PhysicalObject.Heading.South; break; case "W": Direction = PhysicalObject.Heading.West; break; } Rover CurrentRover = new Rover(MySurface, new Point(Convert.ToInt32(Input[0]), Convert.ToInt32(Input[1])), Direction); Console.WriteLine("Please provide a string of directions for the rover to follow. No spaces are requred."); Console.WriteLine("{ L = Turn Left; R = Turn Right; M = Move 1 unit forward }"); String UserInput = Console.ReadLine().Replace(" ", ""); foreach (Char character in UserInput.ToUpper()) { switch (character) { case 'L': CurrentRover.Rotate(PhysicalObject.Rotation.Left); break; case 'R': CurrentRover.Rotate(PhysicalObject.Rotation.Right); break; case 'M': CurrentRover.Move(); break; } } Rovers.Add(CurrentRover); } catch { break; } } Console.WriteLine("These are the resulting location(s) for your rover(s):"); foreach (Rover TestRover in Rovers) { String Output = ""; Output += MySurface.GetPosition(TestRover).X; Output += " "; Output += MySurface.GetPosition(TestRover).Y; Output += " "; switch (TestRover.Direction) { case PhysicalObject.Heading.North: Output += "N"; break; case PhysicalObject.Heading.East: Output += "E"; break; case PhysicalObject.Heading.South: Output += "S"; break; case PhysicalObject.Heading.West: Output += "W"; break; } Console.WriteLine(Output); } Console.Read(); }
static void Main(string[] args) { try { WriteConsole("Please write the upper-right coordinates of the plateau and Press 'Enter'"); WriteConsole("Sample: 5 5"); var upperRightCoordinates = Console.ReadLine(); while (!Regex.Match(upperRightCoordinates, @"\d+\s\d+").Success) { WriteConsole("Please write the correct format => X Y"); WriteConsole("Sample: 10 10"); upperRightCoordinates = Console.ReadLine(); } var coordinates = upperRightCoordinates.Split(" "); var upperRightCornerX = Convert.ToInt32(coordinates[0]); var upperRightCornerY = Convert.ToInt32(coordinates[1]); var c = true; while (c) { WriteConsole("Now, Please enter the Rover position and direction"); WriteConsole("Sample: 1 2 N"); var roverPosition = Console.ReadLine(); while (!Regex.Match(roverPosition, @"\d+\s\d+\s\w").Success) { WriteConsole("Please write the correct format => X Y D"); WriteConsole("Sample: 10 10 N"); roverPosition = Console.ReadLine().ToUpperInvariant(); } var rover = new Rover(roverPosition, upperRightCornerX, upperRightCornerY); WriteConsole("Now, Please enter the Rover's move orders"); WriteConsole("Sample: LMLMLMLMM"); var orderList = new char[] { 'L', 'M', 'R' }; var moveOrders = Console.ReadLine(); while (!Regex.Match(moveOrders, @"\w+").Success || moveOrders.ToCharArray().Where(c => !orderList.Contains(c)).ToArray().Length > 0) { WriteConsole("Please write the correct format => LRM"); WriteConsole("Sample: LMLMLMLMM"); moveOrders = Console.ReadLine().ToUpperInvariant(); } rover.Move(moveOrders); WriteConsole(string.Format("Rover's new position and Direction : {0}", rover.LastPosition)); var yOrN = string.Empty; while (yOrN != "Y" && yOrN != "N") { WriteConsole("Do you want to move your next rover? Y/N"); yOrN = Console.ReadLine().ToUpperInvariant(); } c = yOrN == "Y"; } } catch (Exception e) { WriteConsole(e.Message); } }
static void Main(string[] args) { HashSet <Rover> Rovers = new HashSet <Rover>(); String[] Input; Console.WriteLine("Please specify the size of the Mars Plateu:"); Console.WriteLine("The first number is the width, and the second number is the height."); Input = Console.ReadLine().Split(' '); Surface MySurface = new Surface(new Size(Convert.ToInt32(Input[0]), Convert.ToInt32(Input[1]))); while (true) { Console.WriteLine(""); Console.WriteLine("Please specify the starting position and direction of the rover:"); Console.WriteLine("The first number is the horizontal coordinate, the second number is the vertical coordinate"); Console.WriteLine("The third value is a single character representing a Cardinal Point (N, S, E, or W)"); Console.WriteLine("Please enter a blank line to indicate you have no more rovers to enter."); Input = Console.ReadLine().Split(' '); try { PhysicalObject.Heading Direction = PhysicalObject.Heading.North; switch (Input[2].ToUpper()) { case "N": Direction = PhysicalObject.Heading.North; break; case "E": Direction = PhysicalObject.Heading.East; break; case "S": Direction = PhysicalObject.Heading.South; break; case "W": Direction = PhysicalObject.Heading.West; break; } Rover CurrentRover = new Rover(MySurface, new Point(Convert.ToInt32(Input[0]), Convert.ToInt32(Input[1])), Direction); Console.WriteLine("Please provide a string of directions for the rover to follow. No spaces are requred."); Console.WriteLine("{ L = Turn Left; R = Turn Right; M = Move 1 unit forward }"); String UserInput = Console.ReadLine().Replace(" ", ""); foreach (Char character in UserInput.ToUpper()) { switch (character) { case 'L': CurrentRover.Rotate(PhysicalObject.Rotation.Left); break; case 'R': CurrentRover.Rotate(PhysicalObject.Rotation.Right); break; case 'M': CurrentRover.Move(); break; } } Rovers.Add(CurrentRover); } catch { break; } } Console.WriteLine("These are the resulting location(s) for your rover(s):"); foreach (Rover TestRover in Rovers) { String Output = ""; Output += MySurface.GetPosition(TestRover).X; Output += " "; Output += MySurface.GetPosition(TestRover).Y; Output += " "; switch (TestRover.Direction) { case PhysicalObject.Heading.North: Output += "N"; break; case PhysicalObject.Heading.East: Output += "E"; break; case PhysicalObject.Heading.South: Output += "S"; break; case PhysicalObject.Heading.West: Output += "W"; break; } Console.WriteLine(Output); } Console.Read(); }
public void TMove2() { try { MarsRover.Plateau plateau = Plateau.FormSize(5, 6); string baseLocation = "0 1 N"; string name = "rover1"; MarsRover.Rover target = new MarsRover.Rover(plateau, baseLocation, name); string movements = "LMLM"; target.Move(movements); Assert.IsTrue(target.CurrentLocation != target.BaseLocation); } catch (Exception ex) { Assert.Fail(ex.Message); } }
public void TestCollision() { var mockTerrain = new MockTerrain() { Grid = new IRover[5, 5] }; var testRover = new Rover("test1", new Position() { X = 3, Y = 3 }, OrientationEnum.W, new MockComModule(), mockTerrain); var testRover2 = new Rover("test2", new Position() { X = 4, Y = 4 }, OrientationEnum.W, new MockComModule(), mockTerrain); try { testRover.Move(); } catch (Exception ex) { Assert.AreEqual(ex.GetType(), typeof(ObstacleInTheWayException)); } }