seedable rng

This commit is contained in:
Daniel Olsen 2021-01-04 12:24:37 +01:00
parent 706e33a12f
commit eb9998d639
2 changed files with 17 additions and 6 deletions

View File

@ -226,8 +226,9 @@ impl Default for Board {
} }
} }
#[derive(Default, Debug, Clone)] #[derive(Debug, Clone)]
pub struct Game { pub struct Game {
random: StdRng,
turn: u32, turn: u32,
player: usize, player: usize,
box_top: Bag, box_top: Bag,
@ -237,7 +238,7 @@ pub struct Game {
boards: Vec<Board> boards: Vec<Board>
} }
impl Game { impl Game {
pub fn new(players: usize) -> Result<Self, &'static str> { pub fn new(players: usize, random: StdRng) -> Result<Self, &'static str> {
coz::scope!("create game"); coz::scope!("create game");
let n_factories = match players { let n_factories = match players {
2 => 5, 2 => 5,
@ -256,6 +257,7 @@ impl Game {
} }
let game = Game { let game = Game {
random: random,
turn: 0, turn: 0,
player: 0, player: 0,
box_top: Vec::<Tile>::with_capacity(100).into(), box_top: Vec::<Tile>::with_capacity(100).into(),
@ -283,7 +285,7 @@ impl Game {
return Ok(()) return Ok(())
} }
else { else {
let tile_i = (random::<f32>() * 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); let tile = self.bag.remove(tile_i);
factory.push(tile); factory.push(tile);
} }

View File

@ -1,16 +1,25 @@
mod azul; mod azul;
use azul::*; use azul::*;
use rand::prelude::*;
fn main() -> Result<(), &'static str> { fn main() -> Result<(), &'static str> {
let mut g_rng = StdRng::seed_from_u64(42);
for _ in 0..10000 { 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(()) 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()?; game.fill()?;
let mut all_err = false; let mut all_err = false;