コード例 #1
0
        public static SudokuMap GenerateMap(String filePath)
        {
            StreamReader       mapFileStream;
            SudokuMap          GeneratedMap = new SudokuMap();
            String             readedLine;
            int                lineReaded = 0;
            List <List <int> > ListMap    = new List <List <int> >(Sudoku.COMMON_SIZE);

            if (String.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException($"Argument {nameof(filePath)} not provided.");
            }
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException($"File {filePath} not found.");
            }

            mapFileStream = new StreamReader(filePath);

            while (!mapFileStream.EndOfStream)
            {
                readedLine = mapFileStream.ReadLine();
                GeneratedMap.FillLineValues(readedLine, lineReaded++);
            }
            Console.WriteLine("The map was generated !\n");
            return(GeneratedMap);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            SudokuMap map = SudokuMapBuilder.GenerateMap(args[0]);

            if (map.ResolveMap())
            {
                map.PrintGrid();
            }
        }
コード例 #3
0
        public SudokuMap Find(int givenRow, int givenCol)
        {
            SudokuMap sudokuMap = new SudokuMap();

            if ((givenRow >= 0 && givenRow <= 2) && (givenCol >= 0 && givenCol <= 2))
            {
                sudokuMap.StartRow = 0;
                sudokuMap.StartCol = 0;
            }
            else if ((givenRow >= 0 && givenRow <= 2) && (givenCol >= 3 && givenCol <= 5))
            {
                sudokuMap.StartRow = 0;
                sudokuMap.StartCol = 3;
            }
            else if ((givenRow >= 0 && givenRow <= 2) && (givenCol >= 6 && givenCol <= 8))
            {
                sudokuMap.StartRow = 0;
                sudokuMap.StartCol = 6;
            }
            else if ((givenRow >= 3 && givenRow <= 5) && (givenCol >= 0 && givenCol <= 2))
            {
                sudokuMap.StartRow = 3;
                sudokuMap.StartCol = 0;
            }
            else if ((givenRow >= 3 && givenRow <= 5) && (givenCol >= 3 && givenCol <= 5))
            {
                sudokuMap.StartRow = 3;
                sudokuMap.StartCol = 3;
            }
            else if ((givenRow >= 3 && givenRow <= 5) && (givenCol >= 6 && givenCol <= 8))
            {
                sudokuMap.StartRow = 3;
                sudokuMap.StartCol = 6;
            }
            else if ((givenRow >= 6 && givenRow <= 8) && (givenCol >= 0 && givenCol <= 2))
            {
                sudokuMap.StartRow = 6;
                sudokuMap.StartCol = 0;
            }
            else if ((givenRow >= 6 && givenRow <= 8) && (givenCol >= 3 && givenCol <= 5))
            {
                sudokuMap.StartRow = 6;
                sudokuMap.StartCol = 3;
            }
            else if ((givenRow >= 6 && givenRow <= 8) && (givenCol >= 6 && givenCol <= 8))
            {
                sudokuMap.StartRow = 6;
                sudokuMap.StartCol = 6;
            }

            return(sudokuMap);
        }
コード例 #4
0
ファイル: SudokuMap.cs プロジェクト: TC-42-54/SudokuSolver
        public bool ResolveMap()
        {
            bool executionFinished = false;

            Solved = false;
            SudokuMap[] subMaps = new SudokuMap[2] {
                new SudokuMap(Map), new SudokuMap(Map)
            };
            Task <bool> resolveTask = Task.Run(() => {
                return(subMaps[0].Resolve(0, 0) ? true : false);
            });
            Task <bool> resolveReverseTask = Task.Run(() => {
                return(subMaps[1].ResolveReverse(8, 8) ? true : false);
            });
            Task <bool> timeoutTask = Task.Run(async() =>
            {
                await Task.Delay(10000);
                return(false);
            });

            resolveTask.ContinueWith(task =>
            {
                if (!executionFinished)
                {
                    task.Wait();
                    if (task.Result)
                    {
                        Solved = true;
                        Map    = subMaps[0].Map;
                        Console.WriteLine("\nThe Grid was solved within the time ! (limit 10 seconds)\n\n");
                        executionFinished = true;
                    }
                }
            });
            resolveReverseTask.ContinueWith(task =>
            {
                if (!executionFinished)
                {
                    task.Wait();
                    if (task.Result)
                    {
                        Solved = true;
                        Map    = subMaps[1].Map;
                        Console.WriteLine("\nThe Grid was solved within the time ! (limit 10 seconds)\n\n");
                        executionFinished = true;
                    }
                }
            });
            timeoutTask.ContinueWith(task =>
            {
                if (!executionFinished)
                {
                    task.Wait();
                    executionFinished = true;
                    Console.WriteLine("\nThe Grid wasn't solved within the time ! (10 seconds)\n\n");
                }
            });
            while (!executionFinished)
            {
            }

            return(executionFinished && Solved);
        }