Example #1
0
        // not working :(
        private static bool SolveRecurse(Grid grid, int level = 0)
        {
            level++;
            grid.Propagate();
            foreach (var cell in grid.GetUnsolvedCells().OrderBy(c => grid.GetOptionsForCell(c).Count))
            {
                Console.WriteLine($"{level}: {cell}");
                foreach (var option in grid.GetOptionsForCell(cell))
                {
                    var workGrid = grid.Copy();
                    workGrid.SetValue(cell.X, cell.Y, option);
                    workGrid.Propagate();
                    //Drawer.DrawGrids(workGrid, grid);

                    if (workGrid.Solved())
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Solved");
                        //Drawer.DrawGrid(workGrid);
                        return(true);
                    }
                    if (SolveRecurse(workGrid, level))
                    {
                        return(true);
                    }
                }
            }

            Console.WriteLine($"Break after {level}");
            return(false);
        }