parse and apply moves
This commit is contained in:
parent
c2bbc7834c
commit
15a42ef20c
97
src/main.rs
97
src/main.rs
@ -119,7 +119,6 @@ impl FromStr for Move {
|
||||
type Err = &'static str;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
println!("\"{}\"", s);
|
||||
match s {
|
||||
"U" => Ok(Move::U),
|
||||
"U2" => Ok(Move::U2),
|
||||
@ -144,26 +143,19 @@ impl FromStr for Move {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_moves(s: &str) -> Result<Vec<Move>, &'static str> {
|
||||
fn parse_moves(moves_str: &str) -> Result<Vec<Move>, &'static str> {
|
||||
let mut moves = Vec::new();
|
||||
let mut chars = s.chars().filter(|&c| c != ' ').peekable();
|
||||
let mut chars = moves_str.chars().filter(|&c| c != ' ').peekable();
|
||||
|
||||
while let Some(c) = chars.next() {
|
||||
let nco = chars.peek();
|
||||
match nco {
|
||||
Some(nc) => match nc {
|
||||
'2' | '\'' => {
|
||||
let s = format!("{}{}", c, nc);
|
||||
let m = Move::from_str(s.as_str()).expect("unable to parse move");
|
||||
moves.push(m);
|
||||
chars.next();
|
||||
}
|
||||
_ => {
|
||||
let s = c.to_string();
|
||||
let m = Move::from_str(s.as_str()).expect("unable to parse move");
|
||||
moves.push(m);
|
||||
}
|
||||
},
|
||||
Some(nc) if (nc == &'2' || nc == &'\'') => {
|
||||
let s = format!("{}{}", c, nc);
|
||||
let m = Move::from_str(s.as_str()).expect("unable to parse move");
|
||||
moves.push(m);
|
||||
chars.next();
|
||||
}
|
||||
_ => {
|
||||
let s = c.to_string();
|
||||
let m = Move::from_str(s.as_str()).expect("unable to parse move");
|
||||
@ -175,6 +167,32 @@ fn parse_moves(s: &str) -> Result<Vec<Move>, &'static str> {
|
||||
Ok(moves)
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
fn apply_moves(c: &mut Cube, moves: Vec<Move>) {
|
||||
for m in moves {
|
||||
match m {
|
||||
Move::U => { c.u(); },
|
||||
Move::U2 => { c.u(); c.u(); },
|
||||
Move::Up => { c.u(); c.u(); c.u(); },
|
||||
Move::D => { c.d(); },
|
||||
Move::D2 => { c.d(); c.d(); },
|
||||
Move::Dp => { c.d(); c.d(); c.d(); },
|
||||
Move::L => { c.l(); },
|
||||
Move::L2 => { c.l(); c.l(); },
|
||||
Move::Lp => { c.l(); c.l(); c.l(); },
|
||||
Move::R => { c.r(); },
|
||||
Move::R2 => { c.r(); c.r(); }
|
||||
Move::Rp => { c.r(); c.r(); c.r(); }
|
||||
Move::F => { c.f(); },
|
||||
Move::F2 => { c.f(); c.f(); }
|
||||
Move::Fp => { c.f(); c.f(); c.f(); }
|
||||
Move::B => { c.b(); },
|
||||
Move::B2 => { c.b(); c.b(); }
|
||||
Move::Bp => { c.b(); c.b(); c.b(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Cube {
|
||||
u: Face,
|
||||
d: Face,
|
||||
@ -272,55 +290,14 @@ impl Cube {
|
||||
self.l.apply_l(u_tr, u_tm, u_tl);
|
||||
}
|
||||
|
||||
// TODO: efficient counter-clockwise
|
||||
fn up(&mut self) {
|
||||
self.u();
|
||||
self.u();
|
||||
self.u();
|
||||
}
|
||||
|
||||
fn dp(&mut self) {
|
||||
self.d();
|
||||
self.d();
|
||||
self.d();
|
||||
}
|
||||
|
||||
fn lp(&mut self) {
|
||||
self.l();
|
||||
self.l();
|
||||
self.l();
|
||||
}
|
||||
|
||||
fn rp(&mut self) {
|
||||
self.r();
|
||||
self.r();
|
||||
self.r();
|
||||
}
|
||||
|
||||
fn fp(&mut self) {
|
||||
self.f();
|
||||
self.f();
|
||||
self.f();
|
||||
}
|
||||
|
||||
fn bp(&mut self) {
|
||||
self.b();
|
||||
self.b();
|
||||
self.b();
|
||||
}
|
||||
// TODO: efficient double / counter-clockwise
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut c = Cube::new();
|
||||
println!("Here's the cube:\n{}", c);
|
||||
c.r();
|
||||
c.u();
|
||||
c.rp();
|
||||
c.u();
|
||||
c.r();
|
||||
c.u();
|
||||
c.u();
|
||||
c.rp();
|
||||
let moves = parse_moves("R U R' U R U2 R'").expect("unable to parse moves");
|
||||
apply_moves(&mut c, moves);
|
||||
println!("after OLL:\n{}", c);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user