Ejemplo n.º 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;
                }
            }
        }
Ejemplo n.º 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;
                }
            }
        }
Ejemplo n.º 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());
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
            });
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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;
 }
Ejemplo n.º 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();
 }