function cspBacktrack(assignment, value, csp, solution) {
if (isAssignmentComplete(assignment, csp)) {
if (value > solution.bestValue ?? 0) {
solution.bestValue = value;
solution.bestAssignment = assignment;
}
return;
}
const nextVariable = chooseNextVariable(assignment, csp);
const possibleValues = orderPossibleValues(csp, nextVariable);
for (const possibleValue of possibleValues) {
const newAssignment = { ...assignment };
newAssignment[nextVariable] = possibleValue;
const newValue = evaluateAssignment(newAssignment, csp);
if (newValue <= 0) continue;
cspBacktrack(newAssignment, newValue, csp, solution);
}
}