Skip to content

Commit

Permalink
Make randomization configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
torjusti committed Sep 10, 2023
1 parent 67ea14f commit 3d5f575
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 31 deletions.
12 changes: 8 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
type Scrambler = '3x3' | '2gll' | 'cmll' | 'corners' | 'edges' | 'lse' | 'lsll' | 'pll' | 'zbll' | 'zzls';
export type Scrambler = '3x3' | '2gll' | 'cmll' | 'corners' | 'edges' | 'lse' | 'lsll' | 'pll' | 'zbll' | 'zzls';

type Solver = 'kociemba' | 'cross' | 'eoline' | 'fb' | 'xcross';
export type Solver = 'kociemba' | 'cross' | 'eoline' | 'fb' | 'xcross';

export interface Settings {
randomize: boolean;
}

declare module 'cube-solver' {
// Main method for generating scrambles.
export const scramble: (scrambler: Scrambler) => string;
export const scramble: (scrambler: Scrambler, settings: Setings) => string;
// Solve specific subset of a given cube.
export const solve: (scramble: string, type: Solver) => string;
export const solve: (scramble: string, type: Solver, settings: Settings) => string;
// Initialize given solver.
export const initialize: (solver: Solver) => void;
};
11 changes: 7 additions & 4 deletions src/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Search {
};
}

