damage/
damage.rs

1//use dicey::Equation;
2
3struct Person {
4    health: i32,
5    damage: dice_forge::Equation,
6}
7
8impl Person {
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    }
14    fn attack(&self) -> i32 {
15        self.damage.roll().unwrap()
16    }
17    // fn damage(mut self, d: i32) {
18    //     self.health -= d as i32;
19    // }
20}
21
22fn main() {
23    let person1 = Person::new(100, "2d20");
24    let person2 = Person::new(100, "3d10");
25    let mut turns = 0;
26    let mut health = person2.health;
27    while health > 0 {
28        turns += 1;
29        health -= person1.attack() as i32;
30    }
31    println!("It took {} turns for person 1 to kill person 2", turns);
32    health = person1.health;
33    while health > 0 {
34        turns += 1;
35        health -= person2.attack() as i32;
36    }
37    println!("It took {} turns for person 2 to kill person 1", turns);
38}