private bool _FillMissingNumbers(int[,] board) { UnfilledPosition position = new UnfilledPosition(-1, -1); position = _GetMissingPosition(board); //If there is no missing number, the problem is solved! if (!IndeedMissing(position)) { return(true); } for (int numberToBeFilled = 1; numberToBeFilled < 10; numberToBeFilled++) { if (CanThisNumberFilled(board, position.GetRowPosition(), position.GetColumnPosition(), numberToBeFilled)) { board[position.GetRowPosition(), position.GetColumnPosition()] = numberToBeFilled; //Recurse for other missing numbers if (_FillMissingNumbers(board)) { return(true); } else { //Backtrack -> Reset the filled number board[position.GetRowPosition(), position.GetColumnPosition()] = 0; } } } return(false); }
private UnfilledPosition _GetMissingPosition(int[,] board) { UnfilledPosition unfilledPosition = new UnfilledPosition(-1, -1); for (int index = 0; index < 9; index++) { for (int secIndex = 0; secIndex < 9; secIndex++) { if (board[index, secIndex] == 0) { unfilledPosition.SetRowPosition(index); unfilledPosition.SetColumnPosition(secIndex); break; } } //Break out of outer for if missing position is found if (IndeedMissing(unfilledPosition)) { break; } } return(unfilledPosition); }
private bool IndeedMissing(UnfilledPosition unfilledPosition) { return(unfilledPosition.GetRowPosition() != -1 && unfilledPosition.GetColumnPosition() != -1); }