Esempio n. 1
0
        private bool DeleteField(Sudoku temp, ref int difficulty)
        {
            int i = Rnd.Next(0, 9); ;
            int j = Rnd.Next(0, 9); ;

            // select random not yet deleted field
            while (!temp.Fields[i][j].Filled)
            {
                i = Rnd.Next(0, 9);
                j = Rnd.Next(0, 9);
            }

            temp.Fields[i][j].Value = 0;
            temp.Fields[i][j].Filled = false;
            SudokuSolver Test = new SudokuSolver();
            int Difficulty = difficulty;
            // check the desired characteristics
            Sudoku Temp = Test.Solve(temp.Copy(), true, Difficulty);
            if (Temp == null)
                return false;
            temp.Difficulty = Temp.Difficulty;
            if (Temp.NrSolutions > 1 || Temp.Difficulty > Difficulty)
                return false;
            else
                return true;
        }
Esempio n. 2
0
 private void BruteForce(List<Sudoku.Field> ToDo, int pos, Sudoku Temp, bool count)
 {
     if (Finished)
         return;
     for (int i = 0; i < ToDo[pos].Candidates.Count; i++)
     {
         Sudoku Copied = Temp.Copy();
         Copied.Fields[ToDo[pos].i][ToDo[pos].j].Filled = true;
         Copied.Fields[ToDo[pos].i][ToDo[pos].j].NewFound = false;
         // place a random value and ...
         Copied.Fields[ToDo[pos].i][ToDo[pos].j].Value = ToDo[pos].Candidates[i];
         bool Valid = true;
         // ... check if solution is still valid
         foreach (Sudoku.Field f in Copied.GetGroup(ToDo[pos].i, ToDo[pos].j))
         {
             if (f.Filled && f.Value == Copied.Fields[ToDo[pos].i][ToDo[pos].j].Value)
             {
                 Valid = false;
                 break;
             }
         }
         if (Valid)
         {   // if yes, go to the next position
             if (pos % 5 == 0)
                 Grade0();
             if (pos < ToDo.Count - 1)
                 BruteForce(ToDo, pos + 1, Copied, count);
             else
             {   // or otherwise, we are done
                 if (!count)
                 {
                     Finished = true;
                     Input = Copied;
                     Remaining = 0;
                 }
                 else
                 {   // if we want to count the solutions, we do not stop after having found the first one
                     SolCounter++;
                     Remaining = 0;
                     if (SolCounter > 1)
                     {
                         Finished = true;
                     }
                 }
             }
         }
     }
 }
Esempio n. 3
0
 private void BruteForce(List<Sudoku.Field> ToDo, int pos, Sudoku Temp)
 {
     // the brute force implementation for creating complete sudokus
     if (Finished)
         return;
     for (int i = 1; i <= 9; i++)
     {
         Sudoku Copied = Temp.Copy();
         Copied.Fields[ToDo[pos].i][ToDo[pos].j].Filled = true;
         Copied.Fields[ToDo[pos].i][ToDo[pos].j].Value = Rnd.Next(1, 10);
         bool Valid = true;
         foreach (Sudoku.Field f in Copied.GetGroup(ToDo[pos].i, ToDo[pos].j))
         {
             if (f.Filled && f.Value == Copied.Fields[ToDo[pos].i][ToDo[pos].j].Value)
             {
                 Valid = false;
                 break;
             }
         }
         if (Valid)
         {
             if (pos < ToDo.Count - 1)
                 BruteForce(ToDo, pos + 1, Copied);
             else
             {
                 Finished = true;
                 Result = Copied;
             }
         }
     }
 }