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
78use {
rand::Rng,
std::{
collections::HashMap,
default::default,
},
};
pub type Cards = Vec<Card>;
const MAX_CARDS_SMALL: u8 = 7;
pub fn shuffle() -> Cards
{
let mut deck: HashMap<Card, u8> = HashMap::new();
let mut shuffled: Cards = default();
for _ in 0..50
{
loop
{
let new_card = match rand::thread_rng().gen_range(0..8)
{
0 => Card::One,
1 => Card::Two,
2 => Card::Three,
3 => Card::Four,
4 => Card::Five,
5 => Card::Six,
6 => Card::Seven,
7 => Card::Ten,
_ => unreachable!(),
};
let chosen_card_count = deck.entry(new_card).or_insert(0);
// println!("new_card: {new_card:?}, chosen_card_count: {chosen_card_count}");
let is_new_small = new_card != Card::Ten && *chosen_card_count < MAX_CARDS_SMALL;
let is_new_ten = new_card == Card::Ten && *chosen_card_count == 0;
if is_new_small || is_new_ten
{
*chosen_card_count += 1u8;
shuffled.push(new_card);
break
}
}
}
shuffled
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone, Debug)]
pub enum Card
{
One = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Ten = 10,
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone, Debug)]
pub enum StoredCard
{
UnpairedCard(Card),
PairedCard(Card),
}
impl StoredCard
{
pub fn get_score(self) -> u8
{
match self
{
StoredCard::UnpairedCard(card) => card as u8,
StoredCard::PairedCard(card) => 2 * card as u8,
}
}
}