コード例 #1
0
ファイル: Parser.cs プロジェクト: minception/NonogramV3
 /// <summary>
 /// Write a checksum collection in an xml format
 /// </summary>
 /// <param name="collection">Collection to write</param>
 /// <param name="textWriter">Output writer</param>
 /// <param name="name">Checksum name (usually vertical or horizontal)</param>
 private static void ChecksumCollectionToXml(Puzzle.ChecksumCollection collection, TextWriter textWriter, string name)
 {
     if (collection == null)
     {
         throw new ArgumentNullException(nameof(collection));
     }
     if (textWriter == null)
     {
         throw new ArgumentNullException(nameof(textWriter));
     }
     if (name == null)
     {
         throw new ArgumentNullException(nameof(name));
     }
     textWriter.WriteLine("<" + name + ">");
     for (int i = 0; i < collection.Count; i++)
     {
         textWriter.WriteLine("  <Checksum>");
         for (int j = 0; j < collection[i].Count; j++)
         {
             textWriter.WriteLine("      <State>" + collection[i][j].ToString() + "</State>");
         }
         textWriter.WriteLine("  </Checksum>");
     }
     textWriter.WriteLine("</" + name + ">");
 }
コード例 #2
0
ファイル: Parser.cs プロジェクト: minception/NonogramV3
        /// <summary>
        /// Parse a puzzle checksum in json format
        /// </summary>
        /// <param name="reader">Input reader</param>
        /// <returns>Parsed checksum collection</returns>
        private static Puzzle.ChecksumCollection readJsonChecksum(int size, JsonTextReader jsonReader)
        {
            var checksum = new Puzzle.ChecksumCollection(size);
            // Starting index at -1, it will increase to 0 when reading first opening bracket
            var index         = 0;
            var startingDepth = jsonReader.Depth;

            while (jsonReader.Read())
            {
                switch (jsonReader.TokenType)
                {
                case JsonToken.StartArray:
                {
                    break;
                }

                case JsonToken.EndArray:
                {
                    if (index == size - 1)
                    {
                        return(checksum);
                    }
                    index++;
                    break;
                }

                default:
                {
                    checksum[index].Add(int.Parse(jsonReader.Value.ToString()));
                    break;
                }
                }
            }
            return(checksum);
        }
コード例 #3
0
ファイル: Parser.cs プロジェクト: minception/NonogramV3
        /// <summary>
        /// Parse a puzzle in json format
        /// </summary>
        /// <param name="reader">Input reader</param>
        /// <returns>Parsed puzzle</returns>
        public static Puzzle JsonToPuzzle(TextReader reader)
        {
            var jsonReader = new JsonTextReader(reader);
            int sizeX = 0, sizeY = 0;

            CellState[] state = null;
            Puzzle.ChecksumCollection horizontal = null, vertical = null;
            while (jsonReader.Read())
            {
                if (jsonReader.Value != null)
                {
                    switch (jsonReader.Value)
                    {
                    case "SizeX":
                    {
                        sizeX = (int)jsonReader.ReadAsInt32();
                        break;
                    }

                    case "SizeY":
                    {
                        sizeY = (int)jsonReader.ReadAsInt32();
                        break;
                    }

                    case "Horizontal":
                    {
                        horizontal = readJsonChecksum(sizeY, jsonReader);
                        break;
                    }

                    case "Vertical":
                    {
                        vertical = readJsonChecksum(sizeX, jsonReader);
                        break;
                    }

                    case "State":
                    {
                        state = readJsonState(sizeX, sizeY, jsonReader);
                        break;
                    }
                    }
                }
            }
            var puzzle = new Puzzle(horizontal, vertical);

            SetPuzzleState(puzzle, state);
            return(puzzle);
        }
コード例 #4
0
ファイル: Parser.cs プロジェクト: minception/NonogramV3
 /// <summary>
 /// Write a checksum collection in a json format
 /// </summary>
 /// <param name="collection">Collection to write</param>
 /// <param name="textWriter">Output writer</param>
 /// <param name="name">Checksum name (usually vertical or horizontal)</param>
 private static void JsonChecksumCollection(Puzzle.ChecksumCollection collection, TextWriter textWriter, string name)
 {
     textWriter.Write("\"" + name + "\":[");
     for (int i = 0; i < collection.Count; i++)
     {
         textWriter.Write("[");
         for (int j = 0; j < collection[i].Count; j++)
         {
             textWriter.Write("\"" + collection[i][j].ToString() + "\"");
             if (j != collection[i].Count - 1)
             {
                 textWriter.Write(",");
             }
         }
         textWriter.Write("]");
         if (i != collection.Count - 1)
         {
             textWriter.Write(",");
         }
     }
     textWriter.Write("]");
 }
コード例 #5
0
ファイル: Parser.cs プロジェクト: minception/NonogramV3
        /// <summary>
        /// Parse a checksum in xml format
        /// </summary>
        /// <param name="size">Size of the checksum</param>
        /// <param name="reader">Input reader</param>
        /// <returns>Parsed Checksum collection</returns>
        private static Puzzle.ChecksumCollection readXmlChecksum(int size, XmlReader reader)
        {
            // index will be set to 0 after reading the first checksum
            int index    = -1;
            var checksum = new Puzzle.ChecksumCollection(size);

            reader.MoveToContent();
            while (!reader.EOF)
            {
                if (reader.Name == "State" && reader.NodeType == XmlNodeType.Element)
                {
                    checksum[index].Add(reader.ReadElementContentAsInt());
                    continue;
                }
                if (reader.Name == "Checksum" && reader.NodeType == XmlNodeType.Element)
                {
                    index++;
                }
                reader.Read();
            }
            return(checksum);
        }
コード例 #6
0
ファイル: Parser.cs プロジェクト: minception/NonogramV3
        /// <summary>
        /// Parse a puzzle in Xml format
        /// </summary>
        /// <param name="reader">Input reader</param>
        /// <returns>Parsed puzzle</returns>
        public static Puzzle XmlToPuzzle(TextReader reader)
        {
            var settings = new XmlReaderSettings();

            settings.IgnoreWhitespace = true;
            var xmlReader = XmlReader.Create(reader, settings);

            xmlReader.MoveToContent();
            int sizeX = 0, sizeY = 0;

            CellState[] state = null;
            Puzzle.ChecksumCollection horizontal = null, vertical = null;
            while (!xmlReader.EOF)
            {
                if (xmlReader.NodeType == XmlNodeType.Element)
                {
                    switch (xmlReader.Name)
                    {
                    case "SizeX":
                    {
                        sizeX = xmlReader.ReadElementContentAsInt();
                        break;
                    }

                    case "SizeY":
                    {
                        sizeY = xmlReader.ReadElementContentAsInt();
                        break;
                    }

                    case "Horizontal":
                    {
                        horizontal = readXmlChecksum(sizeY, xmlReader.ReadSubtree());
                        break;
                    }

                    case "Vertical":
                    {
                        vertical = readXmlChecksum(sizeX, xmlReader.ReadSubtree());
                        break;
                    }

                    case "State":
                    {
                        state = readXmlState(sizeX, sizeY, xmlReader.ReadSubtree());
                        break;
                    }

                    default:
                    {
                        xmlReader.Read();
                        break;
                    }
                    }
                }
                else
                {
                    xmlReader.Read();
                }
            }
            var puzzle = new Puzzle(horizontal, vertical);

            SetPuzzleState(puzzle, state);
            return(puzzle);
        }