Пример #1
0
        private bool BruteForceSolve()
        {
            if (SortedCandidates.Count == 0)
            {
                return(Board.IsSolved());
            }
            TekField Field0 = SortedCandidates[0];

            if (Field0.PossibleValues.Count == 0)
            {
                return(Board.IsSolved());
            }
            for (int i = 0; i < Field0.PossibleValues.Count; i++)
            {
                SetFieldValue(Field0, Field0.PossibleValues[i]);
                if (BruteForceSolve())
                {
                    return(true);
                }
                else // backtrack
                {
                    SetFieldValue(Field0, 0);
                }
            } // if we get here, this branch has no solution
            return(false);
        }
Пример #2
0
 private HeuristicAction _tryHeuristic(TekHeuristic heuristic, TekBoard board)
 {
     if (heuristic != null)
     {
         try
         {
             temStoredResults.Add(new TekHeuristicResult(heuristic));
             heuristic.ExecuteAction(temMoves);
             return(HeuristicAction.haNone);
         }
         catch (ETekFieldInvalid)
         {
             return(HeuristicAction.haExcludeValue);
         }
     }
     else if (board.IsSolved())
     {
         foreach (TekHeuristicResult result in temStoredResults)
         {
             heuristics.PrecomputedResults.Add(new TekHeuristicResult(result));
         }
         return(HeuristicAction.haSetValue);
     }
     else
     {
         return(HeuristicAction.haImpossible);
     }
 }
Пример #3
0
        public bool HeuristicSolve(TekBoard board, TekMoves moves)
        {
            TekHeuristic heuristic = FindHeuristic(board);
            bool         Paused    = false;

            while (heuristic != null && !Paused)
            {
                AfterHeuristicFoundHandler?.Invoke(heuristic);
                StoreResult(heuristic);
                if (BeforeExecutionHandler != null && !BeforeExecutionHandler(heuristic))
                {
                    return(false);
                }
                heuristic.ExecuteAction(moves);
                AfterExecutionHandler?.Invoke(heuristic);
                heuristic = FindHeuristic(board);
            }
            return(board.IsSolved());
        }