/// <summary>
        /// Parses the text representing a single <see cref="SudokuPuzzle"/>.
        /// </summary>
        /// <param name="s">The text to parse.</param>
        /// <returns>A <see cref="SudokuPuzzle"/> representation of <c><paramref name="s"/></c>.</returns>
        /// <exception cref="ArgumentNullException"><c><paramref name="s"/></c> is <c>null</c>.</exception>
        /// <exception cref="FormatException"><c><paramref name="s"/></c> is not in the correct format.</exception>
        public static SudokuPuzzle Deserialize(String s)
        {
            if (s is null)
            {
                throw new ArgumentNullException(nameof(s));
            }

            String[] split = s.Split(',');

            if (split.Length != 5)
            {
                throw new FormatException();
            }

            try
            {
                int size    = Int32.Parse(split[0]);
                int squared = size * size;

                if (!SudokuPuzzle.VerifySize(size))
                {
                    throw new FormatException();
                }

                SudokuDifficulty difficulty = (SudokuDifficulty)Enum.Parse(typeof(SudokuDifficulty), split[1], true);
                int[]            numbers    = new int[squared], solutions = new int[squared];
                bool[]           readOnly   = new bool[squared];

                if (split[2].Length != squared || split[3].Length != squared || split[4].Length != squared)
                {
                    throw new FormatException();
                }

                for (int i = 0; i < squared; i++)
                {
                    numbers[i]   = split[2][i] - '0';
                    solutions[i] = split[3][i] - '0';
                    readOnly[i]  = split[3][i] == '1';
                }

                SudokuPuzzle puzzle = SudokuPuzzle.Parse(size, difficulty, numbers, solutions, readOnly);
                puzzle.ResetPossible();
                return(puzzle);
            }

            catch
            {
                throw new FormatException();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts the string representation to its <see cref="SudokuPuzzle"/> equivalent.
        /// </summary>
        /// <param name="s">A string containing a puzzle to convert.</param>
        /// <param name="difficulty">An optional difficulty to associate with the <see cref="SudokuPuzzle"/>.</param>
        /// <returns>A <see cref="SudokuPuzzle"/> equivalent to the puzzle contained in <c><paramref name="s"/></c>.</returns>
        /// <exception cref="ArgumentNullException"><c><paramref name="s"/></c> is <c>null</c>.</exception>
        /// <exception cref="FormatException"><c><paramref name="s"/></c> is not in the correct format.</exception>
        public static SudokuPuzzle Parse(String s, SudokuDifficulty difficulty = SudokuDifficulty.None)
        {
            if (s is null)
            {
                throw new ArgumentNullException(nameof(s));
            }

            s = Regex.Replace(s, @"[^0-9]", "");
            int size = (int)Math.Sqrt(s.Length);

            if (!SudokuPuzzle.VerifySize(size))
            {
                throw new FormatException();
            }

            SudokuPuzzle sudoku;

            try
            {
                sudoku = new SudokuPuzzle(size, difficulty);
            }

            catch
            {
                throw new FormatException();
            }

            for (int i = 0; i < size; i++)
            {
                int currentRow = i * size;

                for (int j = 0; j < size; j++)
                {
                    sudoku[i, j] = s[currentRow + j] - '0';
                }
            }

            return(sudoku);
        }