From 07437890c9d4b97d2a43812de2d7d961c207a41d Mon Sep 17 00:00:00 2001 From: Mohammed Amar-Bensaber Date: Sat, 7 Dec 2024 18:02:13 +0100 Subject: [PATCH] feat(grid): implement cell random generation Prefer filled over empty cells when generating them randomly because it makes the game more fun. --- src/grid.rs | 2 ++ src/grid/rand.rs | 14 ++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 src/grid/rand.rs diff --git a/src/grid.rs b/src/grid.rs index 03e6c92..4954960 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -1,3 +1,5 @@ +mod rand; + #[derive(Clone, Default, Debug, PartialEq)] pub enum Cell { /// Filled. diff --git a/src/grid/rand.rs b/src/grid/rand.rs new file mode 100644 index 0000000..23bd419 --- /dev/null +++ b/src/grid/rand.rs @@ -0,0 +1,14 @@ +use crate::grid::Cell; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; + +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> Cell { + let filled = rng.gen_bool(2. / 3.); + if filled { + Cell::Filled + } else { + Cell::Empty + } + } +}