Exemplo n.º 1
0
 public void TestGameEncoder()
 {
     var parser = new PGNParser();
     var g      = parser.ParsePGN(GameData);
     var game   = g.Games[0];
     var enc    = GameEncoder.EncodeGame(game);
 }
Exemplo n.º 2
0
        public void TestParser3()
        {
            var data   = File.ReadAllText("..\\..\\..\\TestData\\perle.pgn");
            var parser = new PGNParser();

            parser.ParsePGN(data);
        }
Exemplo n.º 3
0
        public void TestParser2()
        {
            var data   = File.ReadAllText("..\\..\\..\\TestData\\annotatedsetone.pgn");
            var parser = new PGNParser();

            parser.ParsePGN(data);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Compiles an opening book out of one or more PGN input files.
        /// </summary>
        /// <param name="pgnFileNames">list of files to parse</param>
        /// <param name="moveCount">how many moves to store in the book, e.g. First 20 moves</param>
        /// <param name="skip">
        /// only use every n-th game, e.g. 2 = use every other game, 3 = use every third game.
        /// Useful for limiting the output size if the input PGN is huge
        /// </param>
        /// <param name="callback">callback to take action for every game that is parsed</param>
        /// <param name="errorHandler">callback to handle errors</param>
        /// <returns>List of all parsed games, Encoded</returns>
        public static List <string> CompileBook(
            List <string> pgnFileNames,
            int moveCount = 20,
            int skip      = 1,
            Action <string, string> callback             = null,
            Action <string, PGNParserError> errorHandler = null)
        {
            int halfMoves = moveCount * 2;             // +1 to round up
            var output    = new List <string>();

            foreach (var file in pgnFileNames)
            {
                try
                {
                    Action <PGNParserError> errHandler  = x => { errorHandler(file, x); };
                    Action <PGNGame>        gameHandler = x =>
                    {
                        var encoded = GameEncoder.EncodeGame(x, false);
                        if (encoded.Length > halfMoves)
                        {
                            // make sure we don't cut off the promotion, in case tha last move is a promotion
                            if (encoded[halfMoves] == '*')
                            {
                                encoded = encoded.Substring(0, halfMoves + 2);
                            }
                            else
                            {
                                encoded = encoded.Substring(0, halfMoves);
                            }
                        }

                        string results = "-" + ((int)x.Results.Results).ToString();
                        var    line    = encoded + results;
                        output.Add(line);
                        if (callback != null)
                        {
                            callback(file, line);
                        }
                    };

                    var data   = File.ReadAllText(file);
                    var parser = new PGNParser();
                    var games  = parser.ParsePGN(data, skip, gameHandler, errHandler);
                }
                catch (Exception e)
                {
                    if (errorHandler != null)
                    {
                        errorHandler(file, new PGNParserError()
                        {
                            Exception = e, Message = "Unable to parse input file"
                        });
                    }
                }
            }

            output = output.Where(x => !String.IsNullOrWhiteSpace(x)).OrderBy(x => x).ToList();
            return(output);
        }
Exemplo n.º 5
0
        public void TestMainVariation()
        {
            var parser    = new PGNParser();
            var g         = parser.ParsePGN(GameData);
            var game      = g.Games[0];
            var variation = game.GetMainVariation();

            Assert.IsTrue(variation.All(x => x is PGNMove));
            Assert.IsTrue(variation.Count > 0);
        }
Exemplo n.º 6
0
        public void TestParser1()
        {
            var parser = new PGNParser();

            parser.ParsePGN(GameData);
        }