summaryrefslogtreecommitdiff
path: root/protocol
diff options
context:
space:
mode:
authorKjetil Orbekk <kj@orbekk.com>2022-12-03 17:25:32 -0500
committerKjetil Orbekk <kj@orbekk.com>2022-12-03 17:25:32 -0500
commit581e9ec21a85a3d2f908016c28d831e8022cb1f4 (patch)
treedc098241f07ff9c1cdc226de3fbc2e385c6240cb /protocol
parente6933ac48848b598f870ff5a9d96a0336728620b (diff)
Change deal function to use random()
Diffstat (limited to 'protocol')
-rw-r--r--protocol/src/bridge_engine.rs42
-rw-r--r--protocol/src/card.rs47
-rw-r--r--protocol/src/simple_bots.rs4
3 files changed, 43 insertions, 50 deletions
diff --git a/protocol/src/bridge_engine.rs b/protocol/src/bridge_engine.rs
index fa5c995..0eb9c1e 100644
--- a/protocol/src/bridge_engine.rs
+++ b/protocol/src/bridge_engine.rs
@@ -1,7 +1,7 @@
-use crate::card::{Card, Deal, RankOrder, Suit};
+use crate::card::{Card, RankOrder, Suit, sort_cards, make_deck};
use anyhow::{anyhow, bail};
use log::{error, info};
-use rand::{prelude::Distribution, distributions::Standard};
+use rand::{prelude::{Distribution, SliceRandom}, distributions::Standard};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
@@ -620,11 +620,39 @@ impl GameState {
}
}
-pub fn deal() -> Deal {
- let mut rng = rand::thread_rng();
- let mut deal = crate::card::deal(&mut rng);
- deal.sort(&SUIT_DISPLAY_ORDER, RankOrder::Descending);
- deal
+#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
+pub struct Deal {
+ pub north: Vec<Card>,
+ pub west: Vec<Card>,
+ pub south: Vec<Card>,
+ pub east: Vec<Card>,
+}
+
+impl Deal {
+ 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 {
+ north,
+ west,
+ south,
+ east,
+ }
+ }
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
diff --git a/protocol/src/card.rs b/protocol/src/card.rs
index e27f29a..3d6081d 100644
--- a/protocol/src/card.rs
+++ b/protocol/src/card.rs
@@ -1,14 +1,15 @@
-use serde::{Deserialize, Serialize};
use anyhow::anyhow;
-use rand::prelude::SliceRandom;
-use rand::Rng;
+
+pub(crate) use serde::{Deserialize, Serialize};
use std::fmt;
use strum::EnumCount;
use strum::IntoEnumIterator;
use strum_macros::EnumCount;
use strum_macros::EnumIter;
-#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, EnumIter, EnumCount, Serialize, Deserialize)]
+#[derive(
+ PartialOrd, Ord, PartialEq, Eq, Clone, Copy, EnumIter, EnumCount, Serialize, Deserialize,
+)]
pub enum Suit {
Club,
Diamond,
@@ -152,7 +153,7 @@ impl std::str::FromStr for Card {
}
}
-fn make_deck() -> Vec<Card> {
+pub fn make_deck() -> Vec<Card> {
let mut result = vec![];
for suit in Suit::iter() {
for rank in Rank::iter() {
@@ -240,39 +241,3 @@ mod tests {
);
}
}
-
-#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
-pub struct Deal {
- pub north: Vec<Card>,
- pub west: Vec<Card>,
- pub south: Vec<Card>,
- pub east: Vec<Card>,
-}
-
-impl Deal {
- 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());
- }
-}
-
-pub fn deal<R>(rng: &mut R) -> Deal
-where
- R: Rng,
-{
- 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 {
- north,
- west,
- south,
- east,
- }
-}
diff --git a/protocol/src/simple_bots.rs b/protocol/src/simple_bots.rs
index 7997418..f5be424 100644
--- a/protocol/src/simple_bots.rs
+++ b/protocol/src/simple_bots.rs
@@ -16,7 +16,7 @@ mod tests {
use super::*;
use log::info;
use rand::random;
- use crate::bridge_engine::{deal, GameState, BiddingState, Bidding, BiddingStatePlayerView};
+ use crate::bridge_engine::{BiddingState, Bidding, BiddingStatePlayerView};
#[tokio::test]
async fn always_passing_bot_passes() {
@@ -25,7 +25,7 @@ mod tests {
let player_position = random();
let bidding_state = BiddingState {
dealer,
- deal: deal(),
+ deal: random(),
bidding: Bidding::new(dealer),
};
let player_view = BiddingStatePlayerView::from_bidding_state(&bidding_state, player_position);