Example #1
0
        /// <summary>
        /// 仅从偏移量角度来筛选出不合法走子,
        /// 不考虑子所在的位置, 不考虑兵的前进后退.
        /// </summary>
        /// <param name="delta">偏移量</param>
        /// <param name="piece">棋子种类</param>
        /// <returns></returns>
        public static Boolean is_valid(CoordinateDelta delta, PieceType piece)
        {
            switch (piece)
            {
            default:
                throw new ArgumentOutOfRangeException("棋子种类越界!");

            case PieceType.ADVISOR:
                if (delta.abs().Equals(new CoordinateDelta(1, 1)))
                {
                    return(true);
                }
                return(false);

            case PieceType.BISHOP:
                if (delta.abs().Equals(new CoordinateDelta(2, 2)))
                {
                    return(true);
                }
                return(false);

            case PieceType.CANNON:
            case PieceType.ROOK:
                if (delta.x == 0 || delta.y == 0)
                {
                    return(true);
                }
                return(false);

            case PieceType.KING:
            case PieceType.PAWN:
                if (delta.abs().Equals(new CoordinateDelta(1, 0)) ||
                    delta.abs().Equals(new CoordinateDelta(0, 1)))
                {
                    return(true);
                }
                return(false);

            case PieceType.KNIGHT:
                if (delta.abs().Equals(new CoordinateDelta(2, 1)) ||
                    delta.abs().Equals(new CoordinateDelta(1, 2)))
                {
                    return(true);
                }
                return(false);
            }
        }
        /// <summary>
        /// 判断忽略棋规的情况下棋步是否合法
        /// </summary>
        /// <param name="start">起始坐标</param>
        /// <param name="end">终止坐标</param>
        private Boolean is_raw_move(Coordinate start, Coordinate end)
        {
            ChessColour     player = this.current_player;
            PieceType       piece  = this[start].type;
            CoordinateDelta delta  = end - start;

            switch (piece)
            {
            default:
                throw new ArgumentOutOfRangeException("棋子种类越界!");

            case PieceType.ADVISOR:
            case PieceType.KING:
                if (ChessPosition.is_inside_castle(end, player))
                {
                    return(true);
                }
                return(false);

            case PieceType.BISHOP:
                if (ChessPosition.is_castle_side(end, player))
                {
                    if (this.check_bishop_eye(start, delta))
                    {
                        return(true);
                    }
                }
                return(false);

            case PieceType.CANNON:
                if (this[end].type == PieceType.NONE)
                {
                    if (this.count_piece(start, delta) == 0)
                    {
                        return(true);
                    }
                    return(false);
                }
                if (this.count_piece(start, delta) == 1)
                {
                    return(true);
                }
                return(false);

            case PieceType.KNIGHT:
                if (this.check_knight_leg(start, delta))
                {
                    return(true);
                }
                return(false);

            case PieceType.PAWN:
                if (ChessPosition.get_move_direction(delta, player) ==
                    MoveDirection.BACKWARD)
                {
                    return(false);
                }
                if (ChessPosition.is_castle_side(start, player))
                {
                    if (!delta.abs().Equals(new CoordinateDelta(0, 1)))
                    {
                        return(false);
                    }
                }
                return(true);

            case PieceType.ROOK:
                if (this.count_piece(start, delta) == 0)
                {
                    return(true);
                }
                return(false);
            }
        }