/// <summary> /// This function actually executes the command by rotating the robot based on given direction /// Position doesn't need to validated as long as the robot is already placed on the Grid/Board /// </summary> /// <param name="robot"></param> /// <returns></returns> public virtual CommandFeedback Impact(Robot robot) { //robot must be positioned first if (!robot.IsPositioned) { return(new CommandFeedback { IsSuccess = false, NewPosition = robot.Position, ErrorCode = CommandErrorCode.RobotNotPositioned }); } var result = robot.ChangeDirection(Direction); var feeback = new CommandFeedback { RobotId = robot.RobotId, IsSuccess = result, NewPosition = robot.Position }; if (!result) { feeback.ErrorCode = CommandErrorCode.RotationException; } return(feeback); }
/// <summary> /// This function is the one that move our robot in the direction it is facing /// which implies robot must be placed in a specific position first. /// </summary> /// <param name="robot"></param> /// <param name="position"></param> /// <returns></returns> public virtual CommandFeedback Impact(Robot robot, Position position = null) { //robot must be position first before moving it if (!robot.IsPositioned) { return(new CommandFeedback { IsSuccess = false, NewPosition = robot.Position, ErrorCode = CommandErrorCode.RobotNotPositioned }); } //let's make a clone of our robot so we don't move the original to the wrong postion var originalPosition = (Position)robot.Position.Clone(); //make robot has been positioned switch (originalPosition.Direction) { case Face.North: originalPosition.Y++; break; case Face.East: originalPosition.X++; break; case Face.South: originalPosition.Y--; break; case Face.West: originalPosition.X--; break; } //validate move var success = Board.IsValidPosition(originalPosition); if (success) { robot.Position = originalPosition; } var feedback = new CommandFeedback { RobotId = robot.RobotId, IsSuccess = success, NewPosition = robot.Position }; if (!feedback.IsSuccess) { feedback.ErrorCode = CommandErrorCode.MoveOutOfBounds; } return(feedback); }