Ejemplo n.º 1
0
        /// <summary>
        /// Convert the given game tree into an IEnumerable of boards with respect to the given base board.
        /// </summary>
        /// <param name="tree">The game tree.</param>
        /// <param name="baseBoard">The base board.</param>
        public static IEnumerable <Board> ToBoardList(this GameTree tree, Board baseBoard)
        {
            Board board = baseBoard;

            foreach (Node node in tree.Elements)
            {
                Color color = node.HasProperty("W") ? Color.W : node.HasProperty("B") ? Color.B : Color.E;
                if (color == Color.E)
                {
                    continue;
                }

                board = board.MakeMove(new Move(color, board.SgfToVertex(node[color.ToString()].Value)));
                yield return(board);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a GameTree from the specified file.
        /// </summary>
        /// <param name="path">The path of the file.</param>
        public static GameTree FromFile(string path)
        {
            string input = File.ReadAllText(path);

            return(GameTree.FromString(input));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns the corresponding game tree of a certain range in the given token list.
        /// </summary>
        /// <param name="tokens">The token list.</param>
        /// <param name="start">The start index.</param>
        /// <param name="end">The end index.</param>
        public static GameTree Parse(List <Tuple <TokenType, string> > tokens, int start = 0, int end = int.MaxValue)
        {
            int i = start;

            end = Math.Min(end, tokens.Count - 1);

            GameTree    tree     = new GameTree();
            Node        node     = new Node();
            SgfProperty property = new SgfProperty("", "");

            while (i <= end)
            {
                Tuple <TokenType, string> token = tokens[i];

                if (token.Item1 == TokenType.Semicolon)
                {
                    node = new Node();
                    tree.Elements.Add(node);
                }
                else if (token.Item1 == TokenType.PropIdent)
                {
                    property = new SgfProperty(token.Item2, new List <string>());
                    node.Properties.Add(property);
                }
                else if (token.Item1 == TokenType.CValueType)
                {
                    property.Values.Add(token.Item2);
                }
                else if (token.Item1 == TokenType.Parenthesis && token.Item2 == "(")
                {
                    break;
                }
                else if (token.Item1 == TokenType.Parenthesis)
                {
                    throw new ParseException("Unexpected parenthesis.");
                }

                i++;
            }

            int depth    = 0;
            int newstart = 0;

            while (i <= end)
            {
                Tuple <TokenType, string> token = tokens[i];

                if (token.Item1 == TokenType.Parenthesis && token.Item2 == "(")
                {
                    depth++;
                    if (depth == 1)
                    {
                        newstart = i + 1;
                    }
                }
                else if (token.Item1 == TokenType.Parenthesis && token.Item2 == ")")
                {
                    depth--;
                    if (depth == 0)
                    {
                        tree.SubTrees.Add(Parse(tokens, newstart, i - 1));
                    }
                }

                i++;
            }

            return(tree);
        }