1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use derive_more::{Display, Error};

use super::{Tableau, TableauSequence};
use crate::card::Rank;
use crate::card_sequence::{SequenceError, Sequenced as _};
use crate::piles::{Cards, Pile as _, PileMut as _};
use crate::{action, undo};

#[derive(Debug)]
pub struct Action(pub Cards);

#[derive(Debug)]
pub struct Value {
    count: usize,
}

#[derive(Debug, Display, Error)]
pub enum Error {
    #[display(fmt = "The cards do not follow the current top card")]
    DoesNotFollow,

    #[display(fmt = "There are no cards to place")]
    Empty,

    #[display(fmt = "The top card of the pile is face-down")]
    FaceDown,

    #[display(fmt = "The cards are not in order for the tableau")]
    OutOfSequence(SequenceError),

    #[display(fmt = "The pile is empty but the bottom card is not a king")]
    RequiresKing,
}

impl action::Action for Action {
    type State<'s> = ();
    type Value = Value;
    type Error = Error;
}

impl action::Target<Action> for Tableau {
    fn update(&mut self, Action(pile): Action, _: ()) -> Result<Value, Error> {
        // Get the bottom card for later and check for emptiness at the same time.
        let bottom_card = *pile.cards().first().ok_or(Error::Empty)?;

        // Ensure that the pile to place is in order
        pile.is_sequential::<TableauSequence>()
            .map_err(Error::OutOfSequence)?;

        if let Some(&pile_top_card) = self.pile.right().cards().last() {
            // There's at least one face-up card on the pile, so ensure the sequence. Note that the
            // card lower in the pile *follows* the higher card because they're in descending rank
            // order.
            [pile_top_card, bottom_card]
                .is_sequential::<TableauSequence>()
                .map_err(|_| Error::DoesNotFollow)?;
        } else if !self.pile.left().is_empty() {
            // There's no face-up cards in the pile and at least one face-down card.
            return Err(Error::FaceDown);
        } else if bottom_card.rank != Rank::King {
            // There are no cards in the pile at all.
            return Err(Error::RequiresKing);
        }

        let count = pile.len();
        self.pile.place(pile);

        Ok(Value { count })
    }
}

impl undo::Target<Value> for Tableau {
    fn revert(&mut self, undo: Value) {
        // Take them and let them drop. Whoever gave us those cards tracked it in their own Undo.
        self.pile.right_mut().take_exactly(undo.count);
    }
}