错误的参数传递给函数C#

时间:2016-12-16 19:39:05

标签: c#

我正在尝试进行自学蛇游戏并遇到一个问题,我想在过去几个小时内解决这个问题。这是我移动蛇头的代码:

        public void moveTo()
        {
            int foodX = food.PosX / food.Width;
            int foodY = food.PosY / food.Height;        
            Direction directionOfMaxForCurrentState= new Direction();
            int currentX = head.PosX / head.Width;
            int currentY = head.PosY / head.Height;          
            do
            {
                int tmpX = currentX;
                int tmpY = currentY;
                 previousX = tmpX;
                 previousY = tmpY;
                directionOfMaxForCurrentState = HighestQactionForState(currentX, currentY, previousX, previousY);
                if (directionOfMaxForCurrentState == Direction.Up) {  head.PosY -= head.Height; }
                if (directionOfMaxForCurrentState == Direction.Down) {  head.PosY += head.Height; }
                if (directionOfMaxForCurrentState == Direction.Left) {  head.PosX -= head.Width; }
                if (directionOfMaxForCurrentState == Direction.Right) {head.PosX += head.Width; }
                currentX = head.PosX / head.Width;
                currentY = head.PosY / head.Height; 
                if (currentX == foodX && currentY == foodY) { snake.Clear(); head = new Cell(1, 1); snake.Add(head); }
            } while (head.PosX == food.PosX && head.PosY == food.PosY);
        }

这里是HighestQactionForState函数:

public Direction HighestQactionForState(int x, int y, int px, int py)
        {
            var Qaround = new List<decimal>();
            var actionsWithMax = new List<Direction>();
            Direction toExclude = new Direction();
            toExclude = directionToExclude(x, y, px, py);
            foreach (Direction action in PosibleActionsForState(x, y).Where(a => a != toExclude).ToList())
            {
                if (action == Direction.Up && (double)Math.Abs(Q[Tuple.Create(x, y - 1, action)] - MaxQaroundState(x, y)) < 0.000000000000000000000001) actionsWithMax.Add(action);
                if (action == Direction.Down && (double)Math.Abs(Q[Tuple.Create(x, y + 1, action)] - MaxQaroundState(x, y)) < 0.000000000000000000000001) actionsWithMax.Add(action);
                if (action == Direction.Left && (double)Math.Abs(Q[Tuple.Create(x - 1, y, action)] - MaxQaroundState(x, y)) < 0.000000000000000000000001) actionsWithMax.Add(action);
                if (action == Direction.Right && (double)Math.Abs(Q[Tuple.Create(x + 1, y, action)] - MaxQaroundState(x, y)) < 0.000000000000000000000001) actionsWithMax.Add(action);
            }
            return actionsWithMax.ElementAt(rnd.Next(actionsWithMax.Count));
        }

所以现在当我将断点放在moveTo函数中时,我看到HighestQactionForState获取了正确的参数,但是在HighestQactionForState函数中参数不正确 - px总是与int x具有相同的值,而py与y导致程序出错。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

如果我没弄错,你应该这样做

directionOfMaxForCurrentState = HighestQactionForState(currentX, currentY, previousX, previousY);
previousX = currentX;
previousY = currentY;

在调用方法之后设置以前的值,这样就可以通过变量的含义来设置它们在上一次迭代中设置的值。

此外,无需声明额外的临时变量。您的参数按值传递,而不是通过引用传递。

答案 1 :(得分:-1)

这一点没有做你想做的事:

int tmpX = currentX;
int tmpY = currentY;
previousX = tmpX;
previousY = tmpY;

您将current值存储在tmp个变量中,然后将这些值分配给previous变量。