feat: add simulation example
authorMA Beaudet <ma@beaudet.xyz>
Sat, 6 Nov 2021 12:16:05 +0000 (13:16 +0100)
committerMA Beaudet <ma@beaudet.xyz>
Sat, 6 Nov 2021 12:16:05 +0000 (13:16 +0100)
Cargo.toml
examples/simulation.rs [new file with mode: 0644]
src/lib.rs

index 3275a2f..c77cd10 100644 (file)
@@ -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 (file)
index 0000000..6d2fb0a
--- /dev/null
@@ -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<Hand> = (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);
+}
index 93b7342..5b5017d 100644 (file)
@@ -253,6 +253,14 @@ pub fn init_deck() -> Vec<u32> {
     deck
 }
 
+#[cfg(feature = "random")]
+pub fn shuffle_deck(mut deck: Vec<u32>) -> Vec<u32> {
+    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