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 ClockSudokuPuzzle ApplyConstraints(int cellIndex, int value) { ClockSudokuPuzzle puzzle = (ClockSudokuPuzzle)this.Clone(); puzzle.Cells[cellIndex].value = value; if (value <= 9) { puzzle.Cells[cellIndex].stringValue = value.ToString(); } else if (value == 10) { puzzle.Cells[cellIndex].stringValue = "A"; } else if (value == 10) { puzzle.Cells[cellIndex].stringValue = "B"; } else { puzzle.Cells[cellIndex].stringValue = "C"; } foreach (int peerIndex in Peers[cellIndex]) { var newCandidates = puzzle.Cells[peerIndex].candidates.Except(new List <int> { value }); puzzle.Cells[peerIndex].candidates = newCandidates.ToList <int>(); if (!newCandidates.Any()) { return(null); } puzzle.Cells[cellIndex].candidates = new List <int>(Enumerable.Repeat(0, 12)); } return(puzzle); }
public static List <ClockSudokuPuzzle> MultiSolve(ClockSudokuPuzzle input, int MaximumSolutions = -1) { var Solutions = new List <ClockSudokuPuzzle>(); input.SolveUsingGuess(p => { Solutions.Add(p); return(Solutions.Count() < MaximumSolutions || MaximumSolutions == -1); }); return(Solutions); }
public object Clone() { var clone = new ClockSudokuPuzzle(); foreach (ClockCell cell in this.Cells) { clone.Cells.Add((ClockCell)cell.Clone()); } return(clone); }
public static ClockSudokuPuzzle RandomGrid() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 72; i++) { sb.Append('0'); } ClockSudokuPuzzle puzzle = new ClockSudokuPuzzle(sb.ToString()); var rand = new Random(); while (true) { int[] UnsolvedCellIndexes = puzzle.Cells .Select((cands, index) => new { cands, index }) //Project to a new sequence of candidates and index (an anonymous type behaving like a tuple) .Where(t => t.cands.candidates.Count >= 2) //Filter to cells with at least 2 candidates .Select(u => u.index) //Project the tuple to only the index .ToArray(); int cellIndex = UnsolvedCellIndexes[rand.Next(UnsolvedCellIndexes.Length)]; int candidateValue = puzzle.Cells[cellIndex].candidates[rand.Next(puzzle.Cells[cellIndex].candidates.Count)]; ClockSudokuPuzzle workingPuzzle = puzzle.PlaceValue(cellIndex, candidateValue); if (workingPuzzle != null) { var Solutions = MultiSolve(workingPuzzle, 2); switch (Solutions.Count) { case 0: continue; case 1: //Console.WriteLine(Solutions[0].ToString()); return(Solutions.Single()); default: puzzle = workingPuzzle; break; } } } }
public bool isEqual(ClockSudokuPuzzle puzzle) { foreach (ClockCell cell in this.Cells) { if ((cell.isFixed == puzzle.Cells[cell.index].isFixed) && (cell.value == puzzle.Cells[cell.index].value) && (cell.candidates.Count == puzzle.Cells[cell.index].candidates.Count)) { for (int i = 0; i < cell.candidates.Count; i++) { if (cell.candidates[i] != puzzle.Cells[cell.index].candidates[i]) { return(false); } } } else { return(false); } } return(true); }
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); }