From 1f52e2e448b464e95530cab9e1b7d9177ada3279 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sat, 7 Jan 2023 16:23:56 -0500 Subject: Add dealer and vulnerability to the Deal struct --- protocol/src/core.rs | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) (limited to 'protocol/src/core.rs') 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 for Standard { } } +#[derive( + PartialEq, + Eq, + Clone, + Copy, + Debug, + FromRepr, + EnumCount, + Serialize, + Deserialize, + EnumIter, +)] +#[repr(u8)] +pub enum Vulnerability { + None, + NorthSouth, + EastWest, + All, +} + +impl Distribution for Standard { + fn sample(&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, pub west: Vec, pub south: Vec, @@ -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 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, -- cgit v1.2.3