예제 #1
0
 public TekHeuristic(string description, HeuristicAction action) : this()
 {
     _description    = description;
     _action         = action;
     HeuristicFields = new List <TekField>();
     AffectedFields  = new List <TekField>();
     HeuristicValues = new List <int>();
     Enabled         = true;
 }
예제 #2
0
        private HeuristicAction TryValue(TekBoard board, TekField field, int value)
        {
            bool            prev   = board.EatExceptions;
            HeuristicAction result = HeuristicAction.haNone;

            try
            {
                board.EatExceptions = false;
                this.Enabled        = false; // make sure FindHeuristic doesnt call this recursively!
                if ((result = _tryValue(field, value)) == HeuristicAction.haNone)
                {
                    temStoredResults.Clear();
                    temMoves.TakeSnapshot(_ssDescription(field));
                    try {
                        do
                        {
                            try
                            {
                                result = _tryHeuristic(heuristics.FindHeuristic(board), board);
                            }
                            catch (ETekFieldInvalid)
                            {
                                result = HeuristicAction.haImpossible;
                            }
                        } while (result == HeuristicAction.haNone);
                    }
                    finally
                    {
                        temMoves.RestoreSnapshot(_ssDescription(field));
                        _ssIndex++;
                    }
                }
            }
            finally
            {
                board.EatExceptions = prev;
                this.Enabled        = true;
                field.Value         = 0; // backtracking
            }
            return(result);
        }
예제 #3
0
        public override bool HeuristicApplies(TekBoard board, TekField field)
        {
            HeuristicAction action = HeuristicAction.haNone;
            HeuristicAction found  = HeuristicAction.haNone;

            HeuristicValues.Clear();                                    // clear any values already set
            foreach (int value in new List <int>(field.PossibleValues)) // can't use the list directly in foreach
            {
                switch (action = TryValue(board, field, value))
                {
                case HeuristicAction.haSetValue:
                    HeuristicValues.Clear();
                    AddValue(value);
                    found = action;
                    break;

                case HeuristicAction.haExcludeValue:
                    AddValue(value);
                    found = action;
                    break;
                }
                if (action == HeuristicAction.haSetValue) // solution found, so we can stop
                {
                    break;
                }
                // if you haven't found a solution, it doesnt hurt to continue maybe you can find a solution
                // or maybe you can add a second value to exclude
            }
            if (found == HeuristicAction.haSetValue || found == HeuristicAction.haExcludeValue)
            {
                SetHeuristicAction(found);
                AddHeuristicField(field);
                AddAffectedField(field);
            }
            return(AffectedFields.Count > 0);
        }
예제 #4
0
 protected void SetHeuristicAction(HeuristicAction value)
 {
     _action = value;
 }