feat: add exploding dice
This commit is contained in:
@@ -11,11 +11,25 @@ fn roll_range(range: RangeInclusive<i32>) -> Vec<i32> {
|
|||||||
vec![rng.random_range(range)]
|
vec![rng.random_range(range)]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn roll_exploding(sides: u16, threshold: i32) -> Vec<i32> {
|
||||||
|
let mut rng = rand::rng();
|
||||||
|
let mut results = Vec::<i32>::new();
|
||||||
|
loop {
|
||||||
|
let result: i32 = rng.random_range(1..=sides as i32);
|
||||||
|
results.push(result);
|
||||||
|
if result < threshold {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
results
|
||||||
|
}
|
||||||
|
|
||||||
impl Die {
|
impl Die {
|
||||||
pub fn roll(&self) -> Vec<i32> {
|
pub fn roll(&self) -> Vec<i32> {
|
||||||
match self {
|
match self {
|
||||||
Die::Sides(sides) => roll_sides(*sides),
|
Die::Exploding(sides, threshold) => roll_exploding(*sides, *threshold),
|
||||||
Die::Range(range) => roll_range(range.clone()),
|
Die::Range(range) => roll_range(range.clone()),
|
||||||
|
Die::Sides(sides) => roll_sides(*sides),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,4 +16,11 @@ impl Handful {
|
|||||||
}
|
}
|
||||||
Self { dice }
|
Self { dice }
|
||||||
}
|
}
|
||||||
|
pub fn grab_exploding(dice_num: u16, sides: u16, threshold: i32) -> Self {
|
||||||
|
let mut dice = Vec::<Die>::new();
|
||||||
|
for _ in 1..=dice_num {
|
||||||
|
dice.push(Die::Exploding(sides, threshold));
|
||||||
|
}
|
||||||
|
Self { dice }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,9 @@ use std::ops::*;
|
|||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
|
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
|
||||||
pub enum E {
|
pub enum E {
|
||||||
Sides(u16),
|
Exploding(u16, i32),
|
||||||
Range(RangeInclusive<i32>),
|
Range(RangeInclusive<i32>),
|
||||||
|
Sides(u16),
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl Display for E {
|
// impl Display for E {
|
||||||
|
|||||||
Reference in New Issue
Block a user