From 88a93e9ed705f1fbe0047a663436ecf822b5b720 Mon Sep 17 00:00:00 2001 From: Michael Peters Date: Thu, 14 Mar 2024 21:57:44 -0700 Subject: [PATCH] add edge cross movement --- src/cube.rs | 8 ++++++++ src/f2l.rs | 46 +++++++++++++++++++++++++++++++++++----------- src/main.rs | 16 +++++++++++++--- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/cube.rs b/src/cube.rs index b129a49..01d8173 100644 --- a/src/cube.rs +++ b/src/cube.rs @@ -173,6 +173,7 @@ impl Cube { // D pub fn u(&mut self) { + print!("u"); self.u.rotate(); let (f_tr, f_tm, f_tl) = (self.f.tr, self.f.tm, self.f.tl); self.f.apply_t(self.r.tl, self.r.tm, self.r.tr); @@ -182,6 +183,7 @@ impl Cube { } pub fn d(&mut self) { + print!("d"); self.d.rotate(); let (f_br, f_bm, f_bl) = (self.f.br, self.f.bm, self.f.bl); self.f.apply_b(self.l.br, self.l.bm, self.l.bl); @@ -191,6 +193,7 @@ impl Cube { } pub fn l(&mut self) { + print!("l"); self.l.rotate(); let (f_tl, f_ml, f_bl) = (self.f.tl, self.f.ml, self.f.bl); self.f.apply_l(self.u.tl, self.u.ml, self.u.bl); @@ -200,6 +203,7 @@ impl Cube { } pub fn r(&mut self) { + print!("r"); self.r.rotate(); let (f_tr, f_mr, f_br) = (self.f.tr, self.f.mr, self.f.br); self.f.apply_r(self.d.tr, self.d.mr, self.d.br); @@ -209,6 +213,7 @@ impl Cube { } pub fn f(&mut self) { + print!("f"); self.f.rotate(); let (u_bl, u_bm, u_br) = (self.u.bl, self.u.bm, self.u.br); self.u.apply_b(self.l.br, self.l.mr, self.l.tr); @@ -218,6 +223,7 @@ impl Cube { } pub fn b(&mut self) { + print!("b"); self.b.rotate(); let (u_tl, u_tm, u_tr) = (self.u.tl, self.u.tm, self.u.tr); self.u.apply_t(self.r.tr, self.r.mr, self.r.br); @@ -232,6 +238,7 @@ impl Cube { self.u(); } + #[allow(dead_code)] pub fn d2(&mut self) { self.d(); self.d(); @@ -264,6 +271,7 @@ impl Cube { self.u(); } + #[allow(dead_code)] pub fn dp(&mut self) { self.d(); self.d(); diff --git a/src/f2l.rs b/src/f2l.rs index c882303..1ca1a34 100644 --- a/src/f2l.rs +++ b/src/f2l.rs @@ -57,7 +57,7 @@ fn find_edge(c: &Cube, color_a: Color, color_b: Color) -> Option<(EdgeLoc, EdgeC EDGE_LOCS .iter() .map(|&el| (el, get_edge_color(&c, el))) - .find(|&(el, ec)| correct_ec(ec)) + .find(|&(_el, ec)| correct_ec(ec)) } #[rustfmt::skip] @@ -103,17 +103,39 @@ fn move_white_edge_top(c: &mut Cube, el: EdgeLoc, ec: EdgeColor) -> EdgeLoc { } } -fn move_edge_cross(c: &mut Cube, el: EdgeLoc, ec: EdgeColor) {} - -fn solve_cross_white_edge(c: &mut Cube, color: Color) { - // moves the white- edge to the top of the cube with the white - // facing the U direction - let (el_start, ec) = find_edge(c, Color::W, color).expect("unable to find edge"); - let el_top = move_white_edge_top(c, el_start, ec); +#[rustfmt::skip] +fn move_edge_cross(c: &mut Cube, el: EdgeLoc, color: Color) { + // F = G, B = B, L = R, R = O + let eli: i8 = match el { + EdgeLoc::UF => 0, + EdgeLoc::UB => 2, + EdgeLoc::UL => 3, + EdgeLoc::UR => 1, + _ => panic!("invalid edge location: {:?}", el), + }; + let eci: i8 = match color { + Color::G => 0, + Color::B => 2, + Color::R => 3, + Color::O => 1, + _ => panic!("invalid edge color: {}", color), + }; + let d = (eli - eci + 4) % 4; + match d { + 0 => (), + 1 => { c.u(); }, + 2 => { c.u2(); }, + 3 => { c.up(); }, + _ => panic!("invalid d: {}", d) + } } -fn solve_cross(_c: &mut Cube) { - for color in [Color::G, Color::B, Color::R, Color::O] {} +pub fn solve_cross(c: &mut Cube) { + for color in [Color::G, Color::B, Color::R, Color::O] { + let (el_start, ec) = find_edge(c, Color::W, color).expect("unable to find edge"); + let el_top = move_white_edge_top(c, el_start, ec); + move_edge_cross(c, el_top, color); + } } fn solve_f2l(_c: &mut Cube) {} @@ -122,7 +144,9 @@ fn solve_oll(_c: &mut Cube) {} fn solve_pll(_c: &mut Cube) {} -pub fn solve(_c: &mut Cube) {} +pub fn solve(c: &mut Cube) { + solve_cross(c); +} #[cfg(test)] mod tests { diff --git a/src/main.rs b/src/main.rs index 63ba0e6..5670ec1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,10 +5,20 @@ fn main() { let mut c = cube::Cube::new(); println!("initial cube:\n{}", c); - let moves = cube::parse_moves("R U R' U R U2 R'").expect("unable to parse moves"); + let moves = cube::parse_moves( + "R2 F' D' B D' B2 L' B2 R2 L2 F' B2 L' D B2 U' B' F' D' F2 D2 U' B2 D' B'", + ) + .expect("unable to parse moves"); + cube::apply_moves(&mut c, moves); - f2l::solve(&mut c); + // TODO: scramble results are wrong + println!(); + println!("after scramble:\n{}", c); - println!("after OLL:\n{}", c); + f2l::solve_cross(&mut c); + + // TODO: cross is not solved + println!(); + println!("after f2l:\n{}", c); }