Example #1
0
        private bool _TrySolve(SquareTracker <TPuzzle> tracker)
        {
            var puzzle = tracker.Puzzle;

            if (puzzle.NumEmptySquares == 0)
            {
                return(true);
            }
            Coordinate c = tracker.GetBestCoordinateToGuess();
            Span <int> possibleValues = stackalloc int[puzzle.Size];
            int        numPossible    = tracker.PopulatePossibleValues(in c, possibleValues);

            for (int i = 0; i < numPossible; ++i)
            {
                int possibleValue = possibleValues[i];
                if (tracker.TrySet(in c, possibleValue))
                {
                    if (_TrySolve(tracker))
                    {
                        return(true);
                    }
                    tracker.UnsetLast();
                }
            }
            return(false);
        }
Example #2
0
        private bool _TrySolve()
        {
            if (_tracker.Puzzle.NumEmptySquares == 0)
            {
                return(true);
            }
            Coordinate c = _tracker.GetBestCoordinateToGuess();

            foreach (int possibleValue in _tracker.GetPossibleValues(in c))
            {
                if (_tracker.TrySet(in c, possibleValue))
                {
                    if (_TrySolve())
                    {
                        return(true);
                    }
                    _tracker.UnsetLast();
                }
            }
            return(false);
        }
Example #3
0
        private bool _TrySolveRandomly(SquareTracker <TPuzzle> tracker, Random random)
        {
            if (tracker.Puzzle.NumEmptySquares == 0)
            {
                return(true);
            }
            Coordinate c = tracker.GetBestCoordinateToGuess();
            Span <int> possibleValues = stackalloc int[tracker.Puzzle.Size];
            int        numPossible    = tracker.PopulatePossibleValues(in c, possibleValues);

            while (numPossible > 0)
            {
                int possibleValue = Spans.PopRandom(random, possibleValues[0..numPossible--]);
Example #4
0
        private static SolveStats _TryAllSolutions(SquareTracker tracker)
        {
            if (tracker.Puzzle.NumEmptySquares == 0)
            {
                return(new SolveStats()
                {
                    NumSolutionsFound = 1,
                });
            }
            Coordinate c = tracker.GetBestCoordinateToGuess();
            List <int>?possibleValues = tracker.GetPossibleValues(in c);

            if (possibleValues.Count == 1)
            {
                if (tracker.TrySet(in c, possibleValues[0]))
                {
                    return(_TryAllSolutions(tracker));
                }
                return(new SolveStats());
            }
            return(_TryAllSolutionsWithGuess(tracker, c, possibleValues));
        }