Beispiel #1
0
        /// <summary>
        /// Parses a SGF game tree
        /// </summary>
        /// <param name="input">Input</param>
        /// <param name="inputPosition">Current input position</param>
        /// <returns>SGF game tree</returns>
        private SgfGameTree ParseGameTree(string input, ref int inputPosition)
        {
            if (input[inputPosition] != '(')
            {
                throw new SgfParseException($"No gameTree node found on input position {inputPosition}");
            }

            inputPosition++;
            SkipInputWhitespace(input, ref inputPosition);

            //parse sequence
            SgfSequence sequence = ParseSequence(input, ref inputPosition);

            SkipInputWhitespace(input, ref inputPosition);

            //parse children
            List <SgfGameTree> children = new List <SgfGameTree>();

            while (inputPosition < input.Length && input[inputPosition] == '(')
            {
                SgfGameTree child = ParseGameTree(input, ref inputPosition);
                children.Add(child);
                SkipInputWhitespace(input, ref inputPosition);
            }

            //check proper ending of the game tree
            if (inputPosition == input.Length || input[inputPosition] != ')')
            {
                throw new SgfParseException($"SGF gameTree was not properly terminated with ) at {inputPosition}");
            }
            inputPosition++;

            return(new SgfGameTree(sequence, children));
        }
Beispiel #2
0
        /// <summary>
        /// Serializes a SGF sequence
        /// </summary>
        /// <param name="sequence">Sequence</param>
        /// <returns>Serialized sequence</returns>
        private string SerializeSequence([NotNull] SgfSequence sequence)
        {
            if (sequence == null)
            {
                throw new ArgumentNullException(nameof(sequence));
            }

            StringBuilder builder = new StringBuilder();

            foreach (var node in sequence)
            {
                builder.Append(SerializeNode(node));
                if (_createNewlines)
                {
                    builder.AppendLine();
                }
            }

            return(builder.ToString());
        }