From 2a403f9d6c28621c00e308c5c1cc707451bd77b4 Mon Sep 17 00:00:00 2001 From: Luckas Date: Wed, 15 May 2024 04:40:00 +0300 Subject: [PATCH] fix: check duplicated pieces (#11) --- kewb/src/cube/cubie.rs | 6 +++++- kewb/src/cube/mod.rs | 1 + kewb/src/cube/utils.rs | 6 ++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 kewb/src/cube/utils.rs diff --git a/kewb/src/cube/cubie.rs b/kewb/src/cube/cubie.rs index 3a61292..3141d41 100644 --- a/kewb/src/cube/cubie.rs +++ b/kewb/src/cube/cubie.rs @@ -1,7 +1,7 @@ use self::{Corner::*, Edge::*, Move::*}; use crate::error::Error; -use super::{facelet::*, moves::*}; +use super::{facelet::*, moves::*, utils::has_duplicates}; use std::ops::Mul; /// Represents the 8 corners on the cube, described by the layer they are on. @@ -191,6 +191,10 @@ impl CubieCube { /// Checks if CubieCube is a valid cubie representation. pub fn is_solvable(&self) -> bool { + if has_duplicates(&self.cp) || has_duplicates(&self.ep) { + return false; + } + let c_perm = self.count_corner_perm(); let e_perm = self.count_edge_perm(); let c_twist = self.count_corner_twist(); diff --git a/kewb/src/cube/mod.rs b/kewb/src/cube/mod.rs index 9feda5d..a5ff87a 100644 --- a/kewb/src/cube/mod.rs +++ b/kewb/src/cube/mod.rs @@ -5,3 +5,4 @@ pub mod generators; pub mod index; pub mod moves; pub mod scramble; +mod utils; diff --git a/kewb/src/cube/utils.rs b/kewb/src/cube/utils.rs new file mode 100644 index 0000000..1aead52 --- /dev/null +++ b/kewb/src/cube/utils.rs @@ -0,0 +1,6 @@ +pub fn has_duplicates(items: &[T]) -> bool { + items + .iter() + .enumerate() + .any(|(i, item)| items[i + 1..].contains(item)) +}