private static void DoTheGuess() { bool broken = false; int attempt = 1; for (int i = 2; !broken; i++) { foreach (SudokuPiece piece in Sudoku.Pieces) { if (!piece.IsFilled) { if (piece.PossibleNumbers.Count == i) { Backup.SetBackup(piece); piece.InsertValue(piece.PossibleNumbers.First()); broken = true; break; } } } } while (!Solve()) { SudokuPiece changedPiece = Backup.RestoreLatestBackup(); changedPiece.InsertValue(changedPiece.PossibleNumbers.ElementAt(attempt)); attempt++; } Backup.DeleteLatestBackup(); }
public static void SetBackup(SudokuPiece changedPiece) { BackupData backup = new BackupData(Sudoku.PiecesFilled, changedPiece); for (int i = 0; i < 81; i++) { if (Sudoku.Pieces[i].IsFilled) { backup.PiecesFilled.Add(i, Sudoku.Pieces[i].Value); } else { List <string> possibleNumbers = new List <string>(); possibleNumbers.AddRange(Sudoku.Pieces[i].PossibleNumbers); backup.PiecesUnfilled.Add(i, possibleNumbers); } } BackupDataStack.Push(backup); }
public BackupData(int piecesToBackup, SudokuPiece changedPiece) { ChangedPiece = changedPiece; PiecesFilled = new Dictionary <int, string>(piecesToBackup); PiecesUnfilled = new Dictionary <int, List <string> >(81 - piecesToBackup); }