有没有办法简化这个功能?

时间:2021-03-25 15:52:17

标签: c# refactoring simplify

我有一个函数,它返回一个带有坐标和对象正在看的方向的字符串。 但我真的不喜欢函数的长度,我想缩短它。我不知道我能做些什么来缩短它。有人可以帮我吗?

函数如下所示:

public string Execute(string text)
{
    var letters = text.ToCharArray().ToList();

    foreach (var letter in letters)
    {
        if (letter == 'R')
        {
            switch(direction)
            {
                case "N":
                    direction = "E";
                    break;
                case "E":
                    direction = "S";
                    break;
                case "S":
                    direction = "W";
                    break;
                case "W":
                    direction = "N";
                    break;
            }
        }

        if (letter == 'L')
        {
            switch (direction)
            {
                case "N":
                    direction = "W";
                    break;
                case "E":
                    direction = "N";
                    break;
                case "S":
                    direction = "E";
                    break;
                case "W":
                    direction = "S";
                    break;
            }
        }

        if (letter == 'M')
        {
            switch (direction)
            {
                case "N":
                    positionY++;
                    break;
                case "E":
                    positionX++;
                    break;
                case "S":
                    positionY--;
                    break;
                case "W":
                    positionX--;
                    break;
            }
        }
            
        if (letter == 'B')
        {
            switch (direction)
            {
                case "N":
                    positionY--;
                    break;
                case "E":
                    positionX--;
                    break;
                case "S":
                    positionY++;
                    break;
                case "W":
                    positionX++;
                    break;
            }
        }
    }

    return $"{positionX}:{positionY}:{direction}";
}

这里的文本只是一个无用的文本,因此我可以提交我的问题,因为我的代码太长并且 Stackoverflow 将问题识别为主要代码,因此我必须编写更多没有代码括号的文本才能提交问题.您不必阅读此文本,因为它没有任何目的。

1 个答案:

答案 0 :(得分:-1)

尝试以下操作:

           string text = "";
            var letters = text.ToCharArray().ToList();
            Dictionary<char, Dictionary<string, string>> dict = new Dictionary<char, Dictionary<string, string>>() {
                {'R', new Dictionary<string, string>() { { "N", "E" }, { "E", "S" }, { "S", "W" }, { "W", "N" }}},
                {'L', new Dictionary<string, string>() { { "N", "W" }, { "E", "N" }, { "S", "E" }, { "W", "S" }}},
                {'M', new Dictionary<string, string>() { { "N", "AY" }, { "E", "AX" }, { "S", "SY" }, { "W", "SX" }}},
                {'R', new Dictionary<string, string>() { { "N", "SY" }, { "E", "SX" }, { "N", "AY" }, { "N", "AX" }}}
            };
            string direction = "";
            int positionX = 0;
            int positionY = 0;

            foreach (var letter in letters)
            {
                string value = dict[letter][direction];
                switch (value)
                {
                    case "SX":
                        positionX--;
                        break;
                    case "SY":
                        positionY--;
                        break;
                    case "AX":
                        positionX++;
                        break;
                    case "AY":
                        positionY++;
                        break;
                    case "N":
                        direction = value;
                        break;
                    case "E":
                        direction = value;
                        break;
                    case "S":
                        direction = value;
                        break;
                    case "W":
                        direction = value;
                        break;
                }