cribbage_core/pegging/
three_card_pegging.rs

1use crate::card::Card;
2use crate::pegging::{Pegger, TwoCardPegging};
3use crate::CribbageCoreError;
4
5#[derive(Clone, Copy)]
6pub struct ThreeCardPegging {
7    cards: [Card; 3],
8}
9
10impl ThreeCardPegging {
11    pub(in crate::pegging) fn new(cards: [Card; 3]) -> ThreeCardPegging {
12        ThreeCardPegging { cards }
13    }
14
15    pub fn play_card(
16        self,
17        card: Card,
18        pegger: &mut Pegger,
19    ) -> Result<(u8, TwoCardPegging), (Self, CribbageCoreError)> {
20        let mut cards = self.cards[..].to_vec();
21        let index = cards[..]
22            .iter()
23            .position(|&c| c == card)
24            .ok_or((self, CribbageCoreError::InvalidCard))?;
25        cards.swap_remove(index);
26
27        match pegger.play_card(card) {
28            Ok(points) => Ok((points, TwoCardPegging::new([cards[0], cards[1]]))),
29            Err(error) => Err((self, error)),
30        }
31    }
32}