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; }
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); }
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); }
protected void SetHeuristicAction(HeuristicAction value) { _action = value; }