Beispiel #1
0
        /// <summary>
        /// 持ち駒をCSA形式に変換します。
        /// </summary>
        /// <example>
        /// P+00KIOOFU
        /// </example>
        private static string HandToCsa(Board board, BWType turn)
        {
            var handList =
                from pieceType in EnumEx.GetValues <PieceType>()
                let count                     = board.GetCapturedPieceCount(pieceType, turn)
                                      let str = string.Format("00{0}",
                                                              CsaUtil.PieceToStr(new Piece(pieceType)))
                                                let list = Enumerable.Range(1, count).Select(_ => str)
                                                           select string.Join("", list.ToArray());

            // ToArray()しないと、MONOでstring.Joinのコンパイルに失敗します。
            var array = handList.ToArray();

            if (!array.Any())
            {
                return(string.Empty);
            }
            else
            {
                return(string.Format("P{0}{1}",
                                     turn == BWType.Black ? "+" : "-",
                                     string.Join("", array)));
            }
        }
Beispiel #2
0
        /// <summary>
        /// <paramref name="move"/>をCSA形式に変換します。
        /// </summary>
        public static string ToCsa(this Move move)
        {
            if (move == null)
            {
                throw new ArgumentNullException("move");
            }

            if (!move.Validate())
            {
                throw new ArgumentException("move");
            }

            if (move.IsSpecialMove)
            {
                return(move.SpecialMoveType.ToCsa());
            }

            var sb = new StringBuilder();

            sb.Append(
                move.BWType == BWType.Black ? "+" :
                move.BWType == BWType.White ? "-" :
                "");

            if (move.SrcSquare != null)
            {
                sb.Append(move.SrcSquare.File);
                sb.Append(move.SrcSquare.Rank);
            }
            else
            {
                // 駒打の場合
                sb.Append("00");
            }

            if (move.DstSquare != null)
            {
                sb.Append(move.DstSquare.File);
                sb.Append(move.DstSquare.Rank);
            }
            else
            {
                // ほんとはエラー
                sb.Append("00");
            }

            var piece = (
                move.ActionType == ActionType.Drop ?
                new Piece(move.DropPieceType) :
                move.MovePiece);

            if (move.IsPromote)
            {
                // 駒を成った場合は、なった後の駒を出力します。
                piece = new Piece(piece.PieceType, true);
            }

            sb.Append(CsaUtil.PieceToStr(piece));

            return(sb.ToString());
        }