示例#1
0
 public void Generate(HintSelections hints)
 {
     // Take any solution for a clear grid
     ClearGrid();
     Candidate[] soln = solver.RandomSolution();
     if (soln == null)
     {
         return;
     }
     int[,] ans = new int[Cells, Cells];
     foreach (Candidate k in soln)
     {
         SudokuCandidate sc = (SudokuCandidate)k;
         ans[sc.x, sc.y] = sc.n;
     }
     // Add clues until soluable
     PlayMode = PlayModes.EditCell;
     while (true)
     {
         SolveResult sr = solver.DoLogicalSolve(this, hints);
         if (sr == SolveResult.SingleSolution)
         {
             break;
         }
         int x = Utils.Utils.Rnd(Cells);
         int y = Utils.Utils.Rnd(Cells);
         if (sr == SolveResult.NoSolutions)
         {
             ClearCell(x, y);
             ClearCell(8 - x, 8 - y);
         }
         else
         {
             SetCell(x, y, ans[x, y]);
             SetCell(Cells - x - 1, Cells - y - 1, ans[Cells - x - 1, Cells - y - 1]);
         }
     }
     // Remove any clues where it remains soluable
     for (int x = 0; x < Cells; ++x)
     {
         for (int y = 0; y < Cells; ++y)
         {
             ClearCell(x, y);
             if (solver.DoLogicalSolve(this, hints) != SolveResult.SingleSolution)
             {
                 SetCell(x, y, ans[x, y]);
             }
         }
     }
     Setup();
 }