-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: generators * wip: refactor index & cross solved gen * feat: cfop state gen & scrmable module * fix: scramble * feat: more generators * fix: formatting * update: re-use solver in scramble * update: README.md
- Loading branch information
1 parent
8d85630
commit e1851fb
Showing
9 changed files
with
379 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
use crate::constants::{CO_COUNT, CP_COUNT, EO_COUNT, EP_COUNT}; | ||
|
||
use super::{cubie::CubieCube, index::*}; | ||
use rand::{rngs::ThreadRng, seq::SliceRandom, thread_rng, Rng}; | ||
|
||
/// Randomly swaps corner or edges to fix parity. | ||
fn fix_parity(state: &mut CubieCube, rng: &mut ThreadRng, corners: Vec<usize>, edges: Vec<usize>) { | ||
if rng.gen_bool(0.5) { | ||
swap_edges(state, rng, edges) | ||
} else { | ||
swap_corners(state, rng, corners) | ||
} | ||
} | ||
|
||
fn swap_edges(state: &mut CubieCube, rng: &mut ThreadRng, edges: Vec<usize>) { | ||
let pos: Vec<&usize> = edges.choose_multiple(rng, 2).collect(); | ||
let a = *pos[0]; | ||
let b = *pos[1]; | ||
state.ep.swap(a, b) | ||
} | ||
|
||
fn swap_corners(state: &mut CubieCube, rng: &mut ThreadRng, corners: Vec<usize>) { | ||
let pos: Vec<&usize> = corners.choose_multiple(rng, 2).collect(); | ||
let a = *pos[0]; | ||
let b = *pos[1]; | ||
state.cp.swap(a, b) | ||
} | ||
|
||
/// Generates a random state with corners solved. | ||
pub fn generate_state_corners_solved() -> CubieCube { | ||
let mut rng = thread_rng(); | ||
let mut state = CubieCube { | ||
ep: index_to_ep(rng.gen_range(0..EP_COUNT)), | ||
eo: index_to_eo(rng.gen_range(0..EO_COUNT)), | ||
..Default::default() | ||
}; | ||
|
||
if !state.is_solvable() { | ||
swap_edges(&mut state, &mut rng, (0..12).collect()); | ||
} | ||
|
||
state | ||
} | ||
|
||
/// Generates a random state with edges solved. | ||
pub fn generate_state_edges_solved() -> CubieCube { | ||
let mut rng = thread_rng(); | ||
let mut state = CubieCube { | ||
cp: index_to_cp(rng.gen_range(0..CP_COUNT)), | ||
co: index_to_co(rng.gen_range(0..CO_COUNT)), | ||
..Default::default() | ||
}; | ||
|
||
if !state.is_solvable() { | ||
swap_corners(&mut state, &mut rng, (0..8).collect()); | ||
} | ||
|
||
state | ||
} | ||
|
||
/// Generates a random state with oriented solved last layer cross. | ||
pub fn generate_state_oll_cross_solved() -> CubieCube { | ||
let mut rng = thread_rng(); | ||
let mut state = CubieCube { | ||
cp: index_to_cp_f2l(rng.gen_range(0..4)), | ||
co: index_to_co_f2l(rng.gen_range(0..27)), | ||
ep: index_to_ep_f2l(rng.gen_range(0..24)), | ||
..Default::default() | ||
}; | ||
|
||
if !state.is_solvable() { | ||
fix_parity(&mut state, &mut rng, (0..4).collect(), (4..8).collect()) | ||
} | ||
|
||
state | ||
} | ||
|
||
/// Generates a random state with oriented last layer corners and edges. | ||
pub fn generate_state_oll_solved() -> CubieCube { | ||
let mut rng = thread_rng(); | ||
let mut state = CubieCube { | ||
cp: index_to_cp_f2l(rng.gen_range(0..4)), | ||
ep: index_to_ep_f2l(rng.gen_range(0..24)), | ||
..Default::default() | ||
}; | ||
|
||
if !state.is_solvable() { | ||
fix_parity(&mut state, &mut rng, (0..4).collect(), (4..8).collect()) | ||
} | ||
|
||
state | ||
} | ||
|
||
/// Generates a random state with solved First two layer. | ||
pub fn generate_state_f2l_solved() -> CubieCube { | ||
let mut rng = thread_rng(); | ||
let mut state = CubieCube { | ||
cp: index_to_cp_f2l(rng.gen_range(0..4)), | ||
co: index_to_co_f2l(rng.gen_range(0..27)), | ||
ep: index_to_ep_f2l(rng.gen_range(0..24)), | ||
eo: index_to_eo_f2l(rng.gen_range(0..8)), | ||
}; | ||
|
||
if !state.is_solvable() { | ||
fix_parity(&mut state, &mut rng, (0..4).collect(), (4..8).collect()) | ||
} | ||
|
||
state | ||
} | ||
|
||
/// Generates a random state with solved cross. | ||
pub fn generate_state_cross_solved() -> CubieCube { | ||
let mut rng = thread_rng(); | ||
let mut state = CubieCube { | ||
cp: index_to_cp(rng.gen_range(0..CP_COUNT)), | ||
co: index_to_co(rng.gen_range(0..CO_COUNT)), | ||
ep: index_to_ep_cross(rng.gen_range(0..40320)), | ||
eo: index_to_eo_cross(rng.gen_range(0..128)), | ||
}; | ||
|
||
if !state.is_solvable() { | ||
fix_parity(&mut state, &mut rng, (0..8).collect(), (0..8).collect()) | ||
} | ||
|
||
state | ||
} | ||
|
||
/// Generates a random state on the cubie level. | ||
pub fn generate_random_state() -> CubieCube { | ||
let mut rng = thread_rng(); | ||
let mut state = CubieCube { | ||
cp: index_to_cp(rng.gen_range(0..CP_COUNT)), | ||
co: index_to_co(rng.gen_range(0..CO_COUNT)), | ||
ep: index_to_ep(rng.gen_range(0..EP_COUNT)), | ||
eo: index_to_eo(rng.gen_range(0..EO_COUNT)), | ||
}; | ||
|
||
if !state.is_solvable() { | ||
fix_parity(&mut state, &mut rng, (0..8).collect(), (0..12).collect()) | ||
} | ||
|
||
state | ||
} |
Oops, something went wrong.