37 lines
1.1 KiB
Rust
37 lines
1.1 KiB
Rust
//! Straight windows of a deck as rank masks (bit = pip value).
|
|
//! Full deck: A-2-3-4-5 up to T-J-Q-K-A. Short deck: A-6-7-8-9 up to T-J-Q-K-A.
|
|
|
|
use super::census::bit;
|
|
use crate::variant::DeckRule;
|
|
|
|
const ACE: u8 = 14;
|
|
|
|
fn span(low: u8, high: u8) -> u16 {
|
|
(low..=high).fold(0, |mask, pip| mask | bit(pip))
|
|
}
|
|
|
|
/// Every five-rank window of the deck, ace-low window first.
|
|
pub fn windows(deck: DeckRule) -> impl Iterator<Item = u16> {
|
|
let low = deck.lowest.value();
|
|
let wheel = bit(ACE) | span(low, low + 3);
|
|
std::iter::once(wheel).chain((low + 4..=ACE).map(|high| span(high - 4, high)))
|
|
}
|
|
|
|
/// The window whose straight has the given high card (the ace-low window has high `lowest + 3`).
|
|
/// Empty (0) when no window has that high card.
|
|
pub fn window_of_high(deck: DeckRule, high: u8) -> u16 {
|
|
let low = deck.lowest.value();
|
|
if high == low + 3 {
|
|
bit(ACE) | span(low, low + 3)
|
|
} else if (low + 4..=ACE).contains(&high) {
|
|
span(high - 4, high)
|
|
} else {
|
|
0
|
|
}
|
|
}
|
|
|
|
/// All ranks of the deck as a mask.
|
|
pub fn deck_mask(deck: DeckRule) -> u16 {
|
|
span(deck.lowest.value(), ACE)
|
|
}
|