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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::mem::take;
use super::{Pile, PileMut, Split};
use crate::card::Card;
use crate::enums::EnumSequence as _;
use crate::shuffle::Shuffle;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Cards {
cards: Vec<Card>,
}
impl Cards {
pub const fn empty() -> Self {
Self { cards: Vec::new() }
}
pub fn deck() -> Self {
Self {
cards: Card::values_iter().collect(),
}
}
pub fn shuffled_deck<S>(shuffle: S) -> Self
where
S: Shuffle,
{
let cards = Card::values_iter().collect::<Vec<_>>();
let permutation = shuffle.permutation(cards.len());
let cards = permutation.permute(&cards);
Self { cards }
}
pub fn into_split(self, left_len: usize) -> Split {
Split::from_cards(self, left_len)
}
pub fn into_left_split(self) -> Split {
Split::from_left(self)
}
pub fn into_right_split(self) -> Split {
Split::from_right(self)
}
#[must_use]
pub fn flipped(mut self) -> Self {
self.cards.reverse();
self
}
}
impl From<Vec<Card>> for Cards {
fn from(cards: Vec<Card>) -> Self {
Self { cards }
}
}
impl<const N: usize> From<[Card; N]> for Cards {
fn from(cards: [Card; N]) -> Self {
Self {
cards: cards.into(),
}
}
}
impl FromIterator<Card> for Cards {
fn from_iter<I>(cards: I) -> Self
where
I: IntoIterator<Item = Card>,
{
Self {
cards: cards.into_iter().collect(),
}
}
}
impl IntoIterator for Cards {
type Item = Card;
type IntoIter = impl Iterator<Item = Self::Item>
+ DoubleEndedIterator<Item = Self::Item>
+ ExactSizeIterator<Item = Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.cards.into_iter()
}
}
impl<'a> IntoIterator for &'a Cards {
type Item = Card;
type IntoIter = impl Iterator<Item = Self::Item>
+ DoubleEndedIterator<Item = Self::Item>
+ ExactSizeIterator<Item = Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.cards.iter().copied()
}
}
impl Extend<Card> for Cards {
fn extend<I>(&mut self, cards: I)
where
I: IntoIterator<Item = Card>,
{
self.place(cards);
}
}
impl Pile for Cards {
fn cards(&self) -> &[Card] {
&self.cards
}
fn len(&self) -> usize {
self.cards.len()
}
fn is_empty(&self) -> bool {
self.cards.is_empty()
}
}
impl PileMut for Cards {
fn take_at_most(&mut self, count: usize) -> Cards {
let index = self.len().saturating_sub(count);
self.cards.split_off(index).into()
}
fn take_at_most_one(&mut self) -> Option<Card> {
self.cards.pop()
}
fn take_or_none(&mut self, count: usize) -> Option<Cards> {
self.len()
.checked_sub(count)
.map(|index| self.cards.split_off(index).into())
}
fn take_exactly(&mut self, count: usize) -> Cards {
assert!(count <= self.len());
let index = self.len() - count;
self.cards.split_off(index).into()
}
fn take_exactly_one(&mut self) -> Card {
assert!(!self.is_empty());
self.take_at_most_one().unwrap()
}
fn take_all(&mut self) -> Cards {
take(self)
}
fn place<I>(&mut self, cards: I)
where
I: IntoIterator<Item = Card>,
{
self.cards.extend(cards);
}
fn place_one(&mut self, card: Card) {
self.cards.push(card);
}
}