standard_card 1.4.0

A Lightweight Library for Efficient Card Representation
Documentation
/// Returns true if the cards are a straight (include `wheel straight`).
///
/// A `wheel straight`, an A-x straight, is the lowest possible straight,  using an ace as the low card in a sequence of consecutive ranks.
/// Example the lowest wheel straight in poker is A2345.
///
/// # Examples
/// ```
/// use standard_card::card::create;
/// use standard_card::deck::is_straight;
///
/// let cards = vec![create(3, 0), create(1, 0), create(2, 0)];
/// let actual = is_straight(&cards);
///
/// assert!(actual);
/// ```
pub fn is_straight(cards: &Vec<i32>) -> bool {
    if cards.len() > 13 {
        return false;
    }

    let royal_straight: i32 = 0x1FFF;
    let mut combine: i32 = 0;
    let straight: i32 = royal_straight >> (13 - cards.len());
    let wheel_straight: i32 = 0x1000 | (royal_straight >> (14 - cards.len()));

    for card in cards {
        combine = combine | (card >> 16);
    }

    // loop til the last bit is set
    while (combine & 1) == 0 {
        combine >>= 1;
    }

    return combine == straight || combine == wheel_straight;
}

#[cfg(test)]
mod is_straight {
    use crate::card::create;
    use crate::deck::is_straight;

    #[test]
    fn normal_straight_return_true() {
        // Arrange
        let cards = vec![create(3, 0), create(1, 0), create(2, 0)];

        // Act
        let actual = is_straight(&cards);

        // Assert
        assert!(actual);
    }

    #[test]
    fn not_normal_straight_return_false() {
        // Arrange
        let cards = vec![create(4, 0), create(1, 0), create(2, 1)];

        // Act
        let actual = is_straight(&cards);

        // Assert
        assert!(!actual);
    }

    #[test]
    fn wheel_straight_return_true() {
        // Arrange
        let cards = vec![create(0, 0), create(12, 0), create(1, 0), create(2, 0)];

        // Act
        let actual = is_straight(&cards);

        // Assert
        assert!(actual);
    }

    #[test]
    fn not_wheel_straight_return_false() {
        // Arrange
        let cards = vec![create(0, 0), create(11, 0), create(1, 0), create(2, 1)];

        // Act
        let actual = is_straight(&cards);

        // Assert
        assert!(!actual);
    }
}