cribbage_core/pegging/
four_card_pegging.rs

1use crate::card::Card;
2use crate::pegging::{Pegger, ThreeCardPegging};
3use crate::CribbageCoreError;
4
5#[derive(Clone, Copy)]
6pub struct FourCardPegging {
7    cards: [Card; 4],
8}
9
10impl FourCardPegging {
11    pub fn new(cards: [Card; 4]) -> FourCardPegging {
12        FourCardPegging { cards }
13    }
14
15    pub fn play_card(
16        self,
17        card: Card,
18        pegger: &mut Pegger,
19    ) -> Result<(u8, ThreeCardPegging), (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((
29                points,
30                ThreeCardPegging::new([cards[0], cards[1], cards[2]]),
31            )),
32            Err(error) => Err((self, error)),
33        }
34    }
35}