private bool FillGrid(int[,] grid)
 {
     for (int i = 0; i < grid.Length; i++)
     {
         int row = (int)Math.Floor(i / 9.0);
         int col = i % 9;
         if (grid[row, col] == 0)
         {
             var numList = GetRandomNumberList();
             foreach (var val in numList)
             {
                 if (_validator.ValidPositionForValue(val, row, col, grid))
                 {
                     grid[row, col] = val;
                     if (_validator.GridIsComplete(grid))
                     {
                         return(true);
                     }
                     else
                     {
                         if (FillGrid(grid))
                         {
                             return(true);
                         }
                     }
                 }
             }
             // could not find a valid number
             // back-propagate, return and make a new try with the previous cell
             grid[row, col] = 0;
             return(false);
         }
     }
     return(false);
 }
Exemplo n.º 2
0
 private bool _SolveGrid(int[,] grid)
 {
     for (int i = 0; i < grid.Length; i++)
     {
         int row = (int)Math.Floor(i / 9.0);
         int col = i % 9;
         if (grid[row, col] == 0)
         {
             foreach (var val in numberList)
             {
                 if (_validator.ValidPositionForValue(val, row, col, grid))
                 {
                     grid[row, col] = val;
                     if (_validator.GridIsComplete(grid))
                     {
                         // detect one found solution
                         NumSolutions++;
                         if (NumSolutions > 1)
                         {
                             return(true);
                         }
                     }
                     else
                     {
                         if (_SolveGrid(grid))
                         {
                             return(true);
                         }
                     }
                 }
             }
             // Could not find a valid value, back-propagate one step
             grid[row, col] = 0;
             break;
         }
     }
     return(false);
 }