From 519057f1e8168f1a28320aa812bcc3fba08dbb52 Mon Sep 17 00:00:00 2001 From: MA Beaudet Date: Mon, 8 Nov 2021 13:17:23 +0100 Subject: [PATCH] feat!: add newtype struct Deck with associated methods Methods associated to Deck `get()` `init_deck()`(changed to Default) and others have been moved from Cards to newtype struct Deck. Functions definitions and returned values are also more coherent. --- examples/simulation.rs | 17 ++---- src/lib.rs | 115 +++++++++++++++++++++-------------------- 2 files changed, 63 insertions(+), 69 deletions(-) diff --git a/examples/simulation.rs b/examples/simulation.rs index f7f84e8..e3a4ad1 100644 --- a/examples/simulation.rs +++ b/examples/simulation.rs @@ -1,17 +1,13 @@ -use poker_eval::{report, Algorithm, Cards, Evaluator, Rules}; +use poker_eval::{report, Algorithm, Deck, Evaluator, Rules}; fn main() { let rules = Rules::Holdem; let algorithm = Algorithm::CactusKev; - let mut deck = Cards::init_deck(); - deck.shuffle_cards(); + let mut deck = Deck::new(); + deck.shuffle(); - let player_hands: Vec = (0..4) - .into_iter() - .map(|_| Cards::new().with_rules(&rules, &mut deck).clone()) - .collect(); - let mut table = Cards::new(); - table.with_cards(deck.get(5).unwrap()); + let player_hands = deck.get_with_rules_and_player_nb(&rules, 4); + let table = deck.get(5).unwrap(); let result = Evaluator::new() .with_rules(&rules) @@ -29,8 +25,5 @@ fn main() { Rules::Classic => (), } println!(); - // println!("Results: {:?}", result); let _winner = report(&result); - // let winner = result.iter().min_by(|a, b| a.1.cmp(&b.1)).unwrap(); - // println!("Winner: Player {} with score {}", winner.0, winner.1); } diff --git a/src/lib.rs b/src/lib.rs index d102a5a..270fe2c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -177,63 +177,6 @@ impl Evaluator { #[derive(Debug, Default, Clone)] pub struct Cards(Vec); -impl Cards { - pub fn new() -> Self { - Self(Vec::new()) - } - - pub fn with_cards<'a>(&'a mut self, cards: Cards) -> &'a mut Cards { - for card in cards.0 { - self.0.push(card.clone()); - } - self - } - - pub fn with_rules<'a>(&'a mut self, rules: &Rules, deck: &mut Cards) -> &'a mut Cards { - match rules { - Rules::Classic => { - self.with_cards(deck.get(5).unwrap()); - } - - Rules::Holdem => { - self.with_cards(deck.get(2).unwrap()); - } - }; - self - } - - pub fn get(&mut self, n: usize) -> Option { - if self.0.len() < n { - None - } else { - Some(Cards(self.0.split_off(self.0.len() - n))) - } - } - - pub fn init_deck() -> Cards { - let mut deck = Vec::new(); - let mut suit = 0x8000; - for _ in 0..4 { - for (j, prime) in PRIMES.iter().enumerate() { - let prime = *prime as u32; - deck.push( - Card::try_from(prime | ((j as u32) << 8) | suit | (1 << (16 + j))).unwrap(), - ); - } - suit >>= 1; - } - Cards(deck) - } - - #[cfg(feature = "random")] - pub fn shuffle_cards<'a>(&'a mut self) -> &'a mut Cards { - use rand::prelude::*; - let mut rng = thread_rng(); - self.0.shuffle(&mut rng); - self - } -} - impl FromStr for Cards { type Err = MyError; fn from_str(s: &str) -> Result { @@ -262,6 +205,64 @@ impl From for Vec { } } +#[derive(Debug, Clone)] +pub struct Deck(Cards); + +impl Deck { + pub fn new() -> Self { + Self::default() + } + + pub fn get_with_rules(&mut self, rules: &Rules) -> Cards { + let v = match rules { + Rules::Classic => self.get(5), + + Rules::Holdem => self.get(2), + }; + v.unwrap() + } + + pub fn get_with_rules_and_player_nb(&mut self, rules: &Rules, player_nb: usize) -> Vec { + (0..player_nb) + .into_iter() + .map(|_| self.get_with_rules(rules)) + .collect() + } + + pub fn get(&mut self, n: usize) -> Option { + if self.0 .0.len() < n { + None + } else { + Some(Cards(self.0 .0.split_off(self.0 .0.len() - n))) + } + } + + #[cfg(feature = "random")] + pub fn shuffle<'a>(&'a mut self) -> &'a mut Deck { + use rand::prelude::*; + let mut rng = thread_rng(); + self.0 .0.shuffle(&mut rng); + self + } +} + +impl Default for Deck { + fn default() -> Self { + let mut deck = Vec::with_capacity(52); + let mut suit = 0x8000; + for _ in 0..4 { + for (j, prime) in PRIMES.iter().enumerate() { + let prime = *prime as u32; + deck.push( + Card::try_from(prime | ((j as u32) << 8) | suit | (1 << (16 + j))).unwrap(), + ); + } + suit >>= 1; + } + Self(Cards(deck)) + } +} + /// ``` /// use poker_eval::{Card}; /// use std::str::FromStr; -- 2.20.1