I wrote this for “fun” after taking our intro AI class.
IAssignment RecursiveBacktracking(CSP csp, IAssignment assignment)
{
NumberOfSteps++;
if (NumberOfSteps > TimeoutNumberOfSteps)
{
throw new Exception($"Timed out after {TimeoutNumberOfSteps} steps.");
}
if (assignment.IsComplete())
{
return assignment;
}
string variable = SelectVariableMethod(assignment, csp);
foreach (int value in OrderValuesMethod(assignment, csp, variable))
{
if (assignment.IsVariableValueConsistent(variable, value))
{
assignment.Assign(variable, value);
IAssignment result = RecursiveBacktracking(csp, assignment);
if (result != default(IAssignment))
{
return result;
}
assignment.Unassign(variable);
}
}
return null;
}
This site is open source. Improve this page »