search(indexes, depth, lastMove, solution) {
search(indexes, depth, lastMove, solution, randomize) {
let minimumDistance = 0;

for (let i = 0; i < this.pruningTables.length; i += 1) {
Expand Down Expand Up @@ -111,10 +111,13 @@ class Search {
solve(settings) {
this.initialize();

this.settings = { maxDepth: 22, // For the Kociemba solver.
this.settings = {
maxDepth: 22, // For the Kociemba solver.
lastMove: null,
format: true,
...settings };
randomize: false,
...settings,
};

const indexes = this.settings.indexes || [];

Expand All @@ -139,7 +142,7 @@ class Search {
}

for (let depth = 0; depth <= this.settings.maxDepth; depth += 1) {
const solution = this.search(indexes, depth, this.settings.lastMove, []);
const solution = this.search(indexes, depth, this.settings.lastMove, [], this.settings.randomize);

if (solution) {
if (this.settings.format) {
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import getZBLLScramble from './scramblers/zbll';
import getZZLSScramble from './scramblers/zzls';

export default {
solve: (scramble, solver = 'kociemba') => {
solve: (scramble, solver = 'kociemba', settings) => {
const solvers = {
kociemba,
cross: crossSolver,
Expand All @@ -25,7 +25,7 @@ export default {
};

if (solvers[solver]) {
return solvers[solver](scramble);
return solvers[solver](scramble, settings);
}

throw new Error('Specified solver does not exist.');
Expand All @@ -46,7 +46,7 @@ export default {
};

if (scramblers[scrambler]) {
return scramblers[scrambler]();
return scramblers[scrambler](settings);
}

throw new Error('Specified scrambler does not exist.');
Expand Down
4 changes: 3 additions & 1 deletion src/scramblers/2gll.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import getScrambleForPieces from './scramblePieces';
import { Edges, Corners } from '../cube';

const getScramble = () => getScrambleForPieces(
const getScramble = (settings) => getScrambleForPieces(
settings,

[Edges.UR, Edges.UF, Edges.UL, Edges.UB],

[],
Expand Down
4 changes: 2 additions & 2 deletions src/scramblers/3x3.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { solveCoordinates } from '../solvers/kociemba';
* of the cube. This guarantees valid orientations, but we have to
* ensure the parity of the generated cube is valid.
*/
const getRandomScramble = () => {
const getRandomScramble = (settings) => {
let eo;
let ep;
let co;
Expand All @@ -39,7 +39,7 @@ const getRandomScramble = () => {
);
} while (getParity(ep) !== getParity(cp));

return solveCoordinates(eo, ep, co, cp);
return solveCoordinates(eo, ep, co, cp, settings);
};

export default getRandomScramble;
4 changes: 3 additions & 1 deletion src/scramblers/cmll.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import getScrambleForPieces from './scramblePieces';
import { Edges, Corners } from '../cube';

const getScramble = () => getScrambleForPieces(
const getScramble = (settings) => getScrambleForPieces(
settings,

[Edges.UR, Edges.UF, Edges.UL, Edges.UB, Edges.DF, Edges.DB],

[Corners.URF, Corners.UFL, Corners.ULB, Corners.UBR],
Expand Down
2 changes: 2 additions & 0 deletions src/scramblers/corners.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import getScrambleForPieces from './scramblePieces';

const getScramble = () => getScrambleForPieces(
settings,

[],

[0, 1, 2, 3, 4, 5, 6, 7],
Expand Down
4 changes: 3 additions & 1 deletion src/scramblers/edges.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import getScrambleForPieces from './scramblePieces';

const getScramble = () => getScrambleForPieces(
const getScramble = (settings) => getScrambleForPieces(
settings,

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],

[],
Expand Down
4 changes: 3 additions & 1 deletion src/scramblers/lse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Edges } from '../cube';

const LSE_EDGES = [Edges.UR, Edges.UF, Edges.UL, Edges.UB, Edges.DF, Edges.DB];

const getScramble = () => getScrambleForPieces(
const getScramble = (settings) => getScrambleForPieces(
settings,

LSE_EDGES,

[],
Expand Down
4 changes: 3 additions & 1 deletion src/scramblers/lsll.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import getScrambleForPieces from './scramblePieces';
import { Edges, Corners } from '../cube';

const getScramble = () => getScrambleForPieces(
const getScramble = (settings) => getScrambleForPieces(
settings,

[Edges.UR, Edges.UF, Edges.UL, Edges.UB, Edges.FR],

[Corners.URF, Corners.UFL, Corners.ULB, Corners.UBR, Corners.DFR],
Expand Down
4 changes: 3 additions & 1 deletion src/scramblers/pll.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import getScrambleForPieces from './scramblePieces';
import { Edges, Corners } from '../cube';

const getScramble = () => getScrambleForPieces(
const getScramble = (settings) => getScrambleForPieces(
settings,

[Edges.UR, Edges.UF, Edges.UL, Edges.UB],

[Corners.URF, Corners.UFL, Corners.ULB, Corners.UBR],
Expand Down
3 changes: 2 additions & 1 deletion src/scramblers/scramblePieces.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const adjustUpperFace = (pieces, amount) => {
* for the provided edges and corners, which will be scrambled randomly.
*/
export const getScrambleForPieces = (
settings,
permutationEdges,
permutationCorners,
orientationEdges = permutationEdges,
Expand Down Expand Up @@ -95,7 +96,7 @@ export const getScrambleForPieces = (
}
} while (getParity(ep) !== getParity(cp));

return solveCoordinates(eo, ep, co, cp);
return solveCoordinates(eo, ep, co, cp, settings);
};

export default getScrambleForPieces;
4 changes: 3 additions & 1 deletion src/scramblers/zbll.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const EDGES = [Edges.UR, Edges.UF, Edges.UL, Edges.UB];

const CORNERS = [Corners.URF, Corners.UFL, Corners.ULB, Corners.UBR];

const getScramble = () => getScrambleForPieces(
const getScramble = (settings) => getScrambleForPieces(
settings,

EDGES,

CORNERS,
Expand Down
4 changes: 3 additions & 1 deletion src/scramblers/zzls.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const CORNERS = [
Corners.DFR,
];

const getScramble = () => getScrambleForPieces(
const getScramble = (settings) => getScrambleForPieces(
settings,

[Edges.UR, Edges.UF, Edges.UL, Edges.UB, Edges.FR],

CORNERS,
Expand Down
2 changes: 1 addition & 1 deletion src/solvers/EOLineSolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export const EOLineSearch = new Search(() => ({
pruningTables: [['EdgeOrientation'], ['EdgePermutation']],
}));

const EOLineSolver = (scramble) => EOLineSearch.solve({ scramble });
const EOLineSolver = (scramble, settings) => EOLineSearch.solve({ scramble, ...settings });

export default EOLineSolver;
2 changes: 1 addition & 1 deletion src/solvers/XCrossSolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ export const XCrossSearch = new Search(() => ({
],
}));

const XCrossSolver = (scramble) => XCrossSearch.solve({ scramble });
const XCrossSolver = (scramble, settings) => XCrossSearch.solve({ scramble, settings });

export default XCrossSolver;
2 changes: 1 addition & 1 deletion src/solvers/crossSolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export const CrossSearch = new Search(() => ({
pruningTables: [['EdgePermutation'], ['EdgeOrientation']],
}));

const crossSolver = (scramble) => CrossSearch.solve({ scramble });
const crossSolver = (scramble, settings) => CrossSearch.solve({ scramble, ...settings });

export default crossSolver;
2 changes: 1 addition & 1 deletion src/solvers/firstBlockSolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ export const FirstBlockSearch = new Search(() => ({
],
}));

const firstBlockSolver = (scramble) => FirstBlockSearch.solve({ scramble });
const firstBlockSolver = (scramble, settings) => FirstBlockSearch.solve({ scramble, ...settings });

export default firstBlockSolver;
10 changes: 5 additions & 5 deletions src/solvers/kociemba.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,23 @@ class PhaseOneSearch extends Search {

export const phaseOne = new PhaseOneSearch(phaseOneTables);

const kociemba = (scramble, maxDepth = 22) => {
const kociemba = (scramble, settings) => {
if (Array.isArray(scramble)) {
return phaseOne.solve({
indexes: scramble,
maxDepth,
...settings,
});
}

return phaseOne.solve({
scramble,
maxDepth,
...settings
});
};

export default kociemba;

export const solveCoordinates = (eo, ep, co, cp) => kociemba([
export const solveCoordinates = (eo, ep, co, cp, settings) => kociemba([
Math.floor(getIndexFromPermutation(ep, [8, 9, 10, 11], true) / 24),
getIndexFromOrientation(co, 3),
getIndexFromOrientation(eo, 2),
Expand All @@ -254,4 +254,4 @@ export const solveCoordinates = (eo, ep, co, cp) => kociemba([
getIndexFromPermutation(cp, [0, 1, 2, 3, 4, 5]),
getIndexFromPermutation(ep, [0, 1, 2]),
getIndexFromPermutation(ep, [3, 4, 5]),
]);
], settings);

0 comments on commit 3d5f575

Please sign in to comment.