C# 而循环卡在代码的随机点上

C# 而循环卡在代码的随机点上,c#,loops,multidimensional-array,random,C#,Loops,Multidimensional Array,Random,我需要帮助,我有我的朋友帮我调试这段代码,我们发现代码在代码上的任意点停止,有时在第一个循环上,有时在最后一个循环上,等等。 代码只是用来制作一个9x9数独板,上面应用了所有的规则。当我分别测试每个模块时,它工作正常,并且打印出了一个做得很好的数独板,但这里的问题是,不知怎么的,它被卡住了。 我们将不胜感激 int[,] helpblock = new int[10, 10]; int[,] helplines = new int[10, 10]; int[,] helpcols = new i

我需要帮助,我有我的朋友帮我调试这段代码,我们发现代码在代码上的任意点停止,有时在第一个循环上,有时在最后一个循环上,等等。 代码只是用来制作一个9x9数独板,上面应用了所有的规则。当我分别测试每个模块时,它工作正常,并且打印出了一个做得很好的数独板,但这里的问题是,不知怎么的,它被卡住了。 我们将不胜感激

int[,] helpblock = new int[10, 10];
int[,] helplines = new int[10, 10];
int[,] helpcols = new int[10, 10];
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        helplines[i, j] = 0;
        helpcols[i, j] = 0;
        helpblock[i, j] = 0;
    }
}

Random rnf = new Random();
int help1;
for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
        help1 = rnf.Next(1, 10);
        if (i == 0 || i == 1 || i == 2) {
            if (j == 0 || j == 1 || j == 2) {
                if (helplines[i, help1] != 0 || helpblock[0, help1] != 0 || helpcols[j, help1] != 0) {
                    while (helplines[i, help1] != 0 || helpblock[0, help1] != 0 || helpcols[j, help1] != 0) {
                        System.Threading.Thread.Sleep(1);
                        help1 = rnf.Next(1, 10);
                    }
                }
                map[i, j] = help1;
                helplines[i, help1]++;
                helpcols[j, help1]++;
                helpblock[0, help1]++;
            } else if (j == 3 || j == 4 || j == 5) {
                if (helplines[i, help1] != 0 || helpblock[1, help1] != 0 || helpcols[j, help1] != 0) {
                    while (helplines[i, help1] != 0 || helpblock[1, help1] != 0 || helpcols[j, help1] != 0) {
                        System.Threading.Thread.Sleep(1);
                        help1 = rnf.Next(1, 10);
                    }
                }
                map[i, j] = help1;
                helplines[i, help1]++;
                helpcols[j, help1]++;
                helpblock[1, help1]++;
            } else if (j == 6 || j == 7 || j == 8) {
                if (helplines[i, help1] != 0 || helpblock[2, help1] != 0 || helpcols[j, help1] != 0) {
                    while (helplines[i, help1] != 0 || helpblock[2, help1] != 0 || helpcols[j, help1] != 0) {
                        System.Threading.Thread.Sleep(1);
                        help1 = rnf.Next(1, 10);
                    }
                }
                map[i, j] = help1;
                helplines[i, help1]++;
                helpcols[j, help1]++;
                helpblock[2, help1]++;
            }
        } else if (i == 3 || i == 4 || i == 5) {
            if (j == 0 || j == 1 || j == 2) {
                if (helplines[i, help1] != 0 || helpblock[3, help1] != 0 || helpcols[j, help1] != 0) {
                    while (helplines[i, help1] != 0 || helpblock[3, help1] != 0 || helpcols[j, help1] != 0) {
                        System.Threading.Thread.Sleep(1);
                        help1 = rnf.Next(1, 10);
                    }
                }
                map[i, j] = help1;
                helplines[i, help1]++;
                helpcols[j, help1]++;
                helpblock[3, help1]++;
            } else if (j == 3 || j == 4 || j == 5) {
                if (helplines[i, help1] != 0 || helpblock[4, help1] != 0 || helpcols[j, help1] != 0) {
                    while (helplines[i, help1] != 0 || helpblock[4, help1] != 0 || helpcols[j, help1] != 0) {
                        System.Threading.Thread.Sleep(1);
                        help1 = rnf.Next(1, 10);
                    }
                }

                map[i, j] = help1;
                helplines[i, help1]++;
                helpcols[j, help1]++;
                helpblock[4, help1]++;
            } else if (j == 6 || j == 7 || j == 8) {
                if (helplines[i, help1] != 0 || helpblock[5, help1] != 0 || helpcols[j, help1] != 0) {
                    while (helplines[i, help1] != 0 || helpblock[5, help1] != 0 || helpcols[j, help1] != 0) {
                        System.Threading.Thread.Sleep(1);
                        help1 = rnf.Next(1, 10);
                    }
                }

                map[i, j] = help1;
                helplines[i, help1]++;
                helpcols[j, help1]++;
                helpblock[5, help1]++;

            }
        } else if (i == 6 || i == 7 || i == 8) {
            if (j == 0 || j == 1 || j == 2) {
                if (helplines[i, help1] != 0 || helpblock[6, help1] != 0 || helpcols[j, help1] != 0) {
                    while (helplines[i, help1] != 0 || helpblock[6, help1] != 0 || helpcols[j, help1] != 0) {
                        System.Threading.Thread.Sleep(1);
                        help1 = rnf.Next(1, 10);
                    }
                }

                map[i, j] = help1;
                helplines[i, help1]++;
                helpcols[j, help1]++;
                helpblock[6, help1]++;
            } else if (j == 3 || j == 4 || j == 5) {
                if (helplines[i, help1] != 0 || helpblock[7, help1] != 0 || helpcols[j, help1] != 0) {
                    while (helplines[i, help1] != 0 || helpblock[7, help1] != 0 || helpcols[j, help1] != 0) {
                        System.Threading.Thread.Sleep(1);
                        help1 = rnf.Next(1, 10);
                    }
                }

                map[i, j] = help1;
                helplines[i, help1]++;
                helpcols[j, help1]++;
                helpblock[7, help1]++;
            } else if (j == 6 || j == 7 || j == 8) {
                if (helplines[i, help1] != 0 || helpblock[8, help1] != 0 || helpcols[j, help1] != 0) {
                    while (helplines[i, help1] != 0 || helpblock[8, help1] != 0 || helpcols[j, help1] != 0) {
                        System.Threading.Thread.Sleep(1);
                        help1 = rnf.Next(1, 10);
                    }
                }

                map[i, j] = help1;
                helplines[i, help1]++;
                helpcols[j, help1]++;
                helpblock[8, help1]++;
            }
        }
    }
}

