Example #1
0
        public static ChessGame LoadFromPGN(string pgn)
        {
            ChessGame     game   = new ChessGame();
            List <string> tokens = new List <string>();
            string        token  = "";

            foreach (char c in pgn)
            {
                if (char.IsWhiteSpace(c))
                {
                    if (token.Length > 0)
                    {
                        tokens.Add(token);
                        token = "";
                    }
                }
                else
                {
                    token += c;
                }
            }
            if (token.Length > 0)
            {
                tokens.Add(token);
            }

            bool   inSideLine = false;
            bool   inComment  = false;
            string sideLine   = "";
            string comment    = "";

            for (int i = 0; i < tokens.Count; i++)
            {
                if (tokens[i][0] == '[')
                {
                    string tagName = tokens[i].Substring(1);
                    string value   = tokens[++i].Substring(0, tokens[i].Length - 1);
                    game.AddTagPair(tagName, value);
                    continue;
                }
                else if (tokens[i][0] == '(')
                {
                    //Annotation moves
                }
                else if (tokens[i][0] == '{')
                {
                    //Comment
                }
                else if (char.IsDigit(tokens[i][0]))
                {
                    //Move number
                }
                else if (char.IsLetter(tokens[i][0]))
                {
                    //Move
                }
            }

            return(game);
        }
Example #2
0
        public static ChessGame LoadFromPGN(string pgnText)
        {
            List <string> tokens = new List <string>();

            bool   stringToken  = false;
            bool   commentToken = false;
            bool   escape       = false;
            string text         = "";

            foreach (char c in pgnText)
            {
                if (_selfTerminating.Contains(c) && !stringToken && !commentToken)
                {
                    if (text.Length > 0)
                    {
                        tokens.Add(text);
                        text = "";
                    }
                    tokens.Add(c.ToString());
                }
                else if (c == '\\')
                {
                    escape = true;
                }
                else if (c == '"')
                {
                    if (!escape)
                    {
                        stringToken = !stringToken;
                        if (!stringToken)
                        {
                            tokens.Add("\"" + text + "\"");
                            text = "";
                        }
                    }
                    else
                    {
                        tokens.Add("\"");
                        escape = false;
                    }
                }
                else if (c == '{')
                {
                    commentToken = true;
                }
                else if (c == '}')
                {
                    commentToken = false;
                    if (text.Length > 0)
                    {
                        tokens.Add("{" + text + "}");
                        text = "";
                    }
                }
                else if (!char.IsWhiteSpace(c) || stringToken || commentToken)
                {
                    if (char.IsWhiteSpace(c))
                    {
                        text += " ";
                    }
                    else
                    {
                        text += c;
                    }
                }
                else if (char.IsWhiteSpace(c))
                {
                    if (text.Length > 0)
                    {
                        tokens.Add(text);
                        text = "";
                    }
                }
            }
            if (text.Length > 0)
            {
                tokens.Add(text);
                text = "";
            }

            ChessGame     game    = new ChessGame();
            ChessPosition builder = new ChessPosition();

            bool          tagPair    = false;
            string        name       = "";
            string        value      = "";
            List <string> moveTokens = new List <string>();

            Dictionary <string, ChessResult> results = new Dictionary <string, ChessResult>();

            results["1-0"]     = ChessResult.WHITE_WINS;
            results["0-1"]     = ChessResult.BLACK_WINS;
            results["1/2-1/2"] = ChessResult.DRAW;
            results["*"]       = ChessResult.UNAVAILABLE;

            foreach (string token in tokens)
            {
                if (token == "[")
                {
                    tagPair = true;
                }
                else if (token == "]")
                {
                    tagPair = false;
                    game.AddTagPair(name, value);
                    name  = "";
                    value = "";
                }
                else if (tagPair)
                {
                    if (name.Length < 1)
                    {
                        name = token;
                    }
                    else
                    {
                        value = token;
                    }
                }
                else if (results.ContainsKey(token))
                {
                    break;
                }
                else
                {
                    moveTokens.Add(token);
                }
            }

            game.Moves = ParseTokens(builder, moveTokens);

            return(game);
        }