1use crate::Error;
29use std::fmt;
30
31pub use board::{Board, BoardDisplay, Move, Position};
32pub use cube::Cube;
33pub use dice::{Dice, Roll};
34pub use player::Player;
35
36mod board;
38mod cube;
40mod dice;
42mod player;
44
45pub mod prelude {
48 pub use crate::rules::{Board, BoardDisplay, Dice, Move, Player, Position, Roll};
49}
50
51#[derive(Debug, Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)]
53pub struct Rules {
54 points: u64,
56 cube_play: bool,
58 beaver: bool,
60 raccoon: bool,
63 murphy: bool,
66 murphy_limit: u8,
69 jacobi: bool,
72 crawford: bool,
75 holland: bool,
78}
79
80impl Default for Rules {
81 fn default() -> Self {
90 Rules {
91 points: 7,
92 cube_play: true,
93 beaver: false,
94 raccoon: false,
95 murphy: false,
96 murphy_limit: 0,
97 jacobi: false,
98 crawford: true,
99 holland: false,
100 }
101 }
102}
103
104impl fmt::Display for Rules {
106 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107 write!(
108 f,
109 "Points: {}, Cube: {}, Beaver: {}, Raccoon: {}, Murphy: {}, Murphy Limit: {}, Jacobi: {}, \
110 Crawford: {}, Holland: {}",
111 self.points,
112 self.cube_play,
113 self.beaver,
114 self.raccoon,
115 self.murphy,
116 self.murphy_limit,
117 self.jacobi,
118 self.crawford,
119 self.holland
120 )
121 }
122}
123
124pub trait MatchRules {
126 fn set_points(&mut self, points: u64) -> Result<(), Error>;
128
129 fn get_points(&self) -> Result<u64, Error>;
131
132 fn set_cube_play(&mut self, cube: bool) -> Result<(), Error>;
134
135 fn get_cube_play(&self) -> Result<bool, Error>;
137
138 fn set_crawford(&mut self, state: bool) -> Result<(), Error>;
141
142 fn get_crawford(&self) -> Result<bool, Error>;
144
145 fn set_beaver(&mut self, state: bool) -> Result<(), Error>;
147
148 fn get_beaver(&self) -> Result<bool, Error>;
150
151 fn set_raccoon(&mut self, state: bool) -> Result<(), Error>;
154
155 fn get_raccoon(&self) -> Result<bool, Error>;
157
158 fn set_murphy(&mut self, state: bool, limit: u8) -> Result<(), Error>;
162
163 fn get_murphy(&self) -> Result<(bool, u8), Error>;
166
167 fn set_jacobi(&mut self, state: bool) -> Result<(), Error>;
170
171 fn get_jacobi(&self) -> Result<bool, Error>;
173
174 fn set_holland(&mut self, state: bool) -> Result<(), Error>;
177
178 fn get_holland(&self) -> Result<bool, Error>;
180}
181
182impl MatchRules for Rules {
183 fn set_points(&mut self, points: u64) -> Result<(), Error> {
184 if points < 1 {
185 return Err(Error::PointsInvalid);
186 }
187 self.points = points;
188 Ok(())
189 }
190
191 fn get_points(&self) -> Result<u64, Error> {
193 Ok(self.points)
194 }
195
196 fn set_cube_play(&mut self, cube: bool) -> Result<(), Error> {
197 self.cube_play = cube;
198 Ok(())
199 }
200
201 fn get_cube_play(&self) -> Result<bool, Error> {
202 Ok(self.cube_play)
203 }
204
205 fn set_crawford(&mut self, state: bool) -> Result<(), Error> {
206 self.crawford = state;
207 Ok(())
208 }
209
210 fn get_crawford(&self) -> Result<bool, Error> {
212 Ok(self.crawford)
213 }
214
215 fn set_beaver(&mut self, state: bool) -> Result<(), Error> {
216 self.beaver = state;
217 Ok(())
218 }
219
220 fn get_beaver(&self) -> Result<bool, Error> {
222 Ok(self.beaver)
223 }
224
225 fn set_raccoon(&mut self, state: bool) -> Result<(), Error> {
226 self.raccoon = state;
227 Ok(())
228 }
229
230 fn get_raccoon(&self) -> Result<bool, Error> {
232 Ok(self.raccoon)
233 }
234
235 fn set_murphy(&mut self, state: bool, limit: u8) -> Result<(), Error> {
236 self.murphy = state;
237 self.murphy_limit = limit;
238 Ok(())
239 }
240
241 fn get_murphy(&self) -> Result<(bool, u8), Error> {
243 if !self.murphy {
244 return Ok((false, 0));
245 }
246 Ok((true, self.murphy_limit))
247 }
248
249 fn set_jacobi(&mut self, state: bool) -> Result<(), Error> {
250 self.jacobi = state;
251 Ok(())
252 }
253
254 fn get_jacobi(&self) -> Result<bool, Error> {
256 Ok(self.jacobi)
257 }
258
259 fn set_holland(&mut self, state: bool) -> Result<(), Error> {
260 self.crawford = state;
261 self.holland = state;
262 Ok(())
263 }
264
265 fn get_holland(&self) -> Result<bool, Error> {
267 Ok(self.holland)
268 }
269}
270
271#[cfg(test)]
273mod tests {
274 use super::*;
275
276 #[test]
277 fn test_default_rules() {
278 let rules = Rules::default();
279 assert_eq!(rules.points, 7);
280 assert!(!rules.beaver);
281 assert!(!rules.raccoon);
282 assert!(!rules.murphy);
283 assert_eq!(rules.murphy_limit, 0);
284 assert!(!rules.jacobi);
285 assert!(rules.crawford);
286 assert!(!rules.holland);
287 }
288
289 #[test]
290 fn test_display() {
291 let rules = Rules::default();
292 assert_eq!(
293 format!("{}", rules),
294 "Points: 7, Cube: true, Beaver: false, Raccoon: false, Murphy: false, Murphy Limit: \
295 0, Jacobi: false, Crawford: true, Holland: false"
296 );
297 }
298
299 #[test]
300 fn test_set_points_valid() -> Result<(), Error> {
301 let mut rules = Rules::default();
302 rules.set_points(10)?;
303
304 assert_eq!(rules.get_points(), Ok(10));
305 Ok(())
306 }
307
308 #[test]
309 fn test_set_points_invalid() {
310 let mut rules = Rules::default();
311 let result = rules.set_points(0);
312 assert!(result.is_err());
313 assert_eq!(result.unwrap_err(), Error::PointsInvalid);
314 }
315
316 #[test]
317 fn test_set_crawford() -> Result<(), Error> {
318 let mut rules = Rules::default();
319 rules.set_crawford(true)?;
320
321 assert_eq!(rules.get_crawford(), Ok(true));
322 Ok(())
323 }
324
325 #[test]
326 fn test_set_beaver() -> Result<(), Error> {
327 let mut rules = Rules::default();
328 rules.set_beaver(true)?;
329
330 assert_eq!(rules.get_beaver(), Ok(true));
331 Ok(())
332 }
333
334 #[test]
335 fn test_set_raccoon() -> Result<(), Error> {
336 let mut rules = Rules::default();
337 rules.set_raccoon(true)?;
338
339 assert_eq!(rules.get_raccoon(), Ok(true));
340 Ok(())
341 }
342
343 #[test]
344 fn test_set_murphy() -> Result<(), Error> {
345 let mut rules = Rules::default();
346 rules.set_murphy(true, 8)?;
347
348 assert_eq!(rules.get_murphy(), Ok((true, 8)));
349 Ok(())
350 }
351
352 #[test]
353 fn test_set_murphy_false() -> Result<(), Error> {
354 let rules = Rules::default();
355
356 assert_eq!(rules.get_murphy(), Ok((false, 0)));
357 Ok(())
358 }
359
360 #[test]
361 fn test_set_jacobi() -> Result<(), Error> {
362 let mut rules = Rules::default();
363 rules.set_jacobi(true)?;
364
365 assert_eq!(rules.get_jacobi(), Ok(true));
366 Ok(())
367 }
368
369 #[test]
370 fn test_set_holland() -> Result<(), Error> {
371 let mut rules = Rules::default();
372 rules.set_crawford(false)?;
373 rules.set_holland(true)?;
374
375 assert_eq!(rules.get_holland(), Ok(true));
376 assert_eq!(rules.get_crawford(), Ok(true));
377 Ok(())
378 }
379}