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
|
/* ************************************************************************** */
/* */
/* . */
/* scramble.rs / \ */
/* / \ */
/* By: charles <charles.cabergs@gmail.com> /o o \ */
/* / v \ */
/* Created: 2020/06/25 13:24:17 by charles / _ \ */
/* Updated: 2020/06/25 13:24:18 by charles '-----------' */
/* */
/* ************************************************************************** */
use std::fmt;
use std::str;
use rand::{
distributions::{Distribution, Standard},
Rng
};
pub struct Scramble(Vec<Move>);
impl Scramble {
pub fn new_rand(n: usize) -> Scramble {
let mut sequence: Vec<Move> = Vec::with_capacity(n);
while sequence.len() != n {
let direction = rand::random::<Direction>();
let modifier = rand::random::<Modifier>();
if let Some(l) = sequence.last() {
if l.direction == direction {
continue;
}
}
sequence.push(Move { direction, modifier });
}
Scramble(sequence)
}
}
impl fmt::Display for Scramble {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0.iter().map(|m| m.to_string()).collect::<Vec<String>>().join(" "))
}
}
impl str::FromStr for Scramble {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let strs = s.split(" ");
let mut scramble = Scramble(Vec::new());
for s in strs {
scramble.0.push(s.parse()?);
}
Ok(scramble)
}
}
impl str::FromStr for Move {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
use Direction::*;
use Modifier::*;
let mut cs = s.chars();
let direction = match cs.next() {
Some('F') => Front,
Some('B') => Back,
Some('D') => Down,
Some('U') => Up,
Some('R') => Right,
Some('L') => Left,
Some(_) => return Err("Move direction isn't valid"),
None => return Err("Move format is empty"),
};
let modifier = match cs.next() {
Some('\'') => Prime,
Some('2') => Twice,
Some(_) => return Err("Move modifier isn't valid"),
None => No,
};
if let Some(_) = cs.next() {
return Err("Unexpected character in move");
}
Ok(Move{ direction, modifier })
}
}
#[derive(PartialEq)]
enum Direction { Front, Back, Down, Up, Right, Left, }
enum Modifier { No, Twice, Prime, }
struct Move {
direction: Direction,
modifier: Modifier,
}
// https://stackoverflow.com/questions/48490049
impl Distribution<Direction> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Direction {
use Direction::*;
match rng.gen_range(0, 6) {
0 => Front,
1 => Back,
2 => Down,
3 => Up,
4 => Right,
_ => Left,
}
}
}
impl Distribution<Modifier> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Modifier {
use Modifier::*;
match rng.gen_range(0, 3) {
0 => No,
1 => Twice,
_ => Prime,
}
}
}
impl fmt::Display for Move {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Direction::*;
use Modifier::*;
let letter = match self.direction {
Front => "F",
Back => "B",
Down => "D",
Up => "U",
Right => "R",
Left => "L",
};
let modifier = match self.modifier {
No => "",
Twice => "2",
Prime => "'",
};
write!(f, "{}{}", letter, modifier)
}
}
|