/// <summary> /// this changes the direction in which robot is facing /// </summary> /// <param name="direction"></param> /// <returns></returns> public bool ChangeDirection(RotationalDirection direction) { if (Position == null) { return(false); } Position.Direction = (Face)(((int)Position.Direction + (int)direction) % 360); return(true); }
/// <summary> /// Util function for Right and Left commands /// </summary> /// <param name="robotId"></param> /// <param name="direction"></param> /// <returns></returns> private IHttpActionResult TurnRobot(string robotId, RotationalDirection direction) { try { //do validation first here. if (string.IsNullOrEmpty(robotId)) { var feedback = new CommandFeedback { IsSuccess = false, ErrorCode = CommandErrorCode.RobotNotPositioned }; return(Ok(JsonConvert.SerializeObject(feedback))); } var robot = RobotService.GetRobot(robotId); //if we cannot find robot, we need to return error message. if (robot == null) { var feedback = new CommandFeedback { IsSuccess = false, ErrorCode = CommandErrorCode.RobotNotPositioned }; return(Ok(JsonConvert.SerializeObject(feedback))); } CommandFeedback result; if (direction == RotationalDirection.Left) { result = _rotateLeft.Impact(robot); } else if (direction == RotationalDirection.Right) { result = _rotateRight.Impact(robot); } else { throw new Exception("Invalid direction/Face provided for robot move."); } if (result.IsSuccess) { RobotService.PersistRobot(robot); } return(Ok(JsonConvert.SerializeObject(result))); } catch (Exception ex) { return(InternalServerError(ex)); } }
public RotationCommand(string name, RotationalDirection direction) { Name = name; Direction = direction; }