1use crate::cards::StandardCard;
28use crate::deck_of_cards::STANDARD_DECK_OF_CARDS;
29use crate::errors::{BASE_DECK_ERROR_CODE, ErrorCode};
30use core::fmt::{self, Debug, Display};
31use rand::RngExt;
32use std::{collections::VecDeque, collections::vec_deque::IntoIter as DequeIntoIter};
33
34pub trait DefaultCollection: Copy + Clone + Debug + Ord + Display {
36 fn default_collection() -> VecDeque<Self>;
38 fn multiple_collections(n: usize) -> VecDeque<Self> {
40 let c = Self::default_collection();
41 let set_len = c.len();
42 c.into_iter().cycle().take(set_len * n).collect()
43 }
44 fn create_deck() -> Deck<Self> {
46 Deck::default()
47 }
48}
49
50impl DefaultCollection for StandardCard {
51 fn default_collection() -> VecDeque<Self> {
52 STANDARD_DECK_OF_CARDS.iter().copied().collect()
53 }
54 fn multiple_collections(n: usize) -> VecDeque<Self> {
55 STANDARD_DECK_OF_CARDS
56 .iter()
57 .copied()
58 .cycle()
59 .take(52usize * n)
60 .collect()
61 }
62}
63
64#[repr(C)]
66#[derive(
67 Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq, Ord, PartialOrd, Hash,
68)]
69pub struct Deck<C: Copy + Clone + Debug + Ord + Display>(pub(crate) VecDeque<C>);
70
71#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
73pub enum DeckError {
74 NoCard,
76}
77
78impl Display for DeckError {
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80 match *self {
81 DeckError::NoCard => f.write_str("No Cards are left in the deck"),
82 }
83 }
84}
85
86impl ErrorCode for DeckError {
87 fn error_code(&self) -> i32 {
88 BASE_DECK_ERROR_CODE
89 + match *self {
90 DeckError::NoCard => 1,
91 }
92 }
93}
94impl<C: Copy + Clone + Debug + Ord + Display> Deck<C> {
95 pub fn draw(&mut self) -> Result<C, DeckError> {
100 self.0.pop_front().ok_or(DeckError::NoCard)
101 }
102 pub fn draw_random(&mut self) -> Result<C, DeckError> {
108 if self.0.is_empty() {
109 return Err(DeckError::NoCard);
110 }
111
112 let len = self.0.len();
113 if len == 1 {
114 self.0.pop_front().ok_or(DeckError::NoCard)
115 } else {
116 let index = crate::get_rng().random_range(0..len);
117 self.0.remove(index).ok_or(DeckError::NoCard)
118 }
119 }
120 pub fn draw_many_random(&mut self, count: usize) -> Vec<C> {
124 if self.0.is_empty() || count == 0 {
125 return Vec::with_capacity(0);
126 }
127 let len = self.0.len();
128 let mut rng = crate::get_rng();
129 let mid = len.saturating_sub(count);
130 for i in (len..mid).rev() {
132 let idx = rng.random_range(0..(i + 1));
133 self.0.swap(i, idx);
135 }
136 self.0.split_off(mid).into()
137 }
138 pub fn draw_many(&mut self, count: usize) -> Vec<C> {
140 if self.0.is_empty() || count == 0 {
141 return Vec::with_capacity(0);
142 }
143 let len = self.0.len();
144 if count >= len {
145 self.0.drain(..).collect()
146 } else {
147 self.0.split_off(len - count).into()
148 }
149 }
150 pub fn draw_rest(&mut self) -> Vec<C> {
152 self.0.drain(0..).collect()
153 }
154 pub fn draw_rest_random(&mut self) -> Vec<C> {
156 self.shuffle();
157 self.0.drain(0..).collect()
158 }
159 pub fn insert(&mut self, card: C) {
161 self.0.push_back(card);
162 }
163 pub fn into_vec(self) -> Vec<C> {
165 self.0.into()
166 }
167 pub fn shuffle(&mut self) {
169 let mut rng = crate::get_rng();
170 for i in (1..self.len()).rev() {
171 let idx = rng.random_range(0..(i + 1));
172 self.0.swap(i, idx);
174 }
175 }
176 pub fn peek(&self) -> Option<&C> {
178 self.0.front()
179 }
180 pub fn from_cards(cards: Vec<C>) -> Self {
182 Self(cards.into())
183 }
184 pub fn is_empty(&self) -> bool {
186 self.0.is_empty()
187 }
188 pub fn len(&self) -> usize {
190 self.0.len()
191 }
192 pub fn empty() -> Self {
194 Self(VecDeque::new())
195 }
196}
197impl<C: DefaultCollection> Default for Deck<C> {
198 fn default() -> Deck<C> {
199 Self(C::default_collection())
200 }
201}
202
203impl<C: Copy + Clone + Debug + Ord + Display> Extend<C> for Deck<C> {
204 fn extend<I: IntoIterator<Item = C>>(&mut self, iter: I) {
205 self.0.extend(iter)
206 }
207}
208
209impl<C: Copy + Clone + Debug + Ord + Display> core::iter::FromIterator<C> for Deck<C> {
210 fn from_iter<I: IntoIterator<Item = C>>(iter: I) -> Self {
211 let mut c = Self::empty();
212 c.extend(iter);
213 c
214 }
215}
216
217impl<C: Copy + Clone + Debug + Ord + Display> IntoIterator for Deck<C> {
218 type Item = C;
219 type IntoIter = DequeIntoIter<C>;
220
221 fn into_iter(self) -> Self::IntoIter {
222 self.0.into_iter()
223 }
224}
225
226#[derive(Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
233pub struct DeckBuilder<C: Copy + Clone + Debug + Ord + Display>(Vec<C>);
234impl<C: Copy + Clone + Debug + Ord + Display> DeckBuilder<C> {
235 pub fn sets(mut self, n: usize) -> Self {
239 let set_len = self.0.len();
240 self.0 = self.0.into_iter().cycle().take(set_len * n).collect();
241 self
242 }
243 pub fn build(self) -> Deck<C> {
245 Deck(self.0.into())
246 }
247}
248impl<C: DefaultCollection> Default for DeckBuilder<C> {
249 fn default() -> DeckBuilder<C> {
250 Self(C::default_collection().into())
251 }
252}
253
254impl<C: Copy + Clone + Debug + Ord + Display> From<Vec<C>> for Deck<C> {
255 fn from(cards: Vec<C>) -> Self {
256 Self(cards.into())
257 }
258}
259
260impl<C: Copy + Clone + Debug + Ord + Display> From<VecDeque<C>> for Deck<C> {
261 fn from(cards: VecDeque<C>) -> Self {
262 Self(cards)
263 }
264}
265
266impl<C: Copy + Clone + Debug + Ord + Display> From<Vec<C>> for DeckBuilder<C> {
267 fn from(cards: Vec<C>) -> Self {
268 Self(cards)
269 }
270}
271impl<C: Copy + Clone + Debug + Ord + Display> From<VecDeque<C>> for DeckBuilder<C> {
272 fn from(cards: VecDeque<C>) -> Self {
273 Self(cards.into())
274 }
275}