Hi Mason,
- Ensure that each recursive call correctly accumulates solutions and passes them up the call stack.
- Make sure that solutions are added to the cellSolutions array in the correct format and that the recursive function returns this array properly.
Here's a revised version of solveSudoku function with comments explaining each step:
function cellSolutions = solveSudoku(PuzzleA)
cellSolutions = {}; % Initialize the cell array to store solutions
if ~reject(PuzzleA) % Check if the current puzzle configuration is valid
if accept(PuzzleA) % Check if the puzzle is completely solved
disp('solution found');
cellSolutions{end+1} = PuzzleA; % Add the completed puzzle to solutions
return; % Return as we've found a solution
end
[nP, Idx] = firstChild(PuzzleA); % Get the first child puzzle and its index
while ~isempty(nP) % Continue until there are no more children
possibleSolutions = solveSudoku(nP); % Recursively solve the child puzzle
if ~isempty(possibleSolutions)
% Append all solutions found to the current cellSolutions
cellSolutions = [cellSolutions, possibleSolutions];
end
nP = nextChild(nP, Idx); % Move to the next child
end
end
end
Hope this helps.
