/// <summary> /// SBookをYaneuraou book形式に変換して保存する /// </summary> /// <param name="book"></param> /// <param name="filename"></param> public static void ExportYaneuraOUbook(this SBook book, string filename) { // using (StreamWriter wr = new StreamWriter(filename, false, Encoding.UTF8)) { wr.WriteLine("#YANEURAOU-DB2016 1.00"); SPosition position = new SPosition(); book.ClearCount(); int cnt = 0; foreach (SBookState state in book.BookStates) { if (state.Count == 0 && ((state.Id == 0) || (state.Position != string.Empty))) { if (state.Position != string.Empty) { Sfen.PositionFromString(position, state.Position); } // 指し手の出力 ルートからの局面以外はやねうら王2016には正しく認識されない WriteMoves(state, position, wr, 1); } cnt++; } } }
/// <summary> /// SBookをGikou形式に変換して保存する /// </summary> /// <param name="book"></param> /// <param name="filename"></param> public static void ExportGikou(this SBook book, string filename) { // GikouBook gikouBook = new GikouBook(); { SPosition position = new SPosition(); book.ClearCount(); int cnt = 0; foreach (SBookState state in book.BookStates) { if (state.Count == 0 && ((state.Id == 0) || (state.Position != string.Empty))) { if (state.Position != string.Empty) { Sfen.PositionFromString(position, state.Position); } // 指し手の出力 ルートからの局面以外はやねうら王2016には正しく認識されない WriteMoves(state, position, gikouBook); } cnt++; } } gikouBook.Save(filename); }
/// <summary> /// SBookをAperyBookに変換して保存する /// </summary> /// <param name="book"></param> /// <param name="filename"></param> public static void ExportApery(this SBook book, string filename) { // 初期局面の出力 AperyBook aperyBook = new AperyBook(); SPosition position = new SPosition(); book.ClearCount(); int cnt = 0; foreach (SBookState state in book.BookStates) { if (state.Position != string.Empty) { // 局面が入っている場合 Sfen.PositionFromString(position, state.Position); } // 指し手出力 if (state.Count == 0 && ((state.Id == 0) || (state.Position != string.Empty))) { WriteMoves(state, position, aperyBook); } cnt++; } aperyBook.Save(filename); }
/// <summary> /// sfen形式の棋譜読み込み /// </summary> /// <param name="notation"></param> /// <param name="sr"></param> public static void ReadNotation(SPosition pos, string str) { Tokenizer tok = new Tokenizer(str); string token; token = tok.Token(); if (token == "position") { token = tok.Token(); } if (token == "startpos") { // 処理なし } else if (token == "sfen") { Sfen.ReadPosition(pos, tok.TokenPosition()); } else if (token == "moves") { tok.Push(token); } else { } token = tok.Token(); if (token == "moves") { while ((token = tok.Token()) != string.Empty) { MoveData moveData = ParseMove(pos, token); if (moveData == null) { break; } if (!pos.Move(moveData)) { break; } } } }
/// <summary> /// 指し手の出力 /// </summary> /// <param name="bookstate"></param> /// <param name="position"></param> /// <param name="aperyBook"></param> private static void WriteMoves(SBookState bookstate, SPosition position, StreamWriter wr, int depth) { if (bookstate == null) { return; } if (bookstate.Count != 0) { return; // 既に出力した } int count = 0; foreach (SBookMove move in bookstate.Moves) { if (move.Weight != 0) { count++; } } bookstate.Count++; if (count != 0) { // 局面の出力 wr.WriteLine("sfen " + position.PositionToString(depth)); foreach (SBookMove move in bookstate.Moves) { if (move.Weight != 0) { // 指し手の出力 MoveData moveData = move.GetMoveData(); string next_str = "none"; SBookMove next_move = GetNextMove(move.NextState); if (next_move != null) { MoveData nextMoveData = next_move.GetMoveData(); next_str = Sfen.MoveToString(nextMoveData); } wr.WriteLine("{0} {1} {3} {4} {2}", Sfen.MoveToString(moveData), next_str, move.Weight, move.Value, move.Depth); } } } foreach (SBookMove move in bookstate.Moves) { // 指し手の出力 MoveData moveData = move.GetMoveData(); if (position.Move(moveData)) { // 再帰呼び出し WriteMoves(move.NextState, position, wr, depth + 1); position.UnMove(moveData, null); } } }
public static SBook ImportYaneuraOu(string filename) { SBook book = new SBook(); try { using (StreamReader sr = new StreamReader(filename)) { string line; SPosition pos = new SPosition(); book.Add(pos, null, 0, 0, 0); // 平手初期局面をいれる while ((line = sr.ReadLine()) != null) { if (line.StartsWith("sfen") || line.StartsWith("startpos") || line.StartsWith("position")) { Sfen.ReadNotation(pos, line); } else if (line.StartsWith("#") || line.StartsWith("//")) { // コメント } else { string[] str_array = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (str_array.Length >= 5) { MoveData move = Sfen.ParseMove(pos, str_array[0]); if (move.Piece == Piece.NoPiece || pos.GetPiece(move.ToSquare).ColorOf() == move.Piece.ColorOf()) { Debug.WriteLine("bb"); } if (move != null && move.MoveType.IsMoveWithoutPass()) { int weight; int depth = 0; int value = 0; int.TryParse(str_array[2], out value); int.TryParse(str_array[3], out depth); if (int.TryParse(str_array[4], out weight)) { // 指し手の追加 book.Add(pos, move, weight, value, depth); } } } } } } // idの付け直し book.SetIds(); } catch (Exception ex) { throw ex; } return(book); }