示例#1
0
        public static SudokuPuzzle RandomGrid(int size)
        {
            var puzzle = new SudokuPuzzle(size);

            while (true)
            {
                var unsolvedCellIndexes = puzzle.Cells
                    .Select((cands, index) => new {cands, index})
                    .Where(t => t.cands.Length >= 2)
                    .Select(u => u.index)
                    .ToArray();

                var cellIndex = unsolvedCellIndexes[Random.Next(unsolvedCellIndexes.Length)];
                var candidateValue = puzzle.Cells[cellIndex][Random.Next(puzzle.Cells[cellIndex].Length)];

                var workingPuzzle = puzzle.PlaceValue(cellIndex, candidateValue);
                if (workingPuzzle == null) continue;
                var solutions = MultiSolve(workingPuzzle, 2);
                switch (solutions.Count)
                {
                    case 0:
                        continue;
                    case 1:
                        return solutions.Single();
                    default:
                        puzzle = workingPuzzle;
                        break;
                }
            }
        }
示例#2
0
        public static SudokuPuzzle RandomGrid(int size)
        {
            var puzzle = new SudokuPuzzle(size);

            while (true)
            {
                var unsolvedCellIndexes = puzzle.Cells
                                          .Select((cands, index) => new { cands, index })
                                          .Where(t => t.cands.Length >= 2)
                                          .Select(u => u.index)
                                          .ToArray();

                var cellIndex      = unsolvedCellIndexes[Random.Next(unsolvedCellIndexes.Length)];
                var candidateValue = puzzle.Cells[cellIndex][Random.Next(puzzle.Cells[cellIndex].Length)];

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

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

                default:
                    puzzle = workingPuzzle;
                    break;
                }
            }
        }
示例#3
0
 private static List <int> FindSingularizedCells(SudokuPuzzle puzzle1, SudokuPuzzle puzzle2, int cellIndex)
 {
     return
         (puzzle1.Peers(cellIndex)
          .Where(i => puzzle1.Cells[i].Length > 1 && puzzle2.Cells[i].Length == 1)
          .ToList());
 }
示例#4
0
        private static List <SudokuPuzzle> MultiSolve(SudokuPuzzle input, int maximumSolutions = -1)
        {
            var solutions = new List <SudokuPuzzle>();

            input.Solve(p =>
            {
                solutions.Add(p);
                return(solutions.Count < maximumSolutions || maximumSolutions == -1);
            });
            return(solutions);
        }
示例#5
0
        public object Clone()
        {
            var clone = new SudokuPuzzle(Length)
            {
                Cells = new int[Cells.Length][]
            };

            for (var i = 0; i < Cells.Length; i++)
            {
                clone.Cells[i] = new int[Cells[i].Length];
                Buffer.BlockCopy(Cells[i], 0, clone.Cells[i], 0, Buffer.ByteLength(Cells[i]));
            }
            return(clone);
        }
示例#6
0
        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            app.Run(async context =>
            {
                context.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
                context.Response.Headers.Add("Access-Control-Allow-Headers", new[] { "*" });
                context.Response.Headers.Add("Access-Control-Allow-Methods", new[] { "*" });
                context.Response.ContentType = "application/json";
                var json = SudokuPuzzle.RandomGrid(9).ToString();
                await context.Response.WriteAsync(json);
            });
        }
示例#7
0
        public object Clone()
        {
            var clone = new SudokuPuzzle(Length) {Cells = new int[Cells.Length][]};

            for (var i = 0; i < Cells.Length; i++)
            {
                clone.Cells[i] = new int[Cells[i].Length];
                Buffer.BlockCopy(Cells[i], 0, clone.Cells[i], 0, Buffer.ByteLength(Cells[i]));
            }
            return clone;
        }
示例#8
0
 private static List<SudokuPuzzle> MultiSolve(SudokuPuzzle input, int maximumSolutions = -1)
 {
     var solutions = new List<SudokuPuzzle>();
     input.Solve(p =>
     {
         solutions.Add(p);
         return solutions.Count < maximumSolutions || maximumSolutions == -1;
     });
     return solutions;
 }
示例#9
0
 private static List<int> FindSingularizedCells(SudokuPuzzle puzzle1, SudokuPuzzle puzzle2, int cellIndex)
 {
     return
         puzzle1.Peers(cellIndex)
             .Where(i => puzzle1.Cells[i].Length > 1 && puzzle2.Cells[i].Length == 1)
             .ToList();
 }