Beispiel #1
0
        /// <summary>
        /// parses a single PGN game.
        /// Can also parse a move list in algebraic notation.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public PGNGame ParseGame(string data, int i = 0)
        {
            // make sure there is padding at the end.
            // When batch processing games this has already been done. Only needs to be
            // done when parsing single games
            if (data[data.Length - 1] != '\n' || data[data.Length - 2] != '\n')
                data += DataPadding;

            // Lexer
            var tags = ParseTags(data, ref i);
            var tokens = Tokenize(data, ref i);

            // Parser
            var iToken = 0;
            var position = new Board(true);
            var variation = GetVariation(tokens, ref iToken, position);
            var game = new PGNGame(tags, variation);
            return game;
        }
Beispiel #2
0
        public static string EncodeGame(PGNGame game, bool writeResults = true)
        {
            var variation = game.GetMainVariation();
            var sb        = new StringBuilder();

            foreach (var move in variation)
            {
                sb.Append(EncodeMove(move.From, move.To, move.Promotion));
            }

            if (writeResults)
            {
                sb.Append('-');
                sb.Append(((int)game.Results.Results).ToString());
            }
            var str = sb.ToString();

            return(str);
        }
Beispiel #3
0
        /// <summary>
        /// parses a single PGN game.
        /// Can also parse a move list in algebraic notation.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public PGNGame ParseGame(string data, int i = 0)
        {
            // make sure there is padding at the end.
            // When batch processing games this has already been done. Only needs to be
            // done when parsing single games
            if (data[data.Length - 1] != '\n' || data[data.Length - 2] != '\n')
            {
                data += DataPadding;
            }

            // Lexer
            var tags   = ParseTags(data, ref i);
            var tokens = Tokenize(data, ref i);

            // Parser
            var iToken    = 0;
            var position  = new Board(true);
            var variation = GetVariation(tokens, ref iToken, position);
            var game      = new PGNGame(tags, variation);

            return(game);
        }
Beispiel #4
0
        public static PGNGame DecodeGame(string encodedString)
        {
            var     s        = encodedString;
            int     i        = 0;
            Color   player   = Color.White;
            int     move     = 1;
            var     moves    = new List <PGNMove>();
            PGNMove lastmove = null;

            while (i < s.Length - 1)
            {
                // check if the last move wa actually a promotion
                if (Decode[s[i]] == SpecialInstruction)
                {
                    lastmove.Promotion = (Piece)Decode[s[i + 1]];
                    continue;
                }

                var from = Decode[s[i]];
                var to   = Decode[s[i + 1]];
                lastmove = new PGNMove()
                {
                    From = from, To = to, MoveNumber = move, Color = player                                       /*, Piece = ...*/
                };
                if (player == Color.Black)
                {
                    move++;
                }
                player = (player == Color.White) ? Color.Black : Color.White;

                moves.Add(lastmove);
                i += 2;
            }

            var variation = new GameVariation(moves.Select(x => (IPGNElement)x).ToList());
            var game      = new PGNGame(new Dictionary <string, string>(), variation);

            return(game);
        }
Beispiel #5
0
        public static string EncodeGame(PGNGame game, bool writeResults = true)
        {
            var variation = game.GetMainVariation();
            var sb = new StringBuilder();
            foreach(var move in variation)
                sb.Append(EncodeMove(move.From, move.To, move.Promotion));

            if (writeResults)
            {
                sb.Append('-');
                sb.Append(((int)game.Results.Results).ToString());
            }
            var str = sb.ToString();
            return str;
        }
Beispiel #6
0
        public static PGNGame DecodeGame(string encodedString)
        {
            var s = encodedString;
            int i = 0;
            Color player = Color.White;
            int move = 1;
            var moves = new List<PGNMove>();
            PGNMove lastmove = null;
            while(i < s.Length - 1)
            {
                // check if the last move wa actually a promotion
                if (Decode[s[i]] == SpecialInstruction)
                {
                    lastmove.Promotion = (Piece)Decode[s[i + 1]];
                    continue;
                }

                var from = Decode[s[i]];
                var to = Decode[s[i + 1]];
                lastmove = new PGNMove() { From = from, To = to, MoveNumber = move, Color = player/*, Piece = ...*/ };
                if(player == Color.Black)
                    move++;
                player = (player == Color.White) ? Color.Black : Color.White;

                moves.Add(lastmove);
                i += 2;
            }

            var variation = new GameVariation(moves.Select(x => (IPGNElement)x).ToList());
            var game = new PGNGame(new Dictionary<string, string>(), variation);
            return game;
        }