From 7fa4f497f77471fa145881d759afe0b521971e04 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 10 Jun 2020 20:55:43 +0200 Subject: Added shuffle generator --- src/shuffle.rs | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) (limited to 'src/shuffle.rs') 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 for Standard { + fn sample(&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 for Standard { + fn sample(&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 { + let mut sequence: Vec = Vec::with_capacity(n); + + while sequence.len() != n { + let direction = rand::random::(); + let modifier = rand::random::(); + + 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::>().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) + } +} -- cgit