public bool ComputerNextSolution() { if (IsFinished()) { return(true); } // next iteration PositionValues val = FindNext(); int[] ints = val.Values; if (ints.Length == 1) { // only one choice insert value - this solution is a success sudukoMap[val.Pos.row, val.Pos.col] = ints[0]; return(true); } // more than one solution - try from the start for (int i = 0; i < ints.Length; i++) { sudukoMap[val.Pos.row, val.Pos.col] = ints[i]; if (ComputerNextSolution()) { return(true); } // else delete and try next sudukoMap[val.Pos.row, val.Pos.col] = 0; } return(false); }
public PositionValues FindNext() { PositionValues returnVar = new PositionValues(new Position(), null); int minNo = 999; // number of possible values Position pos = new Position(); for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { pos.row = row; pos.col = col; int val = sudukoMap[row, col]; if (val == 0) { // no value in this posistion List <int> missingNumbers = this.GetMissingNumbers(pos); if (missingNumbers.Count < minNo) { // find the one with fewest alternatives optimal only one minNo = missingNumbers.Count; returnVar.Pos = pos; returnVar.Values = missingNumbers.ToArray(); } } // if one posistion with only one posssible value - pick that if (minNo == 1) { break; } } if (minNo == 1) { break; } } return(returnVar); }