Equation

Struct Equation 

Source
pub struct Equation { /* private fields */ }
Expand description

struct containing the Equation compiled for faster evaluation

§Example

use dice_forge::Equation;
let my_equation = Equation::new("3d5").unwrap();
let my_roll = my_equation.roll().unwrap();

Implementations§

Source§

impl Equation

Source

pub fn new(input: &str) -> Result<Equation, InvalidExpressionError>

Compiles and returns a new Equation object.

The input parameter should be a string representing a valid mathematical equation. The equation can include dice notation in the format “NdM” where N is the number of dice to roll, and M is the number of sides on each die. For example, “2d6” would roll two six-sided dice. The equation can also include standard mathematical operators such as addition (+), subtraction (-), multiplication (*), and division (/). Parentheses can be used to group sub-expressions together.

The function compiles the equation into a postfix format that is optimized for efficient evaluation. The resulting Equation object can then be used to roll the dice and perform basic math operations without the need for recompilation.

§Example

Creating a new Equation object and rolling the dice:

use dice_forge::Equation;

let my_equation = Equation::new("3d5+10/2^2").unwrap();
let result = my_equation.roll().unwrap();
println!("Result: {}", result);
Examples found in repository?
examples/damage.rs (line 11)
9    fn new(health: i32, d: &str) -> Self {
10        let damage =
11            dice_forge::Equation::new(d).expect("dont do this in your code handle the error");
12        Person { health, damage }
13    }
More examples
Hide additional examples
examples/is_average_average.rs (line 4)
3fn main() {
4    let my_die = Equation::new("d100").expect("handle the error in real code");
5    let mut results: u64 = 0;
6    let mut hit_low = false;
7    let mut hit_high = false;
8    let mut highest: u64 = 0;
9    let mut lowest: u64 = 100;
10    for _n in 0..10000 {
11        let rez = my_die.roll().unwrap();
12        if rez == 1 {
13            hit_low = true;
14        } else if rez == 100 {
15            hit_high = true;
16        }
17        if (rez as u64) < lowest {
18            lowest = rez as u64;
19        }
20        if (rez as u64) > highest {
21            highest = rez as u64;
22        }
23        results += my_die.roll().unwrap() as u64;
24    }
25    println!("average: {}", results / 10000);
26    println!("Hit High: {}, number: {}", hit_high, highest);
27    println!("Hit Low: {}, number: {}", hit_low, lowest);
28    println!("single roll {}", my_die.roll().unwrap());
29}
examples/doc_examples.rs (line 5)
4fn main() {
5    let my_equation = match Equation::new("3d5") {
6        Ok(value) => value,
7        Err(error) => {
8            println!("{}", error);
9            panic!()
10        }
11    };
12    let my_roll = my_equation.roll().unwrap();
13    println!("my_roll:{}", my_roll);
14
15    let doc_equation = match Equation::new("3d5+10/2^2") {
16        Ok(value) => value,
17        Err(error) => {
18            println!("{}", error);
19            panic!()
20        }
21    };
22    println!("You rolled {}", doc_equation.roll().unwrap());
23    println!("Average roll {}", doc_equation.average().unwrap());
24    let (low, high) = doc_equation.range().unwrap();
25    println!("range {} to {}", low, high);
26    println!("lowest possable number {}", doc_equation.low().unwrap());
27    println!("highest possable number {}", doc_equation.high().unwrap());
28    match roll::roll("test") {
29        Ok(v) => println!("{}", v),
30        Err(e) => println!("{}", e),
31    }
32}
Source

pub fn roll(&self) -> Result<i32, InvalidExpressionError>

Rolls the given Equation object.

The input parameter should be a string representing a valid mathematical equation that can include dice notation. Dice notation should be in the format “NdM” where N is the number of dice to roll, and M is the number of sides on each die. For example, “2d6” would roll two six-sided dice. Dice notation can also be combined with standard mathematical operators, such as addition (+), subtraction (-), multiplication (), division (/), and exponent (^). Parentheses can also be used to group sub-expressions together. For example, “10+(3+2d62)+3(2d20)+d2” is a valid equation that includes dice notation.

