Beispiel #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);
        }
Beispiel #2
0
        public static bool SolveBrute(Grid grid)
        {
            var workGrid = grid.Copy();

            for (var y = 0; y < workGrid.Length; y++)
            {
                for (var x = 0; x < workGrid.Length; x++)
                {
                    var cell = workGrid.GetCell(x, y);
                    if (!cell.Value.HasValue)
                    {
                        cell.Value = 1;
                        while (!workGrid.Valid())
                        {
                            cell.Value++;
                            if (cell.Value > workGrid.Length)
                            {
                                return(false);
                            }
                        }

                        if (SolveBrute(workGrid))
                        {
                            Steps.Add(workGrid);
                            return(true);
                        }
                        else
                        {
                            cell.Value++;
                            while (!workGrid.Valid())
                            {
                                cell.Value++;
                                if (cell.Value > workGrid.Length)
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }

            return(workGrid.Solved());
        }