Skip to main content

ai_backgammon/
dice.rs

1use std::fmt;
2
3#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
4/// An unordered roll stored with the larger die first.
5pub struct Dice {
6    high: u8,
7    low: u8,
8}
9
10impl Dice {
11    pub fn new(first: u8, second: u8) -> Result<Self, DiceError> {
12        if !(1..=6).contains(&first) || !(1..=6).contains(&second) {
13            return Err(DiceError);
14        }
15        Ok(Self {
16            high: first.max(second),
17            low: first.min(second),
18        })
19    }
20
21    pub const fn high(self) -> u8 {
22        self.high
23    }
24
25    pub const fn low(self) -> u8 {
26        self.low
27    }
28
29    pub const fn is_double(self) -> bool {
30        self.high == self.low
31    }
32
33    pub fn moves(self) -> Vec<u8> {
34        if self.is_double() {
35            vec![self.high; 4]
36        } else {
37            vec![self.high, self.low]
38        }
39    }
40}
41
42impl fmt::Display for Dice {
43    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(formatter, "{}{}", self.high, self.low)
45    }
46}
47
48#[derive(Clone, Copy, Debug, Eq, PartialEq)]
49pub struct DiceError;
50
51impl fmt::Display for DiceError {
52    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
53        formatter.write_str("dice values must be between one and six")
54    }
55}
56
57impl std::error::Error for DiceError {}
58
59#[derive(Clone, Copy, Debug, Eq, PartialEq)]
60pub struct DiceOutcome {
61    pub dice: Dice,
62    pub weight: u8,
63}
64
65const fn dice(high: u8, low: u8) -> Dice {
66    Dice { high, low }
67}
68
69/// The 21 unordered rolls and their multiplicities among 36 equally likely outcomes.
70pub const DICE_OUTCOMES: [DiceOutcome; 21] = [
71    DiceOutcome {
72        dice: dice(1, 1),
73        weight: 1,
74    },
75    DiceOutcome {
76        dice: dice(2, 1),
77        weight: 2,
78    },
79    DiceOutcome {
80        dice: dice(2, 2),
81        weight: 1,
82    },
83    DiceOutcome {
84        dice: dice(3, 1),
85        weight: 2,
86    },
87    DiceOutcome {
88        dice: dice(3, 2),
89        weight: 2,
90    },
91    DiceOutcome {
92        dice: dice(3, 3),
93        weight: 1,
94    },
95    DiceOutcome {
96        dice: dice(4, 1),
97        weight: 2,
98    },
99    DiceOutcome {
100        dice: dice(4, 2),
101        weight: 2,
102    },
103    DiceOutcome {
104        dice: dice(4, 3),
105        weight: 2,
106    },
107    DiceOutcome {
108        dice: dice(4, 4),
109        weight: 1,
110    },
111    DiceOutcome {
112        dice: dice(5, 1),
113        weight: 2,
114    },
115    DiceOutcome {
116        dice: dice(5, 2),
117        weight: 2,
118    },
119    DiceOutcome {
120        dice: dice(5, 3),
121        weight: 2,
122    },
123    DiceOutcome {
124        dice: dice(5, 4),
125        weight: 2,
126    },
127    DiceOutcome {
128        dice: dice(5, 5),
129        weight: 1,
130    },
131    DiceOutcome {
132        dice: dice(6, 1),
133        weight: 2,
134    },
135    DiceOutcome {
136        dice: dice(6, 2),
137        weight: 2,
138    },
139    DiceOutcome {
140        dice: dice(6, 3),
141        weight: 2,
142    },
143    DiceOutcome {
144        dice: dice(6, 4),
145        weight: 2,
146    },
147    DiceOutcome {
148        dice: dice(6, 5),
149        weight: 2,
150    },
151    DiceOutcome {
152        dice: dice(6, 6),
153        weight: 1,
154    },
155];