Exemple #1
0
        /////////////////////////////////////////////////////////////////////

        /// <summary>
        /// sfen形式の盤情報を読み込み
        /// </summary>
        /// <param name="notation"></param>
        /// <param name="sr"></param>
        private static int ReadPosition(SPosition position, string sfen, int index = 0)
        {
            int   file, rank;
            int   ch;
            Piece piece;
            int   piece_num;

            SPosition pos = position;

            file = 0;
            rank = 0;

            if (sfen != string.Empty)
            {
                pos.BoardClear();
            }

            // 局面データ
            while (index < sfen.Length)
            {
                ch = sfen[index];
                index++;

                if (ch == ' ')
                {
                    break;
                }

                if (ch == '/')
                {
                    // 段の区切り
                    file  = 0;
                    rank += 1;
                    if (rank >= Square.NRANK)
                    {
                        break;
                    }
                }
                else if (ch >= '0' && ch <= '9')
                {
                    // 数字は駒がないところ
                    file += ch - '0';
                }
                else
                {
                    piece = Piece.NoPiece;

                    // なり
                    if (ch == '+')
                    {
                        piece |= Piece.PromotionFlag;
                        if (index < sfen.Length)
                        {
                            ch = sfen[index];
                            index++;
                            if (ch == ' ')
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }

                    // 後手
                    if (ch >= 'a' && ch <= 'z')
                    {
                        piece |= Piece.WhiteFlag;
                        ch     = char.ToUpper((char)ch);
                    }

                    // charをPiece変換
                    PieceType pieceType;
                    if (CharToPieceHashtable.TryGetValue((char)ch, out pieceType))
                    {
                        piece |= (Piece)pieceType;
                    }
                    else
                    {
                        // 不明な文字列
                        Debug.Print("parse error");
                        piece = Piece.NoPiece;
                    }

                    if (piece.TypeOf() != PieceType.NoPieceType)
                    {
                        // 駒がある
                        if (file < Square.NFILE)
                        {
                            pos.SetPiece(file, rank, piece);
                            file++;
                        }
                    }
                }
            }

            // 手番
            while (index < sfen.Length)
            {
                ch = sfen[index];
                index++;

                if (ch == ' ')
                {
                    break;
                }

                if (ch == 'w')
                {
                    pos.Turn = PlayerColor.White;
                }
                else if (ch == 'b')
                {
                    pos.Turn = PlayerColor.Black;
                }
            }

            // 持ち駒
            while (index < sfen.Length)
            {
                ch = sfen[index];
                index++;
                if (ch == ' ')
                {
                    break;
                }

                piece_num = 1;

                // 枚数
                if (ch >= '0' && ch <= '9')
                {
                    piece_num = ch - '0';
                    if (index < sfen.Length)
                    {
                        ch = sfen[index];
                        index++;
                    }
                    else
                    {
                        break;
                    }

                    if (ch == ' ')
                    {
                        break;
                    }
                    else if (ch >= '0' && ch <= '9')
                    {
                        piece_num = (piece_num * 10) + (ch - '0');
                        if (index < sfen.Length)
                        {
                            ch = sfen[index];
                            index++;
                        }
                        else
                        {
                            break;
                        }

                        if (ch == ' ')
                        {
                            break;
                        }
                    }
                }

                PieceType piece_type;

                if (CharToPieceHashtable.TryGetValue(char.ToUpper((char)ch), out piece_type))
                {
                    if (char.IsUpper((char)ch))
                    {
                        // 大文字は先手
                        pos.SetBlackHand(piece_type, piece_num);
                    }
                    else
                    {
                        // 後手
                        pos.SetWhiteHand(piece_type, piece_num);
                    }
                }
            }

            // n手目 (読み飛ばす
            while (index < sfen.Length)
            {
                ch = sfen[index];
                index++;
                if (ch == ' ')
                {
                    break;
                }
            }

            return(index);
        }