Example #1
0
        internal static SudokuBoard LoadFromXML(XElement a_element)
        {
            try
            {
                SudokuBoard board = new SudokuBoard();

                a_element.Elements().ForEach(cell =>
                {
                    Point p = Converters.CoordToPoint(cell.Attribute("coord").Value);
                    int col = p.X;
                    int row = p.Y;

                    cell.Elements().ForEach(num =>
                    {
                        int num_index = Int32.Parse(num.Value) - 1;
                        board[p.X, p.Y][num_index].State = Converters.StringToSudokuNumberState(num.Attribute("state").Value);
                    });
                });

                if (!board.Check())
                {
                    return(null);
                }

                return(board);
            }
            catch
            {
                return(null);
            }
        }
Example #2
0
        private static SudokuBoard LoadFromText(string a_str)
        {
            a_str = a_str.Replace(System.Environment.NewLine, System.Environment.NewLine.Substring(0, 1));
            List <string> lines = a_str.Split(System.Environment.NewLine[0]).ToList();

            for (int i = lines.Count - 1; i >= 0; i--)
            {
                lines[i] = lines[i].Replace(" ", "");
                if (lines[i].IndexOfAny(new char[] { ']', '[', '-', '+', '*' }) != -1)
                {
                    lines.RemoveAt(i);
                }
                else if (lines[i].Length == 0)
                {
                    lines.RemoveAt(i);
                }
            }

            if (lines.Count == 1)
            {
                string str = lines[0];

                if (str.Length == 9 * 9)
                {
                    lines.Clear();

                    for (int i = 0; i < 9; i++)
                    {
                        lines.Add(str.Substring(i * 9, 9));
                    }
                }
            }

            for (int i = lines.Count - 1; i >= 0; i--)
            {
                lines[i] = lines[i].Replace(" |", "");
                lines[i] = lines[i].Replace("|", "");
                lines[i] = lines[i].Replace(" ", ".");
            }

            for (int i = lines.Count - 1; i >= 0; i--)
            {
                if (lines[i].Length != SIZE)
                {
                    lines.RemoveAt(i);
                }
            }

            if (lines.Count != SIZE)
            {
                return(null);
            }

            SudokuBoard board = new SudokuBoard();

            (from cell in board.Cells()
             where lines[cell.Row][cell.Col] != '.'
             select cell).ForEach(cell =>
                                  cell[Int32.Parse(new string(new char[] { lines[cell.Row][cell.Col] })) - 1].State =
                                      SudokuNumberState.sudokucellstateManualEntered);

            if (!board.Check())
            {
                return(null);
            }

            return(board);
        }