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
use crate::core::card::Card;
use crate::core::deck::Deck;
use std::ops::{Index, Range, RangeFrom, RangeFull, RangeTo};
extern crate rand;
use rand::seq::*;
use rand::thread_rng;
#[derive(Debug)]
pub struct FlatDeck {
cards: Vec<Card>,
}
impl FlatDeck {
#[must_use]
pub fn len(&self) -> usize {
self.cards.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.cards.is_empty()
}
#[must_use]
pub fn sample(&self, n: usize) -> Vec<Card> {
let mut rng = thread_rng();
self.cards.choose_multiple(&mut rng, n).cloned().collect()
}
pub fn shuffle(&mut self) {
let mut rng = thread_rng();
self.cards.shuffle(&mut rng)
}
pub fn deal(&mut self) -> Option<Card> {
self.cards.pop()
}
}
impl Index<usize> for FlatDeck {
type Output = Card;
#[must_use]
fn index(&self, index: usize) -> &Card {
&self.cards[index]
}
}
impl Index<Range<usize>> for FlatDeck {
type Output = [Card];
#[must_use]
fn index(&self, index: Range<usize>) -> &[Card] {
&self.cards[index]
}
}
impl Index<RangeTo<usize>> for FlatDeck {
type Output = [Card];
#[must_use]
fn index(&self, index: RangeTo<usize>) -> &[Card] {
&self.cards[index]
}
}
impl Index<RangeFrom<usize>> for FlatDeck {
type Output = [Card];
#[must_use]
fn index(&self, index: RangeFrom<usize>) -> &[Card] {
&self.cards[index]
}
}
impl Index<RangeFull> for FlatDeck {
type Output = [Card];
#[must_use]
fn index(&self, index: RangeFull) -> &[Card] {
&self.cards[index]
}
}
pub trait Flattenable {
fn flatten(self) -> FlatDeck;
}
impl Flattenable for Deck {
#[must_use]
fn flatten(self) -> FlatDeck {
FlatDeck {
cards: self.into_iter().collect(),
}
}
}
impl Into<FlatDeck> for Deck {
#[must_use]
fn into(self) -> FlatDeck {
self.flatten()
}
}