63 lines
2.1 KiB
Rust
63 lines
2.1 KiB
Rust
use super::census::Census;
|
|
use super::pairing::Pairing;
|
|
use super::suit_pattern::SuitPattern;
|
|
use super::texture_wetness::wetness;
|
|
use super::windows::windows;
|
|
use crate::card::{Card, Rank};
|
|
use crate::variant::Rules;
|
|
|
|
/// What the board alone looks like.
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub struct Texture {
|
|
pub suits: SuitPattern,
|
|
pub pairing: Pairing,
|
|
/// Most distinct board ranks inside any straight window of the deck (ace low included),
|
|
/// 0 without a board. 3+ means a straight is possible.
|
|
pub connectivity: u8,
|
|
/// Highest board rank, `None` without a board.
|
|
pub high: Option<Rank>,
|
|
/// 0.0 = dry (rainbow, unpaired, disconnected) … 1.0 = very wet.
|
|
/// Scoring table in `texture_wetness`.
|
|
pub wetness: f64,
|
|
}
|
|
|
|
/// Texture of `board` under the variant's deck (straight windows differ in short deck).
|
|
pub fn texture(rules: &Rules, board: &[Card]) -> Texture {
|
|
let census = Census::of(&[], board);
|
|
let suits = suit_pattern(&census);
|
|
let pairing = pairing(&census);
|
|
let connectivity = windows(rules.deck)
|
|
.map(|w| (w & census.board_mask).count_ones() as u8)
|
|
.max()
|
|
.unwrap_or(0);
|
|
Texture {
|
|
suits,
|
|
pairing,
|
|
connectivity,
|
|
high: board.iter().map(|c| c.rank).max(),
|
|
wetness: wetness(suits, connectivity, pairing),
|
|
}
|
|
}
|
|
|
|
fn suit_pattern(census: &Census) -> SuitPattern {
|
|
match (0..4).map(|s| census.board_suited(s)).max().unwrap_or(0) {
|
|
0 | 1 => SuitPattern::Rainbow,
|
|
2 => SuitPattern::TwoTone,
|
|
3 => SuitPattern::Monotone,
|
|
_ => SuitPattern::FourFlush,
|
|
}
|
|
}
|
|
|
|
fn pairing(census: &Census) -> Pairing {
|
|
let groups = |size: u8| (2..=14u8).filter(|p| census.board_count(*p) == size).count();
|
|
let (pairs, trips, quads) = (groups(2), groups(3), groups(4));
|
|
match (quads, trips, pairs) {
|
|
(q, _, _) if q > 0 => Pairing::Heavy,
|
|
(_, t, p) if t > 0 && p > 0 => Pairing::Heavy,
|
|
(_, t, _) if t > 0 => Pairing::Trips,
|
|
(_, _, p) if p >= 2 => Pairing::TwoPaired,
|
|
(_, _, 1) => Pairing::Paired,
|
|
_ => Pairing::Unpaired,
|
|
}
|
|
}
|