§Examples

Rolling 1d4:

use dice_forge::Equation;

let my_equation = Equation::new("1d4").unwrap();
let result = my_equation.roll().unwrap();

println!("Result: {}", result);

Rolling 2d6 a modifier:

use dice_forge::Equation;

let my_equation = Equation::new("2d6+4").unwrap();
let result = my_equation.roll().unwrap();

println!("Result: {}", result);

Rolling a more complex equation:

use dice_forge::Equation;
let my_equation = Equation::new("10+(3+2d6*2)+3(2d20)+d2").unwrap();
let result = my_equation.roll().unwrap();

println!("Result: {}", result);
Examples found in repository?
examples/damage.rs (line 15)
14    fn attack(&self) -> i32 {
15        self.damage.roll().unwrap()
16    }
More examples
Hide additional examples
examples/is_average_average.rs (line 11)
3fn main() {
4    let my_die = Equation::new("d100").expect("handle the error in real code");
5    let mut results: u64 = 0;
6    let mut hit_low = false;
7    let mut hit_high = false;
8    let mut highest: u64 = 0;
9    let mut lowest: u64 = 100;
10    for _n in 0..10000 {
11        let rez = my_die.roll().unwrap();
12        if rez == 1 {
13            hit_low = true;
14        } else if rez == 100 {
15            hit_high = true;
16        }
17        if (rez as u64) < lowest {
18            lowest = rez as u64;
19        }
20        if (rez as u64) > highest {
21            highest = rez as u64;
22        }
23        results += my_die.roll().unwrap() as u64;
24    }
25    println!("average: {}", results / 10000);
26    println!("Hit High: {}, number: {}", hit_high, highest);
27    println!("Hit Low: {}, number: {}", hit_low, lowest);
28    println!("single roll {}", my_die.roll().unwrap());
29}
examples/doc_examples.rs (line 12)
4fn main() {
5    let my_equation = match Equation::new("3d5") {
6        Ok(value) => value,
7        Err(error) => {
8            println!("{}", error);
9            panic!()
10        }
11    };
12    let my_roll = my_equation.roll().unwrap();
13    println!("my_roll:{}", my_roll);
14
15    let doc_equation = match Equation::new("3d5+10/2^2") {
16        Ok(value) => value,
17        Err(error) => {
18            println!("{}", error);
19            panic!()
20        }
21    };
22    println!("You rolled {}", doc_equation.roll().unwrap());
23    println!("Average roll {}", doc_equation.average().unwrap());
24    let (low, high) = doc_equation.range().unwrap();
25    println!("range {} to {}", low, high);
26    println!("lowest possable number {}", doc_equation.low().unwrap());
27    println!("highest possable number {}", doc_equation.high().unwrap());
28    match roll::roll("test") {
29        Ok(v) => println!("{}", v),
30        Err(e) => println!("{}", e),
31    }
32}
Source

pub fn average(&self) -> Result<i32, InvalidExpressionError>

calculates the product of the equation assuming the average roll of all die in the equation

§Example
use dice_forge::Equation;
println!("average roll {}", Equation::new("3d5+10/2^2").unwrap().average().unwrap());
Examples found in repository?
examples/doc_examples.rs (line 23)
4fn main() {
5    let my_equation = match Equation::new("3d5") {
6        Ok(value) => value,
7        Err(error) => {
8            println!("{}", error);
9            panic!()
10        }
11    };
12    let my_roll = my_equation.roll().unwrap();
13    println!("my_roll:{}", my_roll);
14
15    let doc_equation = match Equation::new("3d5+10/2^2") {
16        Ok(value) => value,
17        Err(error) => {
18            println!("{}", error);
19            panic!()
20        }
21    };
22    println!("You rolled {}", doc_equation.roll().unwrap());
23    println!("Average roll {}", doc_equation.average().unwrap());
24    let (low, high) = doc_equation.range().unwrap();
25    println!("range {} to {}", low, high);
26    println!("lowest possable number {}", doc_equation.low().unwrap());
27    println!("highest possable number {}", doc_equation.high().unwrap());
28    match roll::roll("test") {
29        Ok(v) => println!("{}", v),
30        Err(e) => println!("{}", e),
31    }
32}
Source

