From e17d4cd99715238ebe15dbcf7d49373797c7372a Mon Sep 17 00:00:00 2001 From: MA Beaudet Date: Sat, 6 Nov 2021 13:16:05 +0100 Subject: [PATCH] feat: add simulation example --- Cargo.toml | 3 +++ examples/simulation.rs | 25 +++++++++++++++++++++++++ src/lib.rs | 8 ++++++++ 3 files changed, 36 insertions(+) create mode 100644 examples/simulation.rs diff --git a/Cargo.toml b/Cargo.toml index 3275a2f..c77cd10 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,10 @@ crate-type = ["cdylib", "rlib"] [features] parallel = ["rayon"] +random = ["rand"] +default = ["random"] [dependencies] +rand = { version = "0.8.4", optional = true } rayon = { version = "1.5.1", optional = true } wasm-bindgen = "0.2.78" diff --git a/examples/simulation.rs b/examples/simulation.rs new file mode 100644 index 0000000..6d2fb0a --- /dev/null +++ b/examples/simulation.rs @@ -0,0 +1,25 @@ +use poker_eval::{init_deck, shuffle_deck, Evaluator, Hand, Rules}; + +fn main() { + let rules = Rules::Classic; + let deck = init_deck(); + let mut deck = shuffle_deck(deck); + let mut table = Hand::new(); + + let player_hands: Vec = (0..4) + .into_iter() + .map(|_| Hand::new().with_rules(&rules, &mut deck).clone()) + .collect(); + table.cards = deck.split_off(deck.len() - 5); + + let result = Evaluator::new() + .with_rules(&rules) + .with_hands(&player_hands) + .with_table(&table) + .eval(); + + println!("Table: {:?}", table.cards); + println!("Results: {:?}", result); + let winner = result.iter().min_by(|a, b| a.1.cmp(&b.1)).unwrap(); + println!("Winner: {:?}", winner); +} diff --git a/src/lib.rs b/src/lib.rs index 93b7342..5b5017d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -253,6 +253,14 @@ pub fn init_deck() -> Vec { deck } +#[cfg(feature = "random")] +pub fn shuffle_deck(mut deck: Vec) -> Vec { + use rand::prelude::*; + let mut rng = thread_rng(); + deck.shuffle(&mut rng); + deck +} + /// Returns an option of integer based on the rank and the suit of a card. /// /// An integer is made up of four bytes. The high-order bytes are used to -- 2.20.1