1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// SPDX-FileCopyrightText: 2022 Declan Rixon <twisted.cubing@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-only

// Exports
pub mod moves;
pub mod subset;

// External submodules
mod cycle;

use rand::distributions::{Distribution, Standard};
use rand::Rng;
use std::fmt;
use std::iter::Iterator;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Axis {
    X,
    Y,
    Z,
}

impl Axis {
    pub fn top(&self) -> Face {
        use Face::*;
        match self {
            Axis::X => R,
            Axis::Y => U,
            Axis::Z => F,
        }
    }

    pub fn bottom(&self) -> Face {
        use Face::*;
        match self {
            Axis::X => L,
            Axis::Y => D,
            Axis::Z => B,
        }
    }

    pub fn complements(&self) -> [Self; 2] {
        match self {
            Self::X => [Self::Y, Self::Z],
            Self::Y => [Self::X, Self::Z],
            Self::Z => [Self::X, Self::Y],
        }
    }

    pub fn complement(a: Self, b: Self) -> Option<Self> {
        match a {
            Self::X => match b {
                Self::X => None,
                Self::Y => Some(Self::Z),
                Self::Z => Some(Self::Y),
            },
            Self::Y => match b {
                Self::X => Some(Self::Z),
                Self::Y => None,
                Self::Z => Some(Self::X),
            },
            Self::Z => match b {
                Self::X => Some(Self::Y),
                Self::Y => Some(Self::X),
                Self::Z => None,
            },
        }
    }

    pub fn belt(&self) -> &'static [usize; 4] {
        match self {
            Axis::X => &[0, 8, 10, 2],
            Axis::Y => &[4, 5, 6, 7],
            Axis::Z => &[3, 1, 9, 11],
        }
    }
}

impl fmt::Display for Axis {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Axis::X => "x",
                Axis::Y => "y",
                Axis::Z => "z",
            }
        )
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Face {
    U,
    D,
    R,
    L,
    F,
    B,
}

impl Distribution<Face> for Standard {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Face {
        use Face::*;
        [U, D, R, L, F, B][rng.gen_range(0..6)]
    }
}

impl Face {
    pub fn opposite(&self) -> Face {
        match self {
            Face::U => Face::D,
            Face::D => Face::U,
            Face::R => Face::L,
            Face::L => Face::R,
            Face::F => Face::B,
            Face::B => Face::F,
        }
    }

    pub fn is_top(&self) -> bool {
        match self {
            Face::U => true,
            Face::D => false,
            Face::R => true,
            Face::L => false,
            Face::F => true,
            Face::B => false,
        }
    }

    pub fn axis(&self) -> Axis {
        match self {
            Face::U => Axis::Y,
            Face::D => Axis::Y,
            Face::R => Axis::X,
            Face::L => Axis::X,
            Face::F => Axis::Z,
            Face::B => Axis::Z,
        }
    }

    pub fn corner_cycle(&self) -> &'static [usize; 4] {
        use cycle::corner::*;
        match &self {
            Face::U => &U,
            Face::D => &D,
            Face::R => &R,
            Face::L => &L,
            Face::F => &F,
            Face::B => &B,
        }
    }

    pub fn corner_cycle_matches(&self, cp: &[usize; 4]) -> bool {
        let mut cycle = self.corner_cycle().clone();
        if cycle == *cp {
            return true;
        }
        for _ in 1..4 {
            cycle.rotate_left(1);
            if cycle == *cp {
                return true;
            }
        }
        false
    }

    pub fn edge_cycle(&self) -> &'static [usize; 4] {
        use cycle::edge::*;
        match &self {
            Face::U => &U,
            Face::D => &D,
            Face::R => &R,
            Face::L => &L,
            Face::F => &F,
            Face::B => &B,
        }
    }
}

impl fmt::Display for Face {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Face::U => "U",
                Face::D => "D",
                Face::R => "R",
                Face::L => "L",
                Face::F => "F",
                Face::B => "B",
            }
        )
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Direction {
    Clockwise,
    CounterClockwise,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Move {
    pub face: Face,
    pub amount: i32,
}

impl fmt::Display for Move {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}{}{}",
            self.face,
            match self.amount {
                1 | -1 => "".to_string(),
                x => x.abs().to_string(),
            },
            if self.amount < 0 { "'" } else { "" },
        )
    }
}

impl Distribution<Move> for Standard {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Move {
        let face = rng.gen();
        let amount = [-1, 1, 2][rng.gen_range(0..3)];
        Move { face, amount }
    }
}

pub struct RandomMoves {
    exclude_face: Option<Face>,
    exclude_axis: Option<Axis>,
    rng: rand::rngs::ThreadRng,
}

impl RandomMoves {
    pub fn new() -> Self {
        RandomMoves {
            rng: rand::thread_rng(),
            exclude_face: None,
            exclude_axis: None,
        }
    }
    pub fn new_from(last_face: Option<Face>, last_axis: Option<Axis>) -> Self {
        RandomMoves {
            rng: rand::thread_rng(),
            exclude_face: last_face,
            exclude_axis: last_axis,
        }
    }
}

impl Iterator for RandomMoves {
    type Item = Move;

    fn next(&mut self) -> Option<Self::Item> {
        'find_next: loop {
            let next: Move = self.rng.gen();
            if let Some(face) = self.exclude_face {
                if face == next.face {
                    continue 'find_next;
                }
            }
            if let Some(axis) = self.exclude_axis {
                if axis == next.face.axis() {
                    continue 'find_next;
                }
            }
            if let Some(face) = self.exclude_face {
                self.exclude_axis = if face == next.face.opposite() {
                    Some(next.face.axis())
                } else {
                    None
                }
            }
            self.exclude_face = Some(next.face);
            return Some(next);
        }
    }
}