diff --git a/src/azul.rs b/src/azul.rs index ae29020..aa6de57 100644 --- a/src/azul.rs +++ b/src/azul.rs @@ -226,8 +226,9 @@ impl Default for Board { } } -#[derive(Default, Debug, Clone)] +#[derive(Debug, Clone)] pub struct Game { + random: StdRng, turn: u32, player: usize, box_top: Bag, @@ -237,7 +238,7 @@ pub struct Game { boards: Vec } impl Game { - pub fn new(players: usize) -> Result { + pub fn new(players: usize, random: StdRng) -> Result { coz::scope!("create game"); let n_factories = match players { 2 => 5, @@ -256,6 +257,7 @@ impl Game { } let game = Game { + random: random, turn: 0, player: 0, box_top: Vec::::with_capacity(100).into(), @@ -283,7 +285,7 @@ impl Game { return Ok(()) } else { - let tile_i = (random::() * self.bag.len() as f32).floor() as usize; + let tile_i:usize = self.random.gen_range(0..self.bag.len()); let tile = self.bag.remove(tile_i); factory.push(tile); } diff --git a/src/main.rs b/src/main.rs index f38e559..18bc4a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,25 @@ mod azul; use azul::*; +use rand::prelude::*; fn main() -> Result<(), &'static str> { + let mut g_rng = StdRng::seed_from_u64(42); for _ in 0..10000 { - run()?; + let rng = match StdRng::from_rng(&mut g_rng) { + Ok(r) => r, + Err(e) => { + println!("error! {:?}", e); + break; + } + }; + run(rng)?; } Ok(()) } -fn run() -> Result<(), &'static str> { +fn run(rng: StdRng) -> Result<(), &'static str> { - let mut game = Game::new(2)?; + let mut game = Game::new(2, rng)?; game.fill()?; let mut all_err = false;