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
|
use rand::{distributions::Standard, prelude::Distribution, seq::SliceRandom, random};
use serde::{Deserialize, Serialize};
use strum::EnumCount;
use strum_macros::{EnumCount, EnumIter, FromRepr};
use crate::card::{make_deck, sort_cards, Card, RankOrder, Suit};
#[derive(
PartialEq,
Eq,
Clone,
Copy,
Debug,
FromRepr,
EnumCount,
Serialize,
Deserialize,
EnumIter,
)]
#[repr(u8)]
pub enum Player {
West = 0,
North,
East,
South,
}
impl Player {
pub fn next(&self) -> Self {
self.many_next(1)
}
pub fn many_next(self, i: usize) -> Self {
Player::from_repr(((self as usize + i) % Player::COUNT) as u8).unwrap()
}
pub fn short_str(&self) -> &str {
match self {
Self::West => "W",
Self::North => "N",
Self::East => "E",
Self::South => "W",
}
}
pub fn get_cards<'a>(&self, deal: &'a Deal) -> &'a Vec<Card> {
match self {
Self::West => &deal.west,
Self::North => &deal.north,
Self::East => &deal.east,
Self::South => &deal.south,
}
}
pub fn get_cards_mut<'a>(&self, deal: &'a mut Deal) -> &'a mut Vec<Card> {
match self {
Self::West => &mut deal.west,
Self::North => &mut deal.north,
Self::East => &mut deal.east,
Self::South => &mut deal.south,
}
}
}
impl Distribution<Player> for Standard {
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> Player {
let min = Player::West as u8;
let max = Player::South as u8;
let v = rng.gen_range(min..=max);
Player::from_repr(v).unwrap()
}
}
#[derive(
PartialEq,
Eq,
Clone,
Copy,
Debug,
FromRepr,
EnumCount,
Serialize,
Deserialize,
EnumIter,
)]
#[repr(u8)]
pub enum Vulnerability {
None,
NorthSouth,
EastWest,
All,
}
impl Distribution<Vulnerability> for Standard {
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> Vulnerability {
let min = Vulnerability::None as u8;
let max = Vulnerability::All as u8;
let v = rng.gen_range(min..=max);
Vulnerability::from_repr(v).unwrap()
}
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct Deal {
pub dealer: Player,
pub vulnerability: Vulnerability,
pub north: Vec<Card>,
pub west: Vec<Card>,
pub south: Vec<Card>,
pub east: Vec<Card>,
}
impl Deal {
pub fn empty(dealer: Player, vulnerability: Vulnerability) -> Self {
Self {
dealer,
vulnerability,
north: Vec::with_capacity(13),
west: Vec::with_capacity(13),
south: Vec::with_capacity(13),
east: Vec::with_capacity(13),
}
}
pub fn sort(&mut self, suits: &[Suit; 4], ord: RankOrder) {
sort_cards(suits, ord, self.north.as_mut_slice());
sort_cards(suits, ord, self.west.as_mut_slice());
sort_cards(suits, ord, self.south.as_mut_slice());
sort_cards(suits, ord, self.east.as_mut_slice());
}
}
impl Distribution<Deal> for Standard {
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> Deal {
let mut deck = make_deck();
deck.shuffle(rng);
let mut deck = deck.iter();
let north = deck.by_ref().take(13).cloned().collect();
let west = deck.by_ref().take(13).cloned().collect();
let south = deck.by_ref().take(13).cloned().collect();
let east = deck.by_ref().take(13).cloned().collect();
Deal {
dealer: random(),
vulnerability: random(),
north,
west,
south,
east,
}
}
}
|