1#[derive(Clone, Debug, PartialEq, Eq)]
5pub struct Dice<T> {
6 pub top: T,
8 pub bottom: T,
10 pub front: T,
12 pub back: T,
14 pub right: T,
16 pub left: T,
18}
19
20impl<T> Dice<T>
21where
22 T: Clone,
23{
24 pub fn new(top: T, bottom: T, front: T, back: T, right: T, left: T) -> Self {
26 Dice {
27 top,
28 bottom,
29 front,
30 back,
31 right,
32 left,
33 }
34 }
35
36 pub fn rot_left(&self) -> Self {
38 let Dice {
39 top,
40 bottom,
41 front,
42 back,
43 right,
44 left,
45 } = self.clone();
46 Dice::new(right, left, front, back, bottom, top)
47 }
48
49 pub fn rot_right(&self) -> Self {
51 let Dice {
52 top,
53 bottom,
54 front,
55 back,
56 right,
57 left,
58 } = self.clone();
59 Dice::new(left, right, front, back, top, bottom)
60 }
61
62 pub fn rot_front(&self) -> Self {
64 let Dice {
65 top,
66 bottom,
67 front,
68 back,
69 right,
70 left,
71 } = self.clone();
72 Dice::new(back, front, top, bottom, right, left)
73 }
74
75 pub fn rot_back(&self) -> Self {
77 let Dice {
78 top,
79 bottom,
80 front,
81 back,
82 right,
83 left,
84 } = self.clone();
85 Dice::new(front, back, bottom, top, right, left)
86 }
87
88 pub fn rot_clockwise(&self) -> Self {
90 let Dice {
91 top,
92 bottom,
93 front,
94 back,
95 right,
96 left,
97 } = self.clone();
98 Dice::new(top, bottom, right, left, back, front)
99 }
100
101 pub fn rot_counterclockwise(&self) -> Self {
103 let Dice {
104 top,
105 bottom,
106 front,
107 back,
108 right,
109 left,
110 } = self.clone();
111 Dice::new(top, bottom, left, right, front, back)
112 }
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118
119 #[test]
120 fn test() {
121 let dice = Dice::new(1, 6, 2, 5, 3, 4);
122
123 assert_eq!(dice.rot_right(), dice.rot_left().rot_left().rot_left());
124 assert_eq!(dice.rot_front(), dice.rot_back().rot_back().rot_back());
125 assert_eq!(
126 dice.rot_clockwise(),
127 dice.rot_counterclockwise()
128 .rot_counterclockwise()
129 .rot_counterclockwise()
130 )
131 }
132}