Ejemplo n.º 1
0
        public static void SaveSudoku(WholeGrid grid, string filepath)
        {
            filepath = filepath.EndsWith(".xml") ? filepath : filepath + ".xml";
            XmlWriter xmlwriter = XmlWriter.Create(filepath, XML_Formatting.GetSettings());

            xmlwriter.WriteStartDocument();
            xmlwriter.WriteStartElement("sudoku");
            for (short X = 0; X < 3; X++)
            {
                for (short Y = 0; Y < 3; Y++)
                {
                    xmlwriter.WriteStartElement("Cube");
                    xmlwriter.WriteAttributeString("x", X.ToString());
                    xmlwriter.WriteAttributeString("y", Y.ToString());
                    for (short x = 0; x < 3; x++)
                    {
                        xmlwriter.WriteStartElement("subcolumn");
                        xmlwriter.WriteAttributeString("x", x.ToString());
                        for (short y = 0; y < 3; y++)
                        {
                            xmlwriter.WriteStartElement("val");
                            xmlwriter.WriteAttributeString("y", y.ToString());
                            xmlwriter.WriteValue(grid[X][Y][x][y]);
                            xmlwriter.WriteEndElement();
                        }
                        xmlwriter.WriteEndElement();
                    }
                    xmlwriter.WriteEndElement();
                }
            }
            xmlwriter.WriteEndElement();
            xmlwriter.WriteEndDocument();
            xmlwriter.Close();
        }
Ejemplo n.º 2
0
        public static bool ParseSudoku(string filepath, out WholeGrid grid)
        {
            filepath = filepath.EndsWith(".xml") ? filepath : filepath + ".xml";
            if (!File.Exists(filepath))
            {
                grid = null;
                return(false);
            }
            grid = new WholeGrid();
            XmlReader xmlreader = XmlReader.Create(filepath);

            while (xmlreader.Read())
            {
                if (xmlreader.IsStartElement("Cube"))
                {
                    int       X    = int.Parse(xmlreader["x"]);
                    int       Y    = int.Parse(xmlreader["y"]);
                    XmlReader cube = xmlreader.ReadSubtree();
                    while (cube.Read())
                    {
                        if (cube.IsStartElement("subcolumn"))
                        {
                            int       x         = int.Parse(cube["x"]);
                            XmlReader subcolumn = xmlreader.ReadSubtree();
                            while (subcolumn.Read())
                            {
                                if (subcolumn.IsStartElement("val"))
                                {
                                    int y = int.Parse(subcolumn["y"]);
                                    subcolumn.Read();
                                    grid[X][Y][x][y] = int.Parse(subcolumn.Value);
                                }
                            }
                        }
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 3
0
        public MainForm()
        {
            InitializeComponent();
            if (SudokuLoad.ParseSudoku(sudokufile, out WholeGrid grid))
            {
                currentpuzzle  = grid;
                ogInput        = currentpuzzle.NormaliseCurrentValues();
                puzzleassigned = true;
            }
            else
            {
                MessageBox.Show("Puzzle not parsed.");
            }
            bool g = Genome.ParseGenome(genomefile, out Genome ge);
            bool s = SudokuLoad.LoadScore(scorefile, out int id, out double score);

            if (g && s)
            {
                if (ge.ID == id)
                {
                    maxgenome      = ge;
                    prevcommon     = maxgenome;
                    maxscore       = score;
                    genomeassigned = true;
                    Console.WriteLine("Successfully parsed.");
                }
                else
                {
                    MessageBox.Show("Didn't parse properly. 1");
                }
            }
            else
            {
                MessageBox.Show("Didn't parse properly. 2");
            }
        }