Example #1
0
        private int HardenPuzzle(byte[] solution, byte[] puzzle, PuzzleGenerationOptions options)
        {
            var best      = 0;
            var cellCount = Size * Size;

            Solve(puzzle, null, out best);

            for (var i = 0; i < options.MaxIterations; i++)
            {
                var next = new byte[puzzle.Length];

                Array.Copy(puzzle, next, next.Length);

                var random = new Random();

                for (var j = 0; j < Size * 2; j++)
                {
                    int c = random.Next() % cellCount;
                    int s = 0;

                    if ((random.Next() & 1) != 0)
                    {
                        next[c] = solution[c];
                        next[cellCount - c - 1] = solution[cellCount - c - 1];
                    }
                    else
                    {
                        next[c] = 0;
                        next[cellCount - c - 1] = 0;
                    }

                    if (Solve(next, null, out s) == 0 &&
                        s > best &&
                        (s <= options.MaxDifficulty || options.MaxDifficulty < 0))
                    {
                        Array.Copy(next, puzzle, puzzle.Length);
                        best = s;

                        if (options.TargetDifficulty >= 0 && s >= options.TargetDifficulty)
                        {
                            return(best);
                        }
                    }
                }
            }

            return(best);
        }
Example #2
0
        public int ActionGeneratePuzzle(PuzzleGenerationOptions options, Action <byte[]> puzzlePrinter)
        {
            //var solution = new byte[Size * Size];

            // TODO: move up to constructor
            var puzzleSize = PuzzleSizeExtensions.ToPuzzleSize(Size);

            var solutionGenerator = new OptimizedPuzzleSolutionGenerator(puzzleSize);

            solutionGenerator.CreatePuzzleSolution();
            var solution = solutionGenerator.PuzzleGrid.ByRow()
                           .SelectMany(x => x.Select(c => c.Value ?? 0))
                           .ToArray();
            var puzzle = new byte[Size * Size];

            Array.Copy(solution, puzzle, puzzle.Length);

            var difficulty = HardenPuzzle(solution, puzzle, options);

            puzzlePrinter(puzzle);

            Console.WriteLine("Difficulty: {0}", difficulty);
            return(0);
        }