枚举在C#中表现异常

本文关键字:异常 枚举 | 更新日期: 2023-09-27 18:21:39

我正在为一个学校项目编程(实现搜索算法),遇到了以下问题:

有一个名为action的变量,其类型为enum Actions{esquerda=0,abaixo=1,direita=2,acima=3}。在其中一个搜索算法扩展给定状态的邻居(名为Neighbours的属性执行该过程)后,它调用一个方法(Expansion()),该方法获取可能的邻居并以列表形式返回。问题是在返回之后,如果不做任何事情,变量action的值在调试模式下第一次读取后会发生异常变化。

行:List temp=Expansion();在调用并填充变量temp之后,当您第二次读取temp列表中任何元素的value操作时,它会将其值更改为枚举(acima)的最后一项。

public class PuzzleState : Search.Node
    {
        private States[,] actualState;
        private int empLinePos;
        private int empColumPos;
        private int dimension;
        private Actions action;
        private PuzzleState father;
        private List<PuzzleState> children = new List<PuzzleState>();
        public IEnumerable<Search.Node> Neighbours
        {
            get
            {
                List<PuzzleState> temp = Expansion();
            //    List<Search.Node> converted = Convert(temp);
                return null;
            }
        }
    public PuzzleState(int dimension, States[,] iniStates, Actions lastAction, PuzzleState father)
    {
        actualState = new States[dimension, dimension];
        actualState = iniStates;
        this.action = lastAction;
        this.dimension = dimension;
        this.father = father;
        for (int i = 0; i < dimension; i++)
        {
            for (int j = 0; j < dimension; j++)
            {
                if (actualState[i, j] == States.Empty)
                {
                    empLinePos = i;
                    empColumPos = j;
                    return;
                }
            }
        }
    }
     public List<PuzzleState> Expansion()
        {
            List<PuzzleState> suns = new List<PuzzleState>();
            foreach (Actions item in System.Enum.GetValues(typeof(Actions)))
            {
                PuzzleState temp = SucessorFunc(item);
                if (temp != null)
                {
                    suns.Add(temp);
                }
            }
            return suns;
        }
        public PuzzleState SucessorFunc(Actions action)
        {
            this.action = action;
            switch (action)
            {
                case Actions.esquerda:
                    if (empColumPos > 0)
                    {
                        States[,] newPossibilities = Copy();
                        newPossibilities[empLinePos, empColumPos] = newPossibilities[empLinePos, empColumPos - 1];
                        newPossibilities[empLinePos, empColumPos - 1] = States.Empty;
                        return new PuzzleState(dimension, newPossibilities, action, this);
                    }
                    break;
                case Actions.direita:
                    if (empColumPos < dimension - 1)
                    {
                        States[,] newPossibilities = Copy();
                        newPossibilities[empLinePos, empColumPos] = newPossibilities[empLinePos, empColumPos + 1];
                        newPossibilities[empLinePos, empColumPos + 1] = States.Empty;
                        return new PuzzleState(dimension, newPossibilities, action, this);
                    }
                    break;
                case Actions.acima:
                    if (empLinePos > 0)
                    {
                        States[,] newPossibilities = Copy();
                        newPossibilities[empLinePos, empColumPos] = newPossibilities[empLinePos - 1, empColumPos];
                        newPossibilities[empLinePos - 1, empColumPos] = States.Empty;
                        return new PuzzleState(dimension, newPossibilities, action, this);
                    }
                    break;
                case Actions.abaixo:
                    if (empLinePos < dimension - 1)
                    {
                        States[,] newPossibilities = Copy();
                        newPossibilities[empLinePos, empColumPos] = newPossibilities[empLinePos + 1, empColumPos];
                        newPossibilities[empLinePos + 1, empColumPos] = States.Empty;
                        return new PuzzleState(dimension, newPossibilities, action, this);
                    }
                    break;
            }
            return null;
        }
}

正如我所知,这听起来很疯狂,我制作了一个视频来展示这种新的C#效果:

