Exemplo n.º 1
0
        public void Solve()
        {
            Console.WriteLine("Trying to solve the following Sudoku:");
            sudoku.PrintSudoku();

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            algorithms = ChooseAlgorithms.GetAlgorithms(SolvingAlgorithms.All);
            algorithms.ForEach(algorithm =>
            {
                Console.WriteLine("Using " + algorithm.ToString());
                threads.Add(new Thread(algorithm.Solve));
            });

            foreach (Thread thread in threads)
            {
                thread.IsBackground = true;
                thread.Start(sudoku);
            }

            WaitForAllThreads(lifespan);

            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;

            Console.WriteLine("\nRunTime {0}.{1:D3} Seconds", ts.Seconds, ts.Milliseconds);

            if (ValidateSudoku())
            {
                Console.WriteLine("Solved correctly\n");
            }
            else
            {
                Console.WriteLine("I am way too dumb to solve this sudoku\n");
            }

            sudoku.PrintSudoku();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.Title = "Sudoku Solver";

            while (true)
            {
                // get filepath from input and read it's contents
                Console.WriteLine("Enter path to text file:");

                string filepath = Console.ReadLine();

                string[] lines = FileReader.ReadFromFile(filepath);

                if (lines != null)
                {
                    // print contents from file
                    Console.WriteLine("\nContents read from file:\n");

                    foreach (string line in lines)
                    {
                        Console.WriteLine(line);
                    }

                    Console.WriteLine();

                    // parse text to sudoku grid
                    int[,] numbers = SudokuParser.ParseText(lines);

                    if (numbers != null)
                    {
                        Console.WriteLine("succesfully parsed sudoku\n");

                        Sudoku sudoku = new Sudoku();

                        for (int x = 0; x < 9; x++)
                        {
                            for (int y = 0; y < 9; y++)
                            {
                                sudoku.SetValue(x, y, numbers[x, y]);
                            }
                        }

                        sudoku.PrintSudoku();

                        Console.WriteLine("solving sudoku...");

                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();

                        bool solved = sudoku.Solve();

                        stopwatch.Stop();

                        if (solved)
                        {
                            Console.WriteLine($"solution found in {stopwatch.ElapsedMilliseconds} milliseconds");
                        }
                        else
                        {
                            Console.WriteLine($"no solution found in {stopwatch.ElapsedMilliseconds} milliseconds");
                        }

                        sudoku.PrintSudoku();
                    }
                }
            }
        }