Пример #1
0
        public static SudokuPuzzle RandomGrid(int size)
        {
            SudokuPuzzle puzzle = new SudokuPuzzle(size);
            var          rand   = new Random();

            while (true)
            {
                int[] UnsolvedCellIndexes = puzzle.Cells
                                            .Select((cands, index) => new { cands, index }) //Project to a new sequence of candidates and index (an anonymous type behaving like a tuple)
                                            .Where(t => t.cands.Length >= 2)                //Filter to cells with at least 2 candidates
                                            .Select(u => u.index)                           //Project the tuple to only the index
                                            .ToArray();

                int cellIndex      = UnsolvedCellIndexes[rand.Next(UnsolvedCellIndexes.Length)];
                int candidateValue = puzzle.Cells[cellIndex][rand.Next(puzzle.Cells[cellIndex].Length)];

                SudokuPuzzle workingPuzzle = puzzle.PlaceValue(cellIndex, candidateValue);
                if (workingPuzzle != null)
                {
                    var Solutions = MultiSolve(workingPuzzle, 2);
                    switch (Solutions.Count)
                    {
                    case 0: continue;

                    case 1: return(Solutions.Single());

                    default:
                        puzzle = workingPuzzle;
                        break;
                    }
                }
            }
        }
Пример #2
0
 public static void FindSingularizedCellsTest()
 {
     var puzzle = new SudokuPuzzle("1234567..........................................................................");
     var puzzle2 = puzzle.PlaceValue(7, 8);
     var ConstrainedCellIndexes = SudokuPuzzle.FindSingularizedCells(puzzle, puzzle2, 7);
     Debug.Assert(ConstrainedCellIndexes.SequenceEqual(new int[] { 8 }));
 }
Пример #3
0
        public static void FindSingularizedCellsTest()
        {
            var puzzle  = new SudokuPuzzle("1234567..........................................................................");
            var puzzle2 = puzzle.PlaceValue(7, 8);
            var ConstrainedCellIndexes = SudokuPuzzle.FindSingularizedCells(puzzle, puzzle2, 7);

            Debug.Assert(ConstrainedCellIndexes.SequenceEqual(new int[] { 8 }));
        }
Пример #4
0
        public static SudokuPuzzle RandomGrid(int size)
        {
            SudokuPuzzle puzzle = new SudokuPuzzle(size);
            var rand = new Random();

            while (true)
            {
                int[] UnsolvedCellIndexes = puzzle.Cells
                    .Select((cands, index) => new { cands, index })     //Project to a new sequence of candidates and index (an anonymous type behaving like a tuple)
                    .Where(t => t.cands.Length >= 2)                    //Filter to cells with at least 2 candidates
                    .Select(u => u.index)                               //Project the tuple to only the index
                    .ToArray();

                int cellIndex = UnsolvedCellIndexes[rand.Next(UnsolvedCellIndexes.Length)];
                int candidateValue = puzzle.Cells[cellIndex][rand.Next(puzzle.Cells[cellIndex].Length)];

                SudokuPuzzle workingPuzzle = puzzle.PlaceValue(cellIndex, candidateValue);
                if (workingPuzzle != null)
                {
                    var Solutions = MultiSolve(workingPuzzle, 2);
                    switch (Solutions.Count)
                    {
                        case 0: continue;
                        case 1: return Solutions.Single();
                        default:
                            puzzle = workingPuzzle;
                            break;
                    }
                }
            }
        }