private static SolveStats _TryAllSolutions(SquareTracker <TPuzzle> tracker) { if (tracker.IsSolved) { return(new SolveStats() { NumSolutionsFound = 1 }); } (Coordinate c, int[]? possibleValues) = tracker.GetBestGuess(); if (possibleValues.Length == 1) { if (tracker.TrySet(in c, possibleValues[0])) { return(_TryAllSolutions(tracker)); } return(new SolveStats()); } return(_TryAllSolutionsWithGuess(in c, possibleValues, tracker)); }
private static bool _TrySolve(SquareTracker <TPuzzle> tracker) { if (tracker.IsSolved) { return(true); } (Coordinate c, int[]? possibleValues) = tracker.GetBestGuess(); foreach (int possibleValue in possibleValues) { if (tracker.TrySet(in c, possibleValue)) { if (_TrySolve(tracker)) { return(true); } tracker.UnsetLast(); } } return(false); }
private static bool _TrySolveRandomly(Random rand, SquareTracker <TPuzzle> tracker) { if (tracker.IsSolved) { return(true); } (Coordinate c, int[]? possibleValues) = tracker.GetBestGuess(); var possibleValuesList = new List <int>(possibleValues); while (possibleValuesList.Count > 0) { int possibleValue = possibleValuesList[rand.Next(0, possibleValuesList.Count)]; if (tracker.TrySet(in c, possibleValue)) { if (_TrySolveRandomly(rand, tracker)) { return(true); } tracker.UnsetLast(); } _ = possibleValuesList.Remove(possibleValue); } return(false); }