samedi 1 avril 2017

While loop stuck at random points on the code

i need help, i had my friend to help me debug this code, and we found that the code stops at random points on the code, sometimes on first loop, sometimes on last loop and so on. the code is just to make a 9x9 sudoku board with all the rules applied. when i tested each block seperatly it worked and printed a well done sudoku board, but the problem here is that is gets stuck somehow. help will be appreciated.

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




Aucun commentaire:

Enregistrer un commentaire