c#2.0-方块游戏c#

本文关键字:游戏 方块 c#2 | 更新日期: 2023-09-27 17:57:57

你能帮我纠正我的代码吗

    static void SolveAndDraw(int number)
    {
        // Create Dynamic List of list to 
        List<List<int>> matrix = new List<List<int>>();
        // Intialize the inner lists 
        for (int j = 0; j < number; j++)
        {
            matrix.Add(new List<int>());
        
        
        }
        char direction = 'r';
        int xPos = 0, yPos = 0;
        int rightLimit = number - 1;
        int leftLimit = 0;
        int upLimit = 0;
        int bottomLimit = number - 1;
        for (int i = 1; i <= number * number; ++i)
        {
           // matrix[yPos][xPos] = i;
            matrix[xPos].Insert(yPos, i);
            switch (direction)
            {
                case 'r':
                    if (xPos < rightLimit)
                    {
                        ++xPos;
                    }
                    else
                    {
                        direction = 'd';
                        ++upLimit;
                        ++yPos;
                    }
                    break;

                case 'l':
                    if (xPos > leftLimit)
                    {
                        --xPos;
                    }
                    else
                    {
                        direction = 'u';
                        --bottomLimit;
                        --yPos;
                    }
                    break;
                case 'u':
                    if (yPos > upLimit)
                    {
                        --yPos;
                    }
                    else
                    {
                        direction = 'r';
                        ++leftLimit;
                        ++xPos;
                    }
                    break;
                case 'd':
                    if (yPos < bottomLimit)
                    {
                        ++yPos;
                    }
                    else
                    {
                        direction = 'l';
                        --rightLimit;
                        --xPos;
                    }
                    break;
            }
        }
        // Now, just dump the matrix contents to stdout
        for (int i = 0; i < number; ++i)
        {
            for (int j = 0; j < number; ++j)
            {
                Console.Write("{0}'t", matrix[i][j]);
            }
            Console.Write("'n");
        }
        Console.ReadLine();
    }

它崩溃并给出错误:

索引必须在列表的范围内。

参数名称:索引

c#2.0-方块游戏c#

是否需要列表列表,或者您可以使用具有2个维度的数组(您通过数字传递维度,为什么需要列表?)

static void SolveAndDraw(int number)
        {
            // Create array with two dimensions of size number
            int[,] matrix = new int[number,number];            
            char direction = 'r';
            int xPos = 0, yPos = 0;
            int rightLimit = number - 1;
            int leftLimit = 0;
            int upLimit = 0;
            int bottomLimit = number - 1;
            for (int i = 1; i <= number * number; ++i)
            {
                matrix[xPos,yPos] = i;
                switch (direction)
                {
                    case 'r':
                        if (xPos < rightLimit)
                        {
                            ++xPos;
                        }
                        else
                        {
                            direction = 'd';
                            ++upLimit;
                            ++yPos;
                        }
                        break;

                    case 'l':
                        if (xPos > leftLimit)
                        {
                            --xPos;
                        }
                        else
                        {
                            direction = 'u';
                            --bottomLimit;
                            --yPos;
                        }
                        break;
                    case 'u':
                        if (yPos > upLimit)
                        {
                            --yPos;
                        }
                        else
                        {
                            direction = 'r';
                            ++leftLimit;
                            ++xPos;
                        }
                        break;
                    case 'd':
                        if (yPos < bottomLimit)
                        {
                            ++yPos;
                        }
                        else
                        {
                            direction = 'l';
                            --rightLimit;
                            --xPos;
                        }
                        break;
                }
            }
            // Now, just dump the matrix contents to stdout
            for (int i = 0; i < number; ++i)
            {
                for (int j = 0; j < number; ++j)
                {
                    Console.Write("{0}'t", matrix[i,j]);
                }
                Console.Write("'n");
            }
            Console.ReadLine();
        }
    }

如果尝试在大于列表当前Count的索引处插入(),则会引发该异常。最有可能的是,你正在以这样一种方式遍历"矩阵",即语句

matrix[xPos].Insert(yPos, i);

正在插入到尚未具有yPos大小的列表中。避免这种情况的最简单方法是最初向每个内部列表添加足够的元素:

    // Intialize the inner lists 
    for (int j = 0; j < number; j++)
    {
        matrix.Add(new List<int>());
        // New code here:
        for (int k = 0; k < number; k++)
            matrix[j].Add(0);
    }

在我脑海中没有运行代码的情况下,这行

for (int i = 1; i <= number * number; ++i)

看起来很可疑,可能你应该从0开始,但话说回来,我可能完全错了。