This commit is contained in:
Henri Bourcereau 2024-03-11 20:45:36 +01:00
parent 44c040b414
commit a886526fcf
5 changed files with 205 additions and 97 deletions

View file

@ -1,34 +1,57 @@
use crate::Error;
use rand::distributions::{Distribution, Uniform};
use rand::{rngs::StdRng, SeedableRng};
use serde::{Deserialize, Serialize};
/// Represents the two dices
#[derive(Debug)]
pub struct DiceRoller {
rng: StdRng,
}
impl Default for DiceRoller {
fn default() -> Self {
Self::new(None)
}
}
impl DiceRoller {
pub fn new(opt_seed: Option<u64>) -> Self {
Self {
rng: match opt_seed {
None => StdRng::from_rng(rand::thread_rng()).unwrap(),
Some(seed) => SeedableRng::seed_from_u64(seed),
},
}
}
/// Roll the dices which generates two random numbers between 1 and 6, replicating a perfect
/// dice. We use the operating system's random number generator.
pub fn roll(&mut self) -> Dice {
let between = Uniform::new_inclusive(1, 6);
let v = (between.sample(&mut self.rng), between.sample(&mut self.rng));
Dice { values: (v.0, v.1) }
}
// Heads or tails
// pub fn coin(self) -> bool {
// let between = Uniform::new_inclusive(1, 2);
// let mut rng = rand::thread_rng();
// between.sample(&mut rng) == 1
// }
}
/// Represents the two dice
///
/// Trictrac is always played with two dices.
/// Trictrac is always played with two dice.
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Deserialize, Default)]
pub struct Dices {
pub struct Dice {
/// The two dice values
pub values: (u8, u8),
}
impl Dices {
/// Roll the dices which generates two random numbers between 1 and 6, replicating a perfect
/// dice. We use the operating system's random number generator.
pub fn roll(self) -> Self {
let between = Uniform::new_inclusive(1, 6);
let mut rng = rand::thread_rng();
let v = (between.sample(&mut rng), between.sample(&mut rng));
Dices { values: (v.0, v.1) }
}
/// Heads or tails
pub fn coin(self) -> bool {
let between = Uniform::new_inclusive(1, 2);
let mut rng = rand::thread_rng();
between.sample(&mut rng) == 1
}
impl Dice {
pub fn to_bits_string(self) -> String {
format!("{:0>3b}{:0>3b}", self.values.0, self.values.1)
}
@ -61,14 +84,21 @@ mod tests {
#[test]
fn test_roll() {
let dices = Dices::default().roll();
assert!(dices.values.0 >= 1 && dices.values.0 <= 6);
assert!(dices.values.1 >= 1 && dices.values.1 <= 6);
let dice = DiceRoller::default().roll();
assert!(dice.values.0 >= 1 && dice.values.0 <= 6);
assert!(dice.values.1 >= 1 && dice.values.1 <= 6);
}
#[test]
fn test_seed() {
let dice = DiceRoller::new(Some(123)).roll();
assert!(dice.values.0 == 3);
assert!(dice.values.1 == 2);
}
#[test]
fn test_to_bits_string() {
let dices = Dices { values: (4, 2) };
assert!(dices.to_bits_string() == "100010");
let dice = Dice { values: (4, 2) };
assert!(dice.to_bits_string() == "100010");
}
}