public static ClockSudokuPuzzle GenerateSudokuPuzzle2(ClockSudokuPuzzle fullGrid) { ClockSudokuPuzzle puzzle = new ClockSudokuPuzzle(fullGrid.toStringList()); Random rand = new Random(); int count = 0; while (count < 71) { /////// perform digging holes /////// string stringpuzzle = puzzle.toStringList(); List <int> Indexes = (from cell in puzzle.Cells where cell.isFixed select cell.index).ToList <int>(); int randomCellIndex = rand.Next(Indexes.Count); StringBuilder sb = new StringBuilder(stringpuzzle); sb[Indexes[randomCellIndex]] = '0'; string newstringpuzzle = sb.ToString(); //Console.WriteLine(newstringpuzzle); ClockSudokuPuzzle temppuzzle = new ClockSudokuPuzzle(newstringpuzzle); var solutions = ClockSudokuPuzzle.MultiSolve(temppuzzle); if (solutions.Count == 1) { puzzle = temppuzzle; //if (temppuzzle.isEqual(puzzle)) // break; } //////////////////////////////////// count++; } return(puzzle); }
public static ClockSudokuPuzzle GenerateSudokuPuzzle(ClockSudokuPuzzle fullGrid) { ClockSudokuPuzzle puzzle = new ClockSudokuPuzzle(fullGrid.toStringList()); Random rand = new Random(); int count = 0; bool Continue = false; string stringpuzzle = puzzle.toStringList(); StringBuilder sbb = new StringBuilder(stringpuzzle); stringpuzzle = sbb.ToString(); puzzle = new ClockSudokuPuzzle(stringpuzzle); while (!Continue && puzzle.NumberOfEmptyCells() <= 41) { stringpuzzle = puzzle.toStringList(); List <int> Indexes = (from cell in puzzle.Cells where cell.isFixed select cell.index).ToList <int>(); Continue = true; List <int> possibleIndexes = new List <int>(); List <int> possibleRates = new List <int>(); foreach (int index in Indexes) { StringBuilder sb = new StringBuilder(stringpuzzle); sb[index] = '0'; string newstringpuzzle = sb.ToString(); ClockSudokuPuzzle temppuzzle = new ClockSudokuPuzzle(newstringpuzzle); var solutions = ClockSudokuPuzzle.MultiSolve(temppuzzle); if (solutions.Count == 1) { count++; int weight = 0; int currentRate = ClockSudokuHumanSolver.ratePuzzle(temppuzzle, out weight); Console.WriteLine(String.Format("empty cells={0} , rate={1}", puzzle.NumberOfEmptyCells(), currentRate.ToString() )); if (currentRate != 4) { possibleIndexes.Add(index); possibleRates.Add(currentRate); } } } if (possibleIndexes.Count > 0) { Continue = false; int MaxRate = possibleRates.Max(); List <int> tempRates = new List <int>(); List <int> tempIndexes = new List <int>(); for (int i = 0; i < possibleRates.Count; i++) { if (possibleRates[i] == MaxRate) { tempRates.Add(possibleRates[i]); tempIndexes.Add(possibleIndexes[i]); } } int randomCellIndex = rand.Next(tempRates.Count); StringBuilder sb2 = new StringBuilder(stringpuzzle); sb2[tempIndexes[randomCellIndex]] = '0'; string newstringpuzzle2 = sb2.ToString(); puzzle = new ClockSudokuPuzzle(newstringpuzzle2); } } Console.WriteLine("Fill count=" + puzzle.toStringList().ToList().FindAll(ch => ch != '0').Count); return(puzzle); }