开发者

square puzzle c#

开发者 https://www.devze.com 2023-03-08 12:28 出处:网络
Could u help me and correct my code static void SolveAndDraw(int number) { // Create Dynamic List of list to

Could u help me and correct my code

    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;
                    }
                    开发者_StackOverflowbreak;

                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();

    }

It crashes and gives the error:

Index must be within the bounds of the List.

Parameter name: index


Is that list of lists required, or can you use an array with 2 dimensions (you are passing the dimension through number, why do you need the lists?)

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();

        }

    }


That exception is thrown if you try to Insert() at an index that is larger than the current Count of the list. Most likely you are traversing the "matrix" in such a way that the statement

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

is inserting into a list that is not yet of size yPos. The easiest way to avoid this is to add enough elements to each inner list initially:

    // 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);
    }


Without running the code in my mind, this line

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

looks suspicious, may be you should start with 0, but then again, I might be totally off.

0

精彩评论

暂无评论...
验证码 换一张
取 消