locked
Stackoverflow exception infinite loop RRS feed

  • Question

  • Hello guys,

    Getting a infinite loop in my code.. I hope someone can help me out here..

    static int[,] Sudoku_Solver(int value, int px, int py, int[,] field)
            {
                if (count == 2)
                {
    
                }
                else
                {
                    for (int i = 0; i < 9; i++)
                    {
                        for (int j = 0; j < 9; j++)
                        {
                            field[i, j] = fileText[i * size + j] - '0';
                        }
                    }
    
                    field[px, py] = value;
    
                    for (int x = 0; x < 9; x++)
                    {
                        for (int y = 0; y < 9; y++)
                        {
                            if (field[x, y] == 0)
                            {
                                for (int val = 1; val <= 9; val++)
                                {
                                    if (ChkMove(y, x, val, field))
                                    {
                                        Sudoku_Solver(val, x, y, field);
                                    }
                                }
                                return null;
                            }
                        }
                    }
                    for (int i = 0; i < 9; i++)
                    {
                        for (int j = 0; j < 9; j++)
                        {
                            Console.Write(field[j, i]);
                        }
                        Console.WriteLine();
                    }
                    Console.WriteLine("---------");
                    count = 1;
                }
                return field;
            }

    Hope this is enough to detect the problem.

    Pasibun

    Monday, June 23, 2014 6:50 PM

Answers

  • Solved this problem!

            static int[,] Sudoku_Solver(int value, int px, int py, int[,] field)
            {
                int[,] workField = new int[9, 9];
                if (count >= 1)
                {
                  //doe niks
                }
                else
                {
                    for (int i = 0; i < 9; i++)
                    {
                        for (int j = 0; j < 9; j++)
                        {
                            workField[j, i] = field[i, j] = fileText[i * size + j];
                        }
                    }
    
                    workField[px, py] = value;
    
                    for (int x = 0; x < 9; x++)
                    {
                        for (int y = 0; y < 9; y++)
                        {
                            if (workField[x, y] == 0)
                            {
                                for (int val = 1; val <= 9; val++)
                                {
                                    if (ChkMove(y, x, val, workField))
                                    {
                                        Sudoku_Solver(val, x, y, workField);
                                    }
                                }
                                return null;
                            }
                        }
                    }
                    for (int i = 0; i < 9; i++)
                    {
                        for (int j = 0; j < 9; j++)
                        {
                            Console.Write(workField[j, i]);
                        }
                        Console.WriteLine();
                    }
                    Console.WriteLine("---------");
                    count = count + 1;
                }
                return workField;
            }

    Monday, June 23, 2014 7:20 PM