Exemplo n.º 1
0
        private static void ReadGikouBook(SBook book, GikouBook gbook ,SPosition pos)
        {
            long key = ExportGikouBook.ComputeKey(pos, gbook);

            SBookState state = book.GetBookState(pos.PositionToString(1));
            if (state != null && state.Moves.Count != 0)
            {
                // すでに登録されてる?
                return;
            }

            // 局面登録
            book.Add(pos, null, 0, 0, 0);

            List<GikouBookEntry> entrys = gbook.GetEntry(key);

            if (entrys != null)
            {
                foreach (GikouBookEntry en in entrys)
                {
                    MoveData move = ConvertMove(pos.Turn, en.Move);

                    book.Add(pos, move, (int)en.Frequency, en.Score, 1);
                }

                foreach (GikouBookEntry en in entrys)
                {
                    MoveData move = ConvertMove(pos.Turn, en.Move);
                    pos.Move(move);
                    ReadGikouBook(book, gbook, pos);
                    pos.UnMove(move, null);
                }
            }
        }
Exemplo n.º 2
0
        /// <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++;
                }
            }
        }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
        private static void ReadGikouBook(SBook book, GikouBook gbook, SPosition pos)
        {
            long key = ExportGikouBook.ComputeKey(pos, gbook);

            SBookState state = book.GetBookState(pos.PositionToString(1));

            if (state != null && state.Moves.Count != 0)
            {
                // すでに登録されてる?
                return;
            }

            // 局面登録
            book.Add(pos, null, 0, 0, 0);

            List <GikouBookEntry> entrys = gbook.GetEntry(key);

            if (entrys != null)
            {
                foreach (GikouBookEntry en in entrys)
                {
                    MoveData move = ConvertMove(pos.Turn, en.Move);

                    book.Add(pos, move, (int)en.Frequency, en.Score, 1);
                }

                foreach (GikouBookEntry en in entrys)
                {
                    MoveData move = ConvertMove(pos.Turn, en.Move);
                    pos.Move(move);
                    ReadGikouBook(book, gbook, pos);
                    pos.UnMove(move, null);
                }
            }
        }
Exemplo n.º 5
0
        /// <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);
        }
Exemplo n.º 6
0
        public static SBook Import(string filename)
        {
            SBook book = new SBook();

            SPosition pos = new SPosition();

            try
            {
                GikouBook gbook = new GikouBook(filename);

                ReadGikouBook(book, gbook, pos);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return book;
        }
Exemplo n.º 7
0
        public static SBook Import(string filename)
        {
            SBook book = new SBook();

            SPosition pos = new SPosition();

            try
            {
                GikouBook gbook = new GikouBook(filename);

                ReadGikouBook(book, gbook, pos);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(book);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 読み込み
        /// </summary>
        /// <param name="filename"></param>
        public static SBook Load(string filename)
        {
            SBook book = null;

            using (Stream sr = new FileStream(filename, FileMode.Open))
            {
                book = Serializer.Deserialize <SBook>(sr);
            }

            foreach (SBookState state in book.BookStates)
            {
                foreach (SBookMove move in state.Moves)
                {
                    // ID->ポインタ変換
                    if (move.NextStateId >= 0 && move.NextStateId < book.BookStates.Count)
                    {
                        move.NextState = book.BookStates[move.NextStateId];
                    }
                }
            }

            return(book);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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;
        }