diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2023-01-07 16:23:56 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2023-01-07 16:23:56 -0500 |
commit | 1f52e2e448b464e95530cab9e1b7d9177ada3279 (patch) | |
tree | 9290facc77bfb77b078e973e325f2e3d62216464 /protocol/src/core.rs | |
parent | 0f7dacea15d6e22123c3290c52515a772fc7ee92 (diff) |
Add dealer and vulnerability to the Deal struct
Diffstat (limited to 'protocol/src/core.rs')
-rw-r--r-- | protocol/src/core.rs | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/protocol/src/core.rs b/protocol/src/core.rs index 7939fb2..7d055ac 100644 --- a/protocol/src/core.rs +++ b/protocol/src/core.rs @@ -1,9 +1,9 @@ -use rand::{prelude::Distribution, distributions::Standard, seq::SliceRandom}; +use rand::{distributions::Standard, prelude::Distribution, seq::SliceRandom, random}; +use serde::{Deserialize, Serialize}; use strum::EnumCount; -use strum_macros::{EnumCount, FromRepr, EnumIter}; -use serde::{Serialize, Deserialize}; +use strum_macros::{EnumCount, EnumIter, FromRepr}; -use crate::card::{Card, Suit, RankOrder, sort_cards, make_deck}; +use crate::card::{make_deck, sort_cards, Card, RankOrder, Suit}; #[derive( PartialEq, @@ -71,8 +71,39 @@ impl Distribution<Player> for Standard { } } +#[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>, @@ -80,8 +111,10 @@ pub struct Deal { } impl Deal { - pub fn empty() -> Self { + 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), @@ -107,6 +140,8 @@ impl Distribution<Deal> for Standard { 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, |