haar_lib/misc/
dice.rs

1//! 6面サイコロ
2
3/// 6面サイコロ
4#[derive(Clone, Debug, PartialEq, Eq)]
5pub struct Dice<T> {
6    /// 上の面
7    pub top: T,
8    /// 下の面
9    pub bottom: T,
10    /// 前の面
11    pub front: T,
12    /// 後ろの面
13    pub back: T,
14    /// 右の面
15    pub right: T,
16    /// 左の面
17    pub left: T,
18}
19
20impl<T> Dice<T>
21where
22    T: Clone,
23{
24    /// [`Dice<T>`]を生成する。
25    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    /// サイコロを左の面が下になるように回転する。
37    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    /// サイコロを右の面が下になるように回転する。
50    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    /// サイコロを前の面が下になるように回転する。
63    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    /// サイコロを後ろの面が下になるように回転する。
76    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    /// サイコロを上からみて時計回りに回転する。
89    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    /// サイコロを上からみて反時計回りに回転する。
102    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}