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
78
79
80
81
82
83
84
85
86
use enum_map::Enum;
use seq_macro::seq;

use super::Zone;
use crate::card::Card;
use crate::card_sequence::Sequenced as _;
use crate::piles::{Cards, Split};

pub mod flip_top;
pub mod place;
pub mod take;

mod sequence;

use self::sequence::TableauSequence;

seq!(N in 0..7 {
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Enum)]
    #[repr(u8)]
    pub enum Index {
        #(
            Pile~N,
        )*
    }
});

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Tableau {
    pile: Split,
}

#[derive(Debug, Clone, Copy)]
pub struct View<'a> {
    pub face_down_fan: &'a [Card],
    pub face_up_fan: &'a [Card],
}

impl Tableau {
    pub const fn empty() -> Self {
        Self {
            pile: Split::empty(),
        }
    }
}

impl From<Split> for Tableau {
    fn from(pile: Split) -> Self {
        pile.right()
            .is_sequential::<TableauSequence>()
            .expect("Face-up segment must be sequential");

        Self { pile }
    }
}

impl From<Cards> for Tableau {
    fn from(pile: Cards) -> Self {
        Self {
            pile: pile.into_left_split(),
        }
    }
}

impl FromIterator<Card> for Tableau {
    fn from_iter<I>(cards: I) -> Self
    where
        I: IntoIterator<Item = Card>,
    {
        cards.into_iter().collect::<Cards>().into()
    }
}

impl Zone for Tableau {
    type View<'a> = View<'a>
    where
        Self: 'a;

    fn as_view(&self) -> Self::View<'_> {
        let (face_down_fan, face_up_fan) = self.pile.cards_split();

        View {
            face_down_fan,
            face_up_fan,
        }
    }
}