http://youtu.be/gXQ6QyhMDDI

代码只是一个.cs文件,链接在这里。

https://www.dropbox.com/sh/gc80ld47eps08nr/AAA3v-t-Jcv_-5CeDzxriLKua?dl=0

如果有人想看看,告诉我这里到底发生了什么,我将不胜感激。

谢谢。

解决方案:

我找到了一种方法,添加了一个名为actionName的字符串var。每次我创建一个新的PuzzleState时,我都会将其值设置为action.tostring()。但我仍然不知道为什么操作值会继续更改最后一个项的枚举值。

构造函数中包含actionName变量及其集合的新代码:

public class PuzzleState : Search.Node
        {
            private States[,] actualState;
            private int empLinePos;
            private int empColumPos;
            private int dimension;
            private Actions action;
            private string actionName;
            private PuzzleState father;
            private List<PuzzleState> children = new List<PuzzleState>();
            public IEnumerable<Search.Node> Neighbours
            {
                get
                {
                    List<PuzzleState> temp = Expansion();
                //    List<Search.Node> converted = Convert(temp);
                    return null;
                }
            }
        public PuzzleState(int dimension, States[,] iniStates, Actions lastAction, PuzzleState father)
        {
            actualState = new States[dimension, dimension];
            actualState = iniStates;
            this.actionName = lastAction.ToString();

            this.action = lastAction;
            this.dimension = dimension;
            this.father = father;
            for (int i = 0; i < dimension; i++)
            {
                for (int j = 0; j < dimension; j++)
                {
                    if (actualState[i, j] == States.Empty)
                    {
                        empLinePos = i;
                        empColumPos = j;
                        return;
                    }
                }
            }
        }
         public List<PuzzleState> Expansion()
            {
                List<PuzzleState> suns = new List<PuzzleState>();
                foreach (Actions item in System.Enum.GetValues(typeof(Actions)))
                {
                    PuzzleState temp = SucessorFunc(item);
                    if (temp != null)
                    {
                        suns.Add(temp);
                    }
                }
                return suns;
            }
            public PuzzleState SucessorFunc(Actions action)
            {
                this.action = action;
                switch (action)
                {
                    case Actions.esquerda:
                        if (empColumPos > 0)
                        {
                            States[,] newPossibilities = Copy();
                            newPossibilities[empLinePos, empColumPos] = newPossibilities[empLinePos, empColumPos - 1];
                            newPossibilities[empLinePos, empColumPos - 1] = States.Empty;
                            return new PuzzleState(dimension, newPossibilities, action, this);
                        }
                        break;
                    case Actions.direita:
                        if (empColumPos < dimension - 1)
                        {
                            States[,] newPossibilities = Copy();
                            newPossibilities[empLinePos, empColumPos] = newPossibilities[empLinePos, empColumPos + 1];
                            newPossibilities[empLinePos, empColumPos + 1] = States.Empty;
                            return new PuzzleState(dimension, newPossibilities, action, this);
                        }
                        break;
                    case Actions.acima:
                        if (empLinePos > 0)
                        {
                            States[,] newPossibilities = Copy();
                            newPossibilities[empLinePos, empColumPos] = newPossibilities[empLinePos - 1, empColumPos];
                            newPossibilities[empLinePos - 1, empColumPos] = States.Empty;
                            return new PuzzleState(dimension, newPossibilities, action, this);
                        }
                        break;
                    case Actions.abaixo:
                        if (empLinePos < dimension - 1)
                        {
                            States[,] newPossibilities = Copy();
                            newPossibilities[empLinePos, empColumPos] = newPossibilities[empLinePos + 1, empColumPos];
                            newPossibilities[empLinePos + 1, empColumPos] = States.Empty;
                            return new PuzzleState(dimension, newPossibilities, action, this);
                        }
                        break;
                }
                return null;
            }
    }

枚举在C#中表现异常

SuccessorFunc中,您正在设置action成员变量:

this.action = action;

因此,当Expansion方法循环时,它将被设置为每个枚举值。