24 lines
674 B
Rust
24 lines
674 B
Rust
use crate::node::Node;
|
|
use rand::random_range;
|
|
|
|
const PICK_STOP_PROBABILITY: u8 = 2;
|
|
|
|
pub struct NodeModifier<'a> {
|
|
picked_node: &'a mut Node,
|
|
}
|
|
|
|
impl<'a> NodeModifier<'a> {
|
|
pub fn from_random(root: &'a mut Node) -> NodeModifier<'a> {
|
|
let mut selected = root;
|
|
while random_range(0..PICK_STOP_PROBABILITY) == PICK_STOP_PROBABILITY - 1 {
|
|
let len = selected.children.len();
|
|
selected = &mut selected.children[random_range(0..len)];
|
|
}
|
|
NodeModifier {
|
|
picked_node: selected,
|
|
}
|
|
}
|
|
pub fn from_node(node: &'a mut Node) -> NodeModifier<'a> {
|
|
NodeModifier { picked_node: node }
|
|
}
|
|
}
|