diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-12-03 17:25:32 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-12-03 17:25:32 -0500 |
commit | 581e9ec21a85a3d2f908016c28d831e8022cb1f4 (patch) | |
tree | dc098241f07ff9c1cdc226de3fbc2e385c6240cb | |
parent | e6933ac48848b598f870ff5a9d96a0336728620b (diff) |
Change deal function to use random()
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | protocol/src/bridge_engine.rs | 42 | ||||
-rw-r--r-- | protocol/src/card.rs | 47 | ||||
-rw-r--r-- | protocol/src/simple_bots.rs | 4 | ||||
-rw-r--r-- | server/Cargo.toml | 1 | ||||
-rw-r--r-- | server/src/play.rs | 4 | ||||
-rw-r--r-- | webapp/src/components/game.rs | 5 |
7 files changed, 51 insertions, 53 deletions
@@ -1851,6 +1851,7 @@ dependencies = [ "lru", "openidconnect", "protocol", + "rand", "reqwest", "serde", "serde_json", 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); diff --git a/server/Cargo.toml b/server/Cargo.toml index 041eb4e..ca3475d 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -29,3 +29,4 @@ reqwest = "0.11.12" cookie = "0.16.1" time = "0.1.44" async-trait = "0.1.57" +rand = "0.8.4" diff --git a/server/src/play.rs b/server/src/play.rs index 9ae3d54..8d79977 100644 --- a/server/src/play.rs +++ b/server/src/play.rs @@ -1,5 +1,7 @@ use async_trait::async_trait; -use protocol::bridge_engine::{self, GameState, Player}; +use protocol::{bridge_engine::{self, GameState, Player, Bid, Deal}}; +use rand::random; +use serde::{Deserialize, Serialize}; use serde_json::json; use sqlx::{query, PgPool}; use uuid::Uuid; diff --git a/webapp/src/components/game.rs b/webapp/src/components/game.rs index 9d45095..c590357 100644 --- a/webapp/src/components/game.rs +++ b/webapp/src/components/game.rs @@ -1,12 +1,13 @@ use crate::components::{Bidding, Hand, ShowBid, TrickInPlay, TricksPlayed}; use log::{error, info}; use protocol::bridge_engine::{ - deal, BiddingState, DealInPlay, DealInPlayResult, GameState, PlayState, Player, + BiddingState, DealInPlay, DealInPlayResult, GameState, PlayState, Player, }; +use rand::random; use yew::prelude::*; fn init_state() -> GameState { - GameState::new(deal(), Player::East) + GameState::new(random(), Player::East) } #[function_component(Game)] |