add ability to speed up, slow down
This commit is contained in:
parent
59bd2f3cf2
commit
816cc2c8c3
1 changed files with 115 additions and 83 deletions
55
src/rules.rs
55
src/rules.rs
|
|
@ -1,37 +1,41 @@
|
|||
use rand::{Rng, thread_rng};
|
||||
use rand::rngs::ThreadRng;
|
||||
use rand::{thread_rng, Rng};
|
||||
|
||||
use crate::print::println_info;
|
||||
|
||||
const MAX_BRIGHTNESS: u8 = 12;
|
||||
|
||||
pub struct Rules<TState, TKernel, const KERNEL_SIZE: usize>
|
||||
where TState: Copy + PartialEq, TKernel: Copy
|
||||
where
|
||||
TState: Copy + PartialEq,
|
||||
TKernel: Copy,
|
||||
{
|
||||
pub kernel: [[TKernel; KERNEL_SIZE]; KERNEL_SIZE],
|
||||
pub count_neighbor: Box<dyn Fn(TState, TKernel) -> i32>,
|
||||
pub next_state: Box<dyn Fn(TState, i32) -> TState>,
|
||||
}
|
||||
|
||||
pub const MOORE_NEIGHBORHOOD: [[bool; 3]; 3] = [
|
||||
[true, true, true],
|
||||
[true, false, true],
|
||||
[true, true, true]
|
||||
];
|
||||
pub const MOORE_NEIGHBORHOOD: [[bool; 3]; 3] =
|
||||
[[true, true, true], [true, false, true], [true, true, true]];
|
||||
|
||||
pub const NEUMANN_NEIGHBORHOOD: [[bool; 3]; 3] = [
|
||||
[false, true, false],
|
||||
[true, false, true],
|
||||
[false, true, false]
|
||||
[false, true, false],
|
||||
];
|
||||
|
||||
pub const DIAGONALS_NEIGHBORHOOD: [[bool; 3]; 3] = [
|
||||
[true, false, true],
|
||||
[false, false, false],
|
||||
[true, false, true]
|
||||
[true, false, true],
|
||||
];
|
||||
|
||||
pub fn count_true_neighbor(neighbor_state: bool, kernel_value: bool) -> i32
|
||||
{
|
||||
if neighbor_state && kernel_value { 1 } else { 0 }
|
||||
pub fn count_true_neighbor(neighbor_state: bool, kernel_value: bool) -> i32 {
|
||||
if neighbor_state && kernel_value {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
|
@ -39,26 +43,31 @@ pub fn generate_bb3() -> Rules<bool, bool, 3> {
|
|||
let mut rng = thread_rng();
|
||||
|
||||
let is_moore = rng.gen_bool(1.0 / 2.0);
|
||||
let kernel = if is_moore { MOORE_NEIGHBORHOOD } else { NEUMANN_NEIGHBORHOOD };
|
||||
let kernel = if is_moore {
|
||||
MOORE_NEIGHBORHOOD
|
||||
} else {
|
||||
NEUMANN_NEIGHBORHOOD
|
||||
};
|
||||
let max_neighbors = if is_moore { 8 } else { 4 };
|
||||
|
||||
let birth = generate_neighbor_counts(rng.gen_range(1..=max_neighbors), &mut rng, &[0]);
|
||||
let survive = generate_neighbor_counts(rng.gen_range(1..=max_neighbors), &mut rng, &[]);
|
||||
|
||||
println_info(format!("generated bb3: Birth {birth:?} Survival {survive:?}, kernel: {kernel:?}"));
|
||||
println_info(format!(
|
||||
"generated bb3: Birth {birth:?} Survival {survive:?}, kernel: {kernel:?}"
|
||||
));
|
||||
|
||||
Rules {
|
||||
kernel,
|
||||
count_neighbor: Box::new(count_true_neighbor),
|
||||
next_state: Box::new(move |old_state, neighbors| {
|
||||
old_state && survive.contains(&neighbors)
|
||||
|| !old_state && birth.contains(&neighbors)
|
||||
old_state && survive.contains(&neighbors) || !old_state && birth.contains(&neighbors)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_neighbor_counts(count: u8, rng: &mut ThreadRng, exclude: &[i32]) -> Vec<i32> {
|
||||
let mut result = vec!();
|
||||
let mut result = vec![];
|
||||
for _ in 0..count {
|
||||
let value = rng.gen_range(0..=count) as i32;
|
||||
if !exclude.contains(&value) {
|
||||
|
|
@ -76,13 +85,17 @@ pub fn generate_u8b3() -> Rules<u8, bool, 3> {
|
|||
0 => MOORE_NEIGHBORHOOD,
|
||||
1 => NEUMANN_NEIGHBORHOOD,
|
||||
2 => DIAGONALS_NEIGHBORHOOD,
|
||||
_ => panic!()
|
||||
_ => panic!(),
|
||||
};
|
||||
|
||||
let alive_threshold = rng.gen();
|
||||
|
||||
let birth = generate_neighbor_counts(rng.gen_range(1..=9), &mut rng, &[0]);
|
||||
let survive = generate_neighbor_counts(rng.gen_range(1..=9 - birth.len()) as u8, &mut rng, &[]);
|
||||
let survive = generate_neighbor_counts(
|
||||
rng.gen_range(1..=u8::max(1, 9 - birth.len() as u8)),
|
||||
&mut rng,
|
||||
&[],
|
||||
);
|
||||
|
||||
let add = rng.gen_range(5..40);
|
||||
let sub = rng.gen_range(5..40);
|
||||
|
|
@ -91,9 +104,7 @@ pub fn generate_u8b3() -> Rules<u8, bool, 3> {
|
|||
|
||||
Rules {
|
||||
kernel,
|
||||
count_neighbor: Box::new(|state, kernel| {
|
||||
if kernel { state as i32 } else { 0 }
|
||||
}),
|
||||
count_neighbor: Box::new(|state, kernel| if kernel { state as i32 } else { 0 }),
|
||||
next_state: Box::new(move |old_state, neighbors| {
|
||||
let neighbors = neighbors / alive_threshold as i32;
|
||||
let old_is_alive = old_state >= alive_threshold;
|
||||
|
|
|
|||
Loading…
Reference in a new issue