Example #1
0
        private static void Main()
        {
            Console.Title = Assembly.GetExecutingAssembly().GetName().Name;

            Console.CursorVisible = true;

            Console.WriteLine(ConsoleTextBlocks.ShowWelcome("Welcome to Sudoku Solver!"));
            Console.WriteLine();

            GetAvailableSudokuGrids();

            Console.WriteLine();

            while (true)
            {
                SudokuGrid grid;
                using (var selectedFile = ConsoleInteractions.ShowListAndSelectItem(USER_ABS_DIR_AVAILABLE_GRIDS,
                                                                                    SudokuGridFile.Extension,
                                                                                    Console.In,
                                                                                    Console.Out))
                {
                    grid = SudokuGrid.FromBinary(selectedFile);
                }

                Console.WriteLine();

                var solvingTask = new Task <bool>(() =>
                {
                    var gridSolver = new SudokuSolvingIterationAssumptionTechnique(grid);
                    gridSolver.Solve();
                    gridSolver.ApplySolutionToGrid();
                    return(gridSolver.SolutionExists);
                });
                var dottingTaskCTS          = new CancellationTokenSource();
                var dottingTask             = new Task(() => ConsoleTextBlocks.ShowBlinkingDots(dottingTaskCTS.Token, Console.Out));
                var outputTask              = new Task(() => ShowSolution(grid));
                var escapeTask              = new Task <bool>(ConsoleInteractions.ShowEscapeQuestion);
                var checkingCorrectnessTask = new Task <bool>(grid.CheckCorrectness);

                checkingCorrectnessTask.ContinueWith((fin) =>
                {
                    if (fin.Result)
                    {
                        solvingTask.Start();
                    }
                    else
                    {
                        Console.WriteLine(ConsoleTextMessages.IncorrectInitialConfiguration + "\n");
                        escapeTask.Start();
                    }
                }, TaskContinuationOptions.OnlyOnRanToCompletion);
                solvingTask.ContinueWith((fin) => dottingTaskCTS.Cancel(), TaskContinuationOptions.OnlyOnRanToCompletion);
                dottingTask.ContinueWith((fin) =>
                {
                    if (solvingTask.Result)
                    {
                        outputTask.Start();
                    }
                    else
                    {
                        Console.WriteLine(ConsoleTextMessages.IncorrectInitialConfiguration + "\n");
                        escapeTask.Start();
                    }
                }, TaskContinuationOptions.OnlyOnRanToCompletion);
                outputTask.ContinueWith((fin) => escapeTask.Start(), TaskContinuationOptions.OnlyOnRanToCompletion);

                dottingTask.Start();
                checkingCorrectnessTask.Start();

                escapeTask.Wait();
                if (escapeTask.Result)
                {
                    break;
                }
            }
        }
Example #2
0
        private static void Main()
        {
            Console.Title = Assembly.GetExecutingAssembly().GetName().Name;

            Console.CursorVisible = true;

            Console.WriteLine(ConsoleTextBlocks.ShowWelcome("Welcome to Sudoku Grid Files Converter!"));
            Console.WriteLine();

            string dir = ConsoleInteractions.RequireChild(WORK_ABS_DIR,
                                                          "Sources directory",
                                                          ConsoleTextMessages.DirectoryNotFound,
                                                          null,
                                                          Console.In,
                                                          Console.Out);

            SOURCES_ABS_DIR = WORK_ABS_DIR + dir + Path.DirectorySeparatorChar;
            OUTPUT_ABS_DIR  = SOURCES_ABS_DIR;

            Console.WriteLine("\n" + ConsoleTextBlocks.ShowCharsToLineEnd(Console.CursorLeft));

            while (true)
            {
                string[]   modes = Enum.GetNames(typeof(SudokuConvertionMode));
                FileStream selectedFile;
                string     outputFilePath;

                var selectedMode = (SudokuConvertionMode)ConsoleInteractions.AskMultiple(modes, Console.In, Console.Out);
                Console.WriteLine("\n");

                switch (selectedMode)
                {
                case SudokuConvertionMode.Encoding:
                    selectedFile = ConsoleInteractions.ShowListAndSelectItem(SOURCES_ABS_DIR,
                                                                             SOURCE_EXT,
                                                                             Console.In,
                                                                             Console.Out);
                    outputFilePath = OUTPUT_ABS_DIR +
                                     Path.GetFileNameWithoutExtension(OUTPUT_ABS_DIR + selectedFile.Name) + "." + CONVERTED_EXT;
                    using (SudokuGridFile.CreateBinaryFromText(selectedFile,
                                                               outputFilePath,
                                                               SudokuConvertionAlgorithm.NonUniform))
                    {
                        selectedFile.Dispose();
                    }
                    break;

                case SudokuConvertionMode.Decoding:
                    selectedFile = ConsoleInteractions.ShowListAndSelectItem(SOURCES_ABS_DIR,
                                                                             CONVERTED_EXT,
                                                                             Console.In,
                                                                             Console.Out);
                    outputFilePath = OUTPUT_ABS_DIR +
                                     Path.GetFileNameWithoutExtension(OUTPUT_ABS_DIR + selectedFile.Name) + "." + SOURCE_EXT;
                    using (SudokuGridFile.CreateTextFromBinary(selectedFile,
                                                               outputFilePath))
                    {
                        selectedFile.Dispose();
                    }
                    break;
                }

                Console.WriteLine(ConsoleTextBlocks.ShowCharsToLineEnd(Console.CursorLeft));
                Console.WriteLine();
                Console.WriteLine("Finished!");
                if (ConsoleInteractions.ShowEscapeQuestion())
                {
                    break;
                }
            }
        }