PrintSudoku(map);
int[,]helpblock=newint[10,10];
int[,]帮助热线=新int[10,10];
int[,]helpcols=新的int[10,10];
对于(int i=0;i<10;i++){
对于(int j=0;j<10;j++){
求助热线[i,j]=0;
helpcols[i,j]=0;
帮助块[i,j]=0;
}
}
随机rnf=新随机();
int help1;
对于(int i=0;i<9;i++){
对于(int j=0;j<9;j++){
help1=rnf.Next(1,10);
如果(i==0 | | i==1 | | i==2){
如果(j==0 | | j==1 | | j==2){
如果(帮助热线[i,帮助1]!=0 | |帮助阻止[0,帮助1]!=0 | |帮助热线[j,帮助1]!=0){
while(helplines[i,help1]!=0 | | helpblock[0,help1]!=0 | | helpcols[j,help1]!=0){
系统线程线程睡眠(1);
help1=rnf.Next(1,10);
}
}
map[i,j]=help1;
帮助热线[i,help1]+;
helpcols[j,help1]+;
帮助块[0,帮助1]+;
}else如果(j==3 | | j==4 | | j==5){
如果(帮助热线[i,help1]!=0 | |帮助阻止[1,help1]!=0 | |帮助热线[j,help1]!=0){
while(helplines[i,help1]!=0 | | helpblock[1,help1]!=0 | | helpcols[j,help1]!=0){
系统线程线程睡眠(1);
help1=rnf.Next(1,10);
}
}
map[i,j]=help1;
帮助热线[i,help1]+;
helpcols[j,help1]+;
helpblock[1,help1]+;
}else如果(j==6 | | j==7 | | j==8){
如果(帮助热线[i,help1]!=0 | |帮助阻止[2,help1]!=0 | |帮助热线[j,help1]!=0){
while(helplines[i,help1]!=0 | | helpblock[2,help1]!=0 | | helpcols[j,help1]!=0){
系统线程线程睡眠(1);
help1=rnf.Next(1,10);
}
}
map[i,j]=help1;
帮助热线[i,help1]+;
helpcols[j,help1]+;
helpblock[2,help1]+;
}
}else如果(i==3 | | i==4 | | i==5){
如果(j==0 | | j==1 | | j==2){
if(帮助热线[i,help1]!=0 | |帮助块[3,help1]!=0 | |帮助列表[j,help1]!=0){
while(helplines[i,help1]!=0 | | helpblock[3,help1]!=0 | | helpcols[j,help1]!=0){
系统线程线程睡眠(1);
help1=rnf.Next(1,10);
}
}
map[i,j]=help1;
帮助热线[i,help1]+;
helpcols[j,help1]+;
helpblock[3,help1]+;
}else如果(j==3 | | j==4 | | j==5){
如果(帮助热线[i,help1]!=0 | |帮助阻止[4,help1]!=0 | |帮助热线[j,help1]!=0){
while(helplines[i,help1]!=0 | | helpblock[4,help1]!=0 | | helpcols[j,help1]!=0){
系统线程线程睡眠(1);
help1=rnf.Next(1,10);
}
}
map[i,j]=help1;
帮助热线[i,help1]+;
helpcols[j,help1]+;
helpblock[4,help1]+;
}else如果(j==6 | | j==7 | | j==8){
如果(帮助热线[i,help1]!=0 | |帮助阻止[5,help1]!=0 | |帮助热线[j,help1]!=0){
while(helplines[i,help1]!=0 | | helpblock[5,help1]!=0 | | helpcols[j,help1]!=0){
系统线程线程睡眠(1);
help1=rnf.Next(1,10);
}
}
map[i,j]=help1;
帮助热线[i,help1]+;
helpcols[j,help1]+;
helpblock[5,help1]+;
}
}else如果(i==6 | | i==7 | | i==8){
如果(j==0 | | j==1 | | j==2){
如果(帮助热线[i,help1]!=0 | |帮助阻止[6,help1]!=0 | |帮助热线[j,help1]!=0){
while(helplines[i,help1]!=0 | | helpblock[6,help1]!=0 | | helpcols[j,help1]!=0){
系统线程线程睡眠(1);
help1=rnf.Next(1,10);
}
}
map[i,j]=help1;
帮助热线[i,help1]+;
helpcols[j,help1]+;
helpblock[6,help1]+;
}else如果(j==3 | | j==4 | | j==5){
如果(帮助热线[i,help1]!=0 | |帮助阻止[7,help1]!=0 | |帮助热线[j,help1]!=0){
while(helplines[i,help1]!=0 | | helpblock[7,help1]!=0 | | helpcols[j,help1]!=0){
系统线程线程睡眠(1);
help1=rnf.Next(1,10);
}
}
map[i,j]=help1;
帮助热线[i,help1]+;
helpcols[j,help1]+;
helpblock[7,help1]+;
}else如果(j==6 | | j==7 | | j==8){
如果(帮助热线[i,help1]!=0 | |帮助阻止[8,help1]!=0 | |帮助热线[j,help1]!=0){
while(helplines[i,help1]!=0 | | helpblock[8,help1]!=0 | | helpcols[j,help1]!=0){
系统线程线程睡眠(1);
help1=rnf.Next(1,10);
}
}
map[i,j]=help1;
赫尔
while (helplines[i, help1] != 0 || helpblock[5, help1] != 0 || helpcols[j, help1] != 0) {
    System.Threading.Thread.Sleep(1);
    help1 = rnf.Next(1, 10);
}
if (i == 0 || i == 1 || i == 2) {
    if (j == 0 || j == 1 || j == 2) {
  | 0 1 2 | 3 4 5 | 6 7 8 |
--+-------+-------+--------
0 |       |       |       |
1 |  [0]  |  [1]  |  [2]  |
2 |       |       |       |
--+-------+-------+--------
3 |       |       |       |
4 |  [3]  |  [4]  |  [5]  |
5 |       |       |       |
--+-------+-------+--------
6 |       |       |       |
7 |  [6]  |  [7]  |  [8]  |
8 |       |       |       |
---------------------------
int block = ((row / 3) * 3) + (column / 3);
int[,] blocks = new int[10, 10];
int[,] rows = new int[10, 10];
int[,] columns = new int[10, 10];

for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 10; j++)
    {
        rows[i, j] = 0;
        columns[i, j] = 0;
        blocks[i, j] = 0;
    }
}

Random rnd = new Random();
int randomNumber;

for (int row = 0; row < 9; row++)
{
    for (int column = 0; column < 9; column++)
    {
        int block = ((row / 3) * 3) + (column / 3);
        randomNumber = rnd.Next(1, 10);                    

        while (rows[row, randomNumber] != 0 || 
            blocks[block, randomNumber] != 0 || 
            columns[column, randomNumber] != 0)
        {
            System.Threading.Thread.Sleep(1);
            randomNumber = rnd.Next(1, 10);
        }

        map[row, column] = randomNumber;
        rows[row, randomNumber]++;
        columns[column, randomNumber]++;
        blocks[block, randomNumber]++;
    }
}
static void Main()
{
    int[,] map = new int[9, 9];
    var blocks = new Dictionary<int, List<int>>();
    var rows = new Dictionary<int, List<int>>();
    var columns = new Dictionary<int, List<int>>();

    // initialize lists of available numbers
    for (int i = 0; i < 9; i++)
    {
        blocks.Add(i, new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
        rows.Add(i, new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
        columns.Add(i, new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
    }

    Random rnd = new Random();
    int failCount = 0;

    // For each row
    for (int row = 0; row < 9; row++)
    {
        // For each column in this row
        for (int column = 0; column < 9; column++)
        {
            // Calculate block based on current row and column
            int block = ((row / 3) * 3) + (column / 3);

            // Get set of available numbers for this cell by getting the intersection
            // of available numbers for the row, column, and block
            var availableItems = 
                rows[row].Intersect(
                    columns[column].Intersect(
                        blocks[block])).ToList();

            // If we reach a point where there are no availableItems, then this is
            // not a valid Sudoku pattern. Reset everything and try again
            if (availableItems.Count == 0)
            {
                failCount++;
                Console.WriteLine($"Failed {failCount} times. Trying again...");

                // Reset to the first cell
                row = 0;
                column = 0;
                block = 0;

                // Initialize data
                for (int i = 0; i < 9; i++)
                {
                    blocks[i] = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                    rows[i] = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                    columns[i] = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                }
                availableItems = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            }

            // Grab a number from the available numbers by choosing a random index
            var randomNumber = availableItems[rnd.Next(0, availableItems.Count)];

            // Update our map with this item
            map[row, column] = randomNumber;

            // Remove this item from our lists
            rows[row].Remove(randomNumber);
            columns[column].Remove(randomNumber);
            blocks[block].Remove(randomNumber);
        }
    }

    // Print our Sudoku map:
    Console.WriteLine(new string('-', 25));

    for(int i = 0; i < 9; i++)
    {
        Console.Write("|");

        for (int j = 0; j < 9; j++)
        {
            Console.Write($" {map[i, j]}");
            if ((j + 1) % 3 == 0) Console.Write(" |");
        }

        Console.WriteLine();

        if ((i + 1) % 3 == 0) Console.WriteLine(new string('-', 25));
    }

    Console.Write("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}