Example #1
0
        static void GenerateSomeShape(Shape shape)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            int [] slow_grid = null;
            int [] fast_grid = null;

            // slow_grid = new NaiveTreePopulator ().PopulateGrid (shape, new PopulatorArgs ());

            double slow_time = (double)stopwatch.ElapsedTicks / Stopwatch.Frequency;

            stopwatch.Restart();

            try {
                fast_grid = new FastPopulator().PopulateGrid(shape, new PopulatorArgs());
            } catch (GridNotSolubleException) {
                Console.WriteLine("! Grid cannot be solved.");
            }

            double fast_time = (double)stopwatch.ElapsedTicks / Stopwatch.Frequency;

            Utils.PrintSideBySide(
                "   ",
                slow_grid == null ? "<failed>" : shape.GetString(slow_grid),
                fast_grid == null ? "<failed>" : shape.GetString(fast_grid)
                );

            Console.WriteLine("Constrainedness: " + (double)shape.group_size / shape.cells_by_group.GetLength(0));
            Console.WriteLine("Naive Populator: " + slow_time);
            Console.WriteLine("Beefy Populator: " + fast_time);
        }
Example #2
0
        public Puzzle Generate(bool verbose = false, bool print_fixed_cells = false)
        {
            int [] solution_grid = new FastPopulator().PopulateGrid(shape, new PopulatorArgs()
            {
                alphabet = alphabet, print_fixed_cells = print_fixed_cells
            }, initial_grid);

            if (verbose)
            {
                shape.Print(solution_grid, alphabet);
            }

            int [] puzzle_grid = PuzzleMaker.GeneratePuzzle(shape, solution_grid, seed, min_clues);

            if (verbose)
            {
                shape.Print(puzzle_grid, alphabet);
            }

            int [] output_cells = new int [letters == null ? 0 : letters.Length];

            if (letters != null)
            {
                if (pick_numbers_randomly)
                {
                    List <int> [] letter_locations = new List <int> [shape.group_size];

                    for (int i = 0; i < shape.group_size; i++)
                    {
                        letter_locations [i] = new List <int> ();

                        for (int j = 0; j < shape.num_cells; j++)
                        {
                            if (solution_grid [j] == i + 1 && puzzle_grid [j] == 0)
                            {
                                letter_locations [i].Add(j);
                            }
                        }
                    }

                    Random rng = new Random(seed);

                    for (int i = 0; i < letters.Length; i++)
                    {
                        int l   = letters [i];
                        int ind = rng.Next(letter_locations [l].Count);
                        int loc = letter_locations [l] [ind];

                        letter_locations [l].RemoveAt(ind);

                        output_cells [i] = loc;
                    }
                }
                else
                {
                    new RecursiveSolver().Solve(
                        shape,
                        puzzle_grid,
                        new SolverArgs()
                    {
                        rng = new Random(seed + 1), max_solutions = 1, iteration_limit = 50000
                    },
                        out int diff,
                        out int [] [] cell_fill_orders
                        );                 // We only care about the cell_fill_orders - we know what the solution grid is

                    List <int> fill_orders = cell_fill_orders [0].Reverse().ToList();
                    for (int i = 0; i < letters.Length; i++)
                    {
                        int letter = letters [i];

                        int last_index = 0;
                        for (int j = 0; j < fill_orders.Count; j++)
                        {
                            // Console.WriteLine (fill_orders [j] + " " + solution_grid [fill_orders [j]]);

                            if (solution_grid [fill_orders [j]] == letter + 1)
                            {
                                last_index = j;

                                break;
                            }
                        }

                        int last_cell = fill_orders [last_index];
                        fill_orders.RemoveAt(last_index);

                        Console.WriteLine(alphabet [letter] + " => " + last_cell);

                        output_cells [i] = last_cell;
                    }
                }
            }

            return(new Puzzle(this, puzzle_grid, solution_grid, output_cells));
        }