コード例 #1
0
        public int CheckValid(string filePath, int count)
        {
            //新申请一个数独棋盘
            var sudokuSets = new HashSet <string>();
            //从路径中读取相应内容
            var    content       = File.ReadAllText(filePath);
            string splitSymbol   = Environment.NewLine + Environment.NewLine;
            var    multipleLines = content.Split(new[] { splitSymbol }, StringSplitOptions.RemoveEmptyEntries);

            if (multipleLines.Length == 1)
            {
                multipleLines = content.Split(new[] { "\n\n" }, StringSplitOptions.RemoveEmptyEntries);
            }
            if (multipleLines.Any())
            {
                foreach (var lines in multipleLines)
                {
                    try
                    {
                        var sudokuPanel =
                            new SudokuPanel(
                                lines.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries),
                                NumberId);
                        string hashCode = GetHashCode(sudokuPanel);
                        if (sudokuSets.Contains(hashCode))
                        {
                            Logger.Error("Sudoku.txt have repeated sudoku panels!", _logFile);
                            return((int)ErrorType.RepeatedPanels);
                        }
                        if (!sudokuPanel.Valid)
                        {
                            Logger.Error($"SudokuPanel Not Invalid:\n {sudokuPanel}", _logFile);
                            return((int)ErrorType.SudokuPanelInvalid);
                        }

                        sudokuSets.Add(hashCode);
                    }
                    catch (Exception e)
                    {
                        //the sudoku is not valid.
                        break;
                    }
                }
                if (sudokuSets.Count == count)
                {
                    return(1);
                }
            }
            Logger.Error($"Sudoku.txt doesn't have engough sudoku panels! Expect:{count} Actual:{sudokuSets.Count}", _logFile);
            return((int)ErrorType.NotEnoughCount);
        }
コード例 #2
0
 public bool MatchPuzzle(SudokuPanel sudokuPuzzle)
 {
     for (int i = 0; i < this.Grid.GetLength(0); i++)
     {
         for (int j = 0; j < this.Grid.GetLength(1); j++)
         {
             int Puzzlenum = int.Parse(sudokuPuzzle.Grid[i, j]);
             int Answernum = int.Parse(this.Grid[i, j]);
             if (Puzzlenum != 0 && Puzzlenum != Answernum)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
コード例 #3
0
        //Overview:将-s和-c功能整合到一起
        //当puzzlePath为非空串时,测试-s,此时count应为0,否则测试-c
        public int CheckValid(string puzzlePath, string filePath, int count)
        {
            //新申请一个数独棋盘
            var sudokuSets = new HashSet <string>();
            //从filepath路径中读取相应内容
            var    content       = File.ReadAllText(filePath);
            string splitSymbol   = Environment.NewLine + Environment.NewLine;
            var    multipleLines = content.Split(new[] { splitSymbol }, StringSplitOptions.RemoveEmptyEntries);

            if (multipleLines.Length == 1)
            {
                multipleLines = content.Split(new[] { "\n\n" }, StringSplitOptions.RemoveEmptyEntries);
            }
            //若puzzle路径不为空,则读取其中的puzzle并进行split,此时filepath内是解得的答案
            bool   hasPuzzle = string.IsNullOrEmpty(puzzlePath) ^ true;
            string puzzleContent;
            LinkedList <string> puzzleLines = new LinkedList <string>();

            if (hasPuzzle)
            {
                puzzleContent = File.ReadAllText(puzzlePath);
                string[] puzzleTemp = puzzleContent.Split(new[] { splitSymbol }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string puzzle in puzzleTemp)
                {
                    puzzleLines.AddLast(puzzle);
                }
                if (puzzleLines.Count != multipleLines.Length)
                {
                    Logger.Error($"Puzzle Number Do Not Match Answer Number!", _logFile);
                    return((int)ErrorType.NumbersDoNotMatch);
                }
            }
            //开始测试
            if (multipleLines.Any())
            {
                foreach (var lines in multipleLines)
                {
                    try
                    {
                        var sudokuPanel =
                            new SudokuPanel(
                                lines.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries),
                                NumberId);


                        //进行数独的有效性检查
                        if (!sudokuPanel.Valid)
                        {
                            Logger.Error($"SudokuPanel Not Invalid:\n {sudokuPanel}", _logFile);
                            return((int)ErrorType.SudokuPanelInvalid);
                        }
                        //进行判重
                        if (!hasPuzzle)
                        {
                            string hashCode = GetHashCode(sudokuPanel);
                            if (sudokuSets.Contains(hashCode))
                            {
                                Logger.Error("Sudoku.txt have repeated sudoku panels!", _logFile);
                                return((int)ErrorType.RepeatedPanels);
                            }
                            sudokuSets.Add(hashCode);
                        }
                        //检查解与题目是否对应】
                        if (hasPuzzle)
                        {
                            //从puzzleline中取出题目
                            var puzzlePanel =
                                new SudokuPanel(
                                    puzzleLines.First.Value.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries),
                                    NumberId);
                            if (!sudokuPanel.MatchPuzzle(puzzlePanel))
                            {
                                Logger.Error($"Sudoku Answer Do Not Match The Puzzle!:\n Puzzle:\n{puzzlePanel}\n\nAnswer:\n{sudokuPanel}", _logFile);
                                return((int)ErrorType.SudokuAnswerDoNotMatch);
                            }
                            puzzleLines.RemoveFirst();
                        }
                    }
                    catch (Exception e)
                    {
                        //the sudoku is not valid.
                        break;
                    }
                }
                if (hasPuzzle && puzzleLines.Count == 0)
                {
                    return(1);
                }
                else
                {
                    if (sudokuSets.Count == count)
                    {
                        return(1);
                    }
                }
            }
            Logger.Error($"Sudoku.txt doesn't have engough sudoku panels! Expect:{count} Actual:{sudokuSets.Count}", _logFile);
            return((int)ErrorType.NotEnoughCount);
        }
コード例 #4
0
 //Get Hash Code
 public static string GetHashCode(SudokuPanel obj)
 {
     return(obj.ToString());
 }