use hearts::{Hand, PassDirection, Phase, Round, Rules, Seat, Suit, Trick};
pub struct View<'a> {
round: &'a Round,
seat: Seat,
voids: &'a [u8; 4],
scores: [u16; 4],
}
impl<'a> View<'a> {
pub(crate) const fn new(
round: &'a Round,
seat: Seat,
voids: &'a [u8; 4],
scores: [u16; 4],
) -> Self {
Self {
round,
seat,
voids,
scores,
}
}
#[must_use]
pub const fn seat(&self) -> Seat {
self.seat
}
#[must_use]
pub const fn game_scores(&self) -> [u16; 4] {
self.scores
}
#[must_use]
pub const fn rules(&self) -> &'a Rules {
self.round.rules()
}
#[must_use]
pub const fn direction(&self) -> PassDirection {
self.round.direction()
}
#[must_use]
pub const fn phase(&self) -> Phase {
self.round.phase()
}
#[must_use]
pub const fn hand(&self) -> Hand {
self.round.hand(self.seat)
}
#[must_use]
pub fn turn(&self) -> Option<Seat> {
self.round.turn()
}
#[must_use]
pub const fn current_trick(&self) -> Option<Trick> {
self.round.current_trick()
}
#[must_use]
pub fn tricks(&self) -> &'a [Trick] {
self.round.tricks()
}
#[must_use]
pub fn played(&self) -> Hand {
self.round.played()
}
#[must_use]
pub fn hearts_broken(&self) -> bool {
self.round.hearts_broken()
}
#[must_use]
pub fn legal_plays(&self) -> Hand {
self.round.legal_plays(self.seat)
}
#[must_use]
pub fn points_taken(&self, seat: Seat) -> u8 {
self.round.points_taken(seat)
}
#[must_use]
pub const fn hand_len(&self, seat: Seat) -> usize {
self.round.hand(seat).len()
}
#[must_use]
pub const fn passed(&self) -> Option<Hand> {
self.round.passed(self.seat)
}
#[must_use]
pub fn received(&self) -> Option<Hand> {
match self.phase() {
Phase::Passing => None,
Phase::Playing | Phase::Finished => {
self.round.passed(self.direction().giver(self.seat))
}
}
}
#[must_use]
pub const fn is_void(&self, seat: Seat, suit: Suit) -> bool {
self.voids[seat as usize] & 1 << suit as u8 != 0
}
#[must_use]
pub fn known_cards(&self, seat: Seat) -> Hand {
let receiver = self.direction().receiver(self.seat);
if seat != self.seat && seat == receiver && self.phase() != Phase::Passing {
self.passed()
.map_or(Hand::EMPTY, |passed| passed - self.played())
} else {
Hand::EMPTY
}
}
#[must_use]
pub fn unseen(&self) -> Hand {
Hand::ALL - self.hand() - self.played() - self.passed().unwrap_or(Hand::EMPTY)
}
#[must_use]
pub fn possible_cards(&self, seat: Seat) -> Hand {
if seat == self.seat {
return self.hand();
}
let mut cards = self.unseen();
for suit in Suit::ASC {
if self.is_void(seat, suit) {
cards[suit] = hearts::Holding::EMPTY;
}
}
cards | self.known_cards(seat)
}
}