Skip to content

Commit

Permalink
made non redundant move movable
Browse files Browse the repository at this point in the history
  • Loading branch information
Odilf committed Jul 11, 2023
1 parent 8b0de6c commit abef42d
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions barbarosa/src/cube_n/moves/non_redundant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use strum::IntoEnumIterator;

use crate::{
cube_n::space::{Axis, Direction, Face},
generic::Alg,
generic::{Alg, Cube, Movable, Move},
};

use super::{Amount, AxisMove};
Expand Down Expand Up @@ -117,8 +117,8 @@ impl NonRedundantAxisMove {
/// In this context, "absorve" means modifying the original [NonRedundantAxisMove] in such
/// a way that the result is the same as doing both moves sequentially.
///
/// This function modifies `self` in-place. It returns `Ok(())` when it is possible to absorve
/// the move, and an [AbsorveResult] otherwise.
/// This function modifies `self` in-place. It returns an [AbsorveResult] specifying
/// what happened with the input move.
///
/// # Example
///
Expand Down Expand Up @@ -243,7 +243,7 @@ impl Alg<AxisMove> {
let mut moves: Vec<AxisMove> = Vec::new();

while moves.len() < length {
let chosen = match moves.get(0) {
let chosen = match moves.get(moves.len() - 1) {
Some(mov) => NonRedundantAxisMove::given_last_axis(&mov.face.axis)
.choose(rng)
.expect("`given_last_axis` returns 30 elements"),
Expand Down Expand Up @@ -324,6 +324,31 @@ impl Alg<AxisMove> {
}
}

impl Move for NonRedundantAxisMove {
fn inverse(&self) -> Self {
match self {
NonRedundantAxisMove::Single(mov) => NonRedundantAxisMove::Single(mov.inverse()),
NonRedundantAxisMove::Double {
axis,
amount_positive,
amount_negative,
} => NonRedundantAxisMove::Double {
axis: *axis,
amount_positive: *amount_positive * Direction::Negative,
amount_negative: *amount_negative * Direction::Negative,
},
}
}
}

impl<C: Cube + Movable<AxisMove>> Movable<NonRedundantAxisMove> for C {
fn apply(&mut self, m: &NonRedundantAxisMove) {
for mov in m.moves() {
self.apply(&mov);
}
}
}

impl From<AxisMove> for NonRedundantAxisMove {
fn from(value: AxisMove) -> Self {
NonRedundantAxisMove::Single(value)
Expand All @@ -336,6 +361,20 @@ impl From<NonRedundantAxisMove> for Alg<AxisMove> {
}
}

impl From<Alg<NonRedundantAxisMove>> for Alg<AxisMove> {
fn from(value: Alg<NonRedundantAxisMove>) -> Self {
let mut moves = Vec::with_capacity(value.moves.len());

for nr_move in value.moves {
for mov in nr_move.moves() {
moves.push(mov);
}
}

Self::new(moves)
}
}

impl std::fmt::Display for NonRedundantAxisMove {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.moves().map(|mov| mov.to_string()).join(" "))
Expand Down

0 comments on commit abef42d

Please sign in to comment.