pub fn range(&self) -> Result<(i32, i32), InvalidExpressionError>

Calculates the range of possible values that can be produced by the equation.

The range is calculated by finding the product of the highest and lowest possible rolls for all dice in the equation. Note that this calculation will not take into account any additional mathematical operations in the equation, and may not accurately represent the true range of possible values.

§Example
use dice_forge::Equation;

let (low, high) = Equation::new("3d5+10/2^2").unwrap().range().unwrap();

println!("Range: {} - {}", low, high);
Examples found in repository?
examples/doc_examples.rs (line 24)
4fn main() {
5    let my_equation = match Equation::new("3d5") {
6        Ok(value) => value,
7        Err(error) => {
8            println!("{}", error);
9            panic!()
10        }
11    };
12    let my_roll = my_equation.roll().unwrap();
13    println!("my_roll:{}", my_roll);
14
15    let doc_equation = match Equation::new("3d5+10/2^2") {
16        Ok(value) => value,
17        Err(error) => {
18            println!("{}", error);
19            panic!()
20        }
21    };
22    println!("You rolled {}", doc_equation.roll().unwrap());
23    println!("Average roll {}", doc_equation.average().unwrap());
24    let (low, high) = doc_equation.range().unwrap();
25    println!("range {} to {}", low, high);
26    println!("lowest possable number {}", doc_equation.low().unwrap());
27    println!("highest possable number {}", doc_equation.high().unwrap());
28    match roll::roll("test") {
29        Ok(v) => println!("{}", v),
30        Err(e) => println!("{}", e),
31    }
32}
Source

pub fn low(&self) -> Result<i32, InvalidExpressionError>

Calculates the lowest possible value that can be produced by the equation.

The value is calculated by finding the product of the lowest possible rolls for all dice in the equation. Note that this calculation will not take into account any additional mathematical operations in the equation, and may not accurately represent the true lowest of possible values.

§Example
use dice_forge::Equation;

let low = Equation::new("3d5+10/2^2").unwrap().low().unwrap();

println!("Low: {}", low);
Examples found in repository?
examples/doc_examples.rs (line 26)
4fn main() {
5    let my_equation = match Equation::new("3d5") {
6        Ok(value) => value,
7        Err(error) => {
8            println!("{}", error);
9            panic!()
10        }
11    };
12    let my_roll = my_equation.roll().unwrap();
13    println!("my_roll:{}", my_roll);
14
15    let doc_equation = match Equation::new("3d5+10/2^2") {
16        Ok(value) => value,
17        Err(error) => {
18            println!("{}", error);
19            panic!()
20        }
21    };
22    println!("You rolled {}", doc_equation.roll().unwrap());
23    println!("Average roll {}", doc_equation.average().unwrap());
24    let (low, high) = doc_equation.range().unwrap();
25    println!("range {} to {}", low, high);
26    println!("lowest possable number {}", doc_equation.low().unwrap());
27    println!("highest possable number {}", doc_equation.high().unwrap());
28    match roll::roll("test") {
29        Ok(v) => println!("{}", v),
30        Err(e) => println!("{}", e),
31    }
32}
Source

pub fn high(&self) -> Result<i32, InvalidExpressionError>

Calculates the highest possible value that can be produced by the equation.

The value is calculated by finding the product of the highest possible rolls for all dice in the equation. Note that this calculation will not take into account any additional mathematical operations in the equation, and may not accurately represent the true highest of possible values.

§Example
use dice_forge::Equation;

let high = Equation::new("3d5+10/2^2").unwrap().high().unwrap();

println!("High: {}", high);
Examples found in repository?
examples/doc_examples.rs (line 27)
4fn main() {
5    let my_equation = match Equation::new("3d5") {
6        Ok(value) => value,
7        Err(error) => {
8            println!("{}", error);
9            panic!()
10        }
11    };
12    let my_roll = my_equation.roll().unwrap();
13    println!("my_roll:{}", my_roll);
14
15    let doc_equation = match Equation::new("3d5+10/2^2") {
16        Ok(value) => value,
17        Err(error) => {
18            println!("{}", error);
19            panic!()
20        }
21    };
22    println!("You rolled {}", doc_equation.roll().unwrap());
23    println!("Average roll {}", doc_equation.average().unwrap());
24    let (low, high) = doc_equation.range().unwrap();
25    println!("range {} to {}", low, high);
26    println!("lowest possable number {}", doc_equation.low().unwrap());
27    println!("highest possable number {}", doc_equation.high().unwrap());
28    match roll::roll("test") {
29        Ok(v) => println!("{}", v),
30        Err(e) => println!("{}", e),
31    }
32}
Source

pub fn advantage(&self) -> Result<i32, InvalidExpressionError>

Rolls the given Equation object with advantage.

The input parameter should be a string representing a valid mathematical equation that can include dice notation. Dice notation should be in the format “NdM” where N is the number of dice to roll, and M is the number of sides on each die. For example, “2d6” would roll two six-sided dice. Dice notation can also be combined with standard mathematical operators, such as addition (+), subtraction (-), multiplication (), division (/), and exponent (^). Parentheses can also be used to group sub-expressions together. For example, “10+(3+2d62)+3(2d20)+d2” is a valid equation that includes dice notation.

The function rolls the given equation twice and returns the greater of the two results. This emulates the “advantage” mechanic in some games, where a player can roll two dice and take the greater result.

§Examples

Rolling 1d4:

use dice_forge::Equation;

let my_equation = Equation::new("1d4").unwrap();
let result = my_equation.advantage().unwrap();

println!("Result: {}", result);

Rolling 2d6 a modifier:

use dice_forge::Equation;

let my_equation = Equation::new("2d6+4").unwrap();
let result = my_equation.advantage().unwrap();

println!("Result: {}", result);

Rolling a more complex equation:

use dice_forge::Equation;
let my_equation = Equation::new("10+(3+2d6*2)+3(2d20)+d2").unwrap();
let result = my_equation.advantage().unwrap();

println!("Result: {}", result);
Source

pub fn disadvantage(&self) -> Result<i32, InvalidExpressionError>

Rolls the given Equation object with disadvantage.

The input parameter should be a string representing a valid mathematical equation that can include dice notation. Dice notation should be in the format “NdM” where N is the number of dice to roll, and M is the number of sides on each die. For example, “2d6” would roll two six-sided dice. Dice notation can also be combined with standard mathematical operators, such as addition (+), subtraction (-), multiplication (), division (/), and exponent (^). Parentheses can also be used to group sub-expressions together. For example, “10+(3+2d62)+3(2d20)+d2” is a valid equation that includes dice notation.

The function rolls the given equation twice and returns the lesser of the two results. This emulates the “disadvantage” mechanic in some games, where a player can roll two dice and take the lesser result.

§Examples

Rolling 1d4:

use dice_forge::Equation;

let my_equation = Equation::new("1d4").unwrap();
let result = my_equation.disadvantage().unwrap();

println!("Result: {}", result);

Rolling 2d6 a modifier:

use dice_forge::Equation;

let my_equation = Equation::new("2d6+4").unwrap();
let result = my_equation.disadvantage().unwrap();

println!("Result: {}", result);

Rolling a more complex equation:

use dice_forge::Equation;
let my_equation = Equation::new("10+(3+2d6*2)+3(2d20)+d2").unwrap();
let result = my_equation.disadvantage().unwrap();

println!("Result: {}", result);
Source

pub fn emphasis(&self) -> Result<i32, InvalidExpressionError>

Calculates the highest possible value that can be produced by the equation.

The value is calculated by finding the product of the highest possible rolls for all dice in the equation. Note that this calculation will not take into account any additional mathematical operations in the equation, and may not accurately represent the true highest of possible values.

§Example
use dice_forge::Equation;

let high = Equation::new("3d5+10/2^2").unwrap().high().unwrap();

println!("High: {}", high);

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V