aboutsummaryrefslogtreecommitdiff
path: root/src/shuffle.rs
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-06-10 20:55:43 +0200
committerCharles <sircharlesaze@gmail.com>2020-06-10 20:55:43 +0200
commit7fa4f497f77471fa145881d759afe0b521971e04 (patch)
tree4cb813362075667db6dbec140dc2ce166dc4d894 /src/shuffle.rs
parent47d15d537c61b828e72113cc8d3d8da3db9a2efb (diff)
downloadrutikmer-7fa4f497f77471fa145881d759afe0b521971e04.tar.gz
rutikmer-7fa4f497f77471fa145881d759afe0b521971e04.tar.bz2
rutikmer-7fa4f497f77471fa145881d759afe0b521971e04.zip
Added shuffle generator
Diffstat (limited to 'src/shuffle.rs')
-rw-r--r--src/shuffle.rs99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/shuffle.rs b/src/shuffle.rs
index e69de29..8d421b6 100644
--- a/src/shuffle.rs
+++ b/src/shuffle.rs
@@ -0,0 +1,99 @@
+use rand::{distributions::{Distribution, Standard}, Rng};
+
+#[derive(PartialEq)]
+enum MoveDirection {
+ Front,
+ Back,
+ Down,
+ Up,
+ Right,
+ Left,
+}
+
+enum MoveModifier {
+ None,
+ Twice,
+ Prime,
+}
+
+pub struct Move {
+ direction: MoveDirection,
+ modifier: MoveModifier,
+}
+
+
+// https://stackoverflow.com/questions/48490049
+impl Distribution<MoveDirection> for Standard {
+ fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> MoveDirection {
+ use MoveDirection::*;
+
+ match rng.gen_range(0, 6) {
+ 0 => Front,
+ 1 => Back,
+ 2 => Down,
+ 3 => Up,
+ 4 => Right,
+ _ => Left,
+ }
+ }
+}
+
+impl Distribution<MoveModifier> for Standard {
+ fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> MoveModifier {
+ use MoveModifier::*;
+
+ match rng.gen_range(0, 3) {
+ 0 => None,
+ 1 => Twice,
+ _ => Prime,
+ }
+ }
+}
+
+impl Move {
+ pub fn sequence(n: usize) -> Vec<Move> {
+ let mut sequence: Vec<Move> = Vec::with_capacity(n);
+
+ while sequence.len() != n {
+ let direction = rand::random::<MoveDirection>();
+ let modifier = rand::random::<MoveModifier>();
+
+ if let Some(l) = sequence.last() {
+ if l.direction == direction {
+ continue;
+ }
+ }
+ sequence.push(Move { direction, modifier });
+ }
+ sequence
+ }
+
+ pub fn string_sequence(n: usize) -> String {
+ let seq = Move::sequence(n);
+ seq.iter().map(|m| m.to_string() + " ").collect::<Vec<String>>().join(" ")
+ }
+}
+
+use std::fmt;
+
+impl fmt::Display for Move {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ use MoveDirection::*;
+ use MoveModifier::*;
+
+ let letter = match self.direction {
+ Front => "F",
+ Back => "B",
+ Down => "D",
+ Up => "U",
+ Right => "R",
+ Left => "L",
+ };
+ let modifier = match self.modifier {
+ None => "",
+ Twice => "2",
+ Prime => "'",
+ };
+ write!(f, "{}{}", letter, modifier)
+ }
+}