/// <summary> /// Generate sudoku to fill from full-filled sudoku /// </summary> /// <param name="sudoku"></param> /// <param name="notCheckedPositionsFromPrev"></param> /// <param name="difficultyPoints"></param> /// <param name="symmetry"></param> /// <param name="token">Cancellation token</param> /// <returns></returns> private static bool GenerateFromFull(Domain.Sudoku sudoku, IEnumerable <Tuple <int, int> > notCheckedPositionsFromPrev, int difficultyPoints, Enums.SymmetryType symmetry, CancellationToken token) { var notCheckedPositions = new List <Tuple <int, int> >(notCheckedPositionsFromPrev); while (notCheckedPositions.Count > 0) { token.ThrowIfCancellationRequested(); var removedCells = RemoveNext(sudoku, notCheckedPositions, symmetry); var solutions = Solver.GetTopNSolutions(sudoku, 2, Enums.TopType.Any); if (solutions.Count == 1 && solutions.First().DifficultyPoints > difficultyPoints) { sudoku.DifficultyPoints = solutions.First().DifficultyPoints; return(true); } if (solutions.Count > 1) { RecoverCells(sudoku, removedCells); return(false); } if (GenerateFromFull(sudoku, notCheckedPositions, difficultyPoints, symmetry, token)) { return(true); } RecoverCells(sudoku, removedCells); } return(false); }
private static List <Cell> RemoveNext(Domain.Sudoku sudoku, IList <Tuple <int, int> > notCheckedPositions, Enums.SymmetryType symmetry) { var randomPosition = Random.Next(0, notCheckedPositions.Count - 1); var currentCell = notCheckedPositions[randomPosition]; notCheckedPositions.RemoveAt(randomPosition); var row = currentCell.Item1; var column = currentCell.Item2; var removedValues = new List <Cell>() { new Cell(row, column, sudoku[row, column]) }; sudoku[row, column] = 0; if (symmetry == Enums.SymmetryType.Horizontal) { if (row == 4) { return(removedValues); } var symmetricRow = Domain.Sudoku.BigSide - 1 - row; removedValues.Add(new Cell(symmetricRow, column, sudoku[symmetricRow, column])); notCheckedPositions.Remove(Tuple.Create(symmetricRow, column)); sudoku[symmetricRow, column] = 0; } else if (symmetry == Enums.SymmetryType.Vertical) { if (column == 4) { return(removedValues); } var symmetricColumn = Domain.Sudoku.BigSide - 1 - column; removedValues.Add(new Cell(row, symmetricColumn, sudoku[row, symmetricColumn])); notCheckedPositions.Remove(Tuple.Create(row, symmetricColumn)); sudoku[row, symmetricColumn] = 0; } return(removedValues); }