// Check to see if a solution is found static bool FoundSolution() { if (row == endRow && col == endCol) { Printer.print_sudoku(); foundsolution = true; return(true); } return(false); }
// Recursive backtracking method that uses forward checking with the most constraining variable public void ForwardCheck() { // Check if the most constraining variable is empty if (MCV == null) { // Create a new location with a high domain size Location l = new Location(); l.size = 900; //Check for each square in the sudoku which square has the lowest domainsize foreach (Square s in sudoku) { if (!Program.unchangable[s.row, s.column]) { if (s.domainSize <= l.size) { l.row = s.row; l.column = s.column; l.size = s.domainSize; if (l.size == 1) { break; } } } } // Assign the location to the most contraining variable MCV = l; } // If the most constraining variable has a size of 900, the sudoku is solved and print the solution if (MCV.size > 800) { //PrintSolution(); Program.sudokufc = sudoku; Printer.print_sudoku(); return; } Program.recursivecalls++; // Copy this grid to make changes in the child child = new Sudoku_Grid(); child = Clone(this); child.parent = this; // Get location of most constraining square int row = MCV.row; int col = MCV.column; // Check if index has exceeded the amount of variables there are if (currentVariableIndex < sudoku[row, col].variables.Count) { int variable = sudoku[row, col].variables[currentVariableIndex]; // Set the square to this variable child.sudoku[row, col].number = variable; // If there are no empty domains move next with the child if (child.MakeConsistent(row, col)) { child.ForwardCheck(); } // Else increase the variable index to try the next possible variable else { currentVariableIndex++; ForwardCheck(); } } // If it has exceeded the amount of variables go back to the parent else { parent.currentVariableIndex++; parent.ForwardCheck(); } }