/// <summary> /// Place function /// </summary> /// <param name="targetLocation">coordinate of the new location robot to be placed</param> /// <param name="f">direction we are facing</param> internal void Place(CoordinateXY targetLocation, Direction f = Direction.NORTH) { if (!_table.IsValidPlacement(targetLocation)) { throw new RobotOutofBoundException(_table.TableBoundary, targetLocation); } this._location = targetLocation; this._currentDirection = f; }
private void SetTableDimensions(int height, int width) { if (height <= 0 || width <= 0) { throw new ArgumentException( $"Invalid table dimensions: height {height}, width {width}"); } this._tableBoundary = new CoordinateXY(height - 1, width - 1); }
// validate if robot placement is valid public bool IsValidPlacement(CoordinateXY targetLocation) { // if the coordinate is out side of x: 0 -> X if (targetLocation.X < 0 || targetLocation.Y < 0 || targetLocation.Y > this.TableBoundary.Y || targetLocation.X > this.TableBoundary.X) { return(false); } return(true); }
/// <summary> /// Move function will move the toy robot one unit forward in the direction it is currently facing. /// </summary> /// <returns>true if move was successful, false otherwise</returns> internal void Move() { if (this._location != null) { CoordinateXY targetLocation = new CoordinateXY(this._location.X, this._location.Y); targetLocation.Move(this._currentDirection, 1); if (_table.IsValidPlacement(targetLocation)) { this._location = targetLocation; } else { throw new RobotOutofBoundException(this._table.TableBoundary, targetLocation); } } else { throw new InvalidRobotCommandException("MOVE", "Robot has not been placed."); } }
public RobotOutofBoundException(CoordinateXY tableBoundary, CoordinateXY robotLocation) { this._tableBoundary = tableBoundary; this._robotLocation = robotLocation; }