public void TestGameEncoder() { var parser = new PGNParser(); var g = parser.ParsePGN(GameData); var game = g.Games[0]; var enc = GameEncoder.EncodeGame(game); }
/// <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); }
public List <OpeningMove> GetAvailableMoves(string currentLine) { var len = currentLine.Length; var a = OpeningLines.BinarySearch(x => x.Substring(0, len).CompareTo(currentLine)); var b = OpeningLines.BinarySearchMax(x => x.Substring(0, len).CompareTo(currentLine)); var lines = new List <string>(); for (int i = a; i <= b; i++) { lines.Add(OpeningLines[i]); } var moves = new Dictionary <Move, OpeningMove>(); foreach (var line in lines) { var result = GameEncoder.DecodeResult(line); var move = GameEncoder.DecodeMove(line, len); OpeningMove m = null; if (moves.ContainsKey(move)) { m = moves[move]; } else { m = new OpeningMove() { Move = move }; moves[move] = m; } m.Total++; switch (result) { case (GameResultsType.BlackWins): m.BlackWins++; break; case (GameResultsType.WhiteWins): m.WhiteWins++; break; case (GameResultsType.Tie): m.Tie++; break; } } return(moves.Select(x => x.Value).ToList()); }
private static bool EncodeFile(string file) { FileStream fs = File.Open(file, FileMode.Open, FileAccess.ReadWrite); if (fs == null || fs.Length == 0) { Debug.LogError("EncodeFile Error : can not open '" + file + "' !!!"); return(false); } byte[] fileBuff = new byte[fs.Length]; fs.Read(fileBuff, 0, (int)fs.Length); if (GameEncoder.EncodeBytes(ref fileBuff, 0, fs.Length, "WLGame", 2014) == true) { fs.Seek(0, SeekOrigin.Begin); fs.Write(fileBuff, 0, fileBuff.Length); } fs.Close(); return(true); }