public RoverPosition Parse(string source) { var tokens = source.Split(' '); if (tokens.Length != 3) { throw new ParserException($"Could not parse {source} into a Rover, expected 3 tokens got {tokens.Length}"); } int x; if (!int.TryParse(tokens[0], out x)) { throw new ParserException($"Could not parse {source} into a Rover, first token isn't an integer"); } int y; if (!int.TryParse(tokens[1], out y)) { throw new ParserException($"Could not parse {source} into a Rover, second token isn't an integer"); } var coordinate = new Coordinate(x, y); IDirectionState direction = ReadDirectionToken(tokens[2]); return new RoverPosition(coordinate, direction); }
public Terrain(Coordinate coordinate) { if (coordinate.X < 1 || coordinate.Y < 1) { throw new TerrainInvalidInitializationValueException(Messages.TerrainInvalidInitializationValueExceptionMessage); } _upperBoundary = coordinate; }
public Coordinate Move(Coordinate position, Terrain terrain) { if (position.Y == terrain.UpperBoundary.Y) { throw new RoverInvalidPositionException(Messages.RoverInvalidUpperBoundaryYExceptionMessage); } position.Y++; return position; }
public Coordinate Move(Coordinate position, Terrain terrain) { if (position.X == terrain.LowerBoundary.X) { throw new RoverInvalidPositionException(Messages.RoverInvalidLowerBoundaryXExceptionMessage); } position.X--; return position; }
public void CannotMoveForwardWhenObstructed() { var currentLocation = new Coordinate(5, 4); var nextSpace = grid.GetNextSpaceInFront(currentLocation, Direction.North); Assert.AreEqual(currentLocation, nextSpace); }
public void CannotMoveBackwardWhenObstructed() { var currentLocation = new Coordinate(5, 6); var nextSpace = grid.GetNextSpaceBehind(currentLocation, Direction.North); Assert.AreEqual(currentLocation, nextSpace); }