Skip to content

Commit ed682fd

Browse files
committed
Add shared interface for picking non-cancelling moves
1 parent c4cd405 commit ed682fd

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

scrambles/src/main/java/org/worldcubeassociation/tnoodle/puzzle/LayeredRandomizationCubePuzzle.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,7 @@ public PuzzleStateAndGenerator generateRandomMoves(Random r) {
6464
}
6565
}
6666

67-
// we take a "classic" big cube scramble sequence as reference
68-
// because there is currently no API to generate N random moves directly
69-
// (and I am too lazy to write one if we already have something that comes close)
70-
PuzzleStateAndGenerator randomMovesPsag = super.generateRandomMoves(r);
71-
String[] randomMoves = AlgorithmBuilder.splitAlgorithm(randomMovesPsag.generator);
72-
73-
int remainingLength = this.getRandomMoveCount() - ab.getTotalCost();
74-
75-
for (int i = 0; i < remainingLength; i++) {
76-
try {
77-
ab.appendMove(randomMoves[i]);
78-
} catch (InvalidMoveException e) {
79-
l.log(Level.SEVERE, "", e);
80-
throw new RuntimeException(e);
81-
}
82-
}
67+
fillWithRandomMoves(ab, r, getRandomMoveCount());
8368

8469
return ab.getStateAndGenerator();
8570
}

scrambles/src/main/java/org/worldcubeassociation/tnoodle/scrambles/Puzzle.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -837,26 +837,42 @@ boolean movesCommute(String move1, String move2) {
837837
*/
838838
@NoExport
839839
public PuzzleStateAndGenerator generateRandomMoves(Random r) {
840-
AlgorithmBuilder ab = new AlgorithmBuilder(
841-
this, MergingMode.NO_MERGING);
842-
while(ab.getTotalCost() < getRandomMoveCount()) {
843-
Map<String, ? extends PuzzleState> successors =
844-
ab.getState().getScrambleSuccessors();
845-
String move;
840+
AlgorithmBuilder ab = new AlgorithmBuilder(this, MergingMode.NO_MERGING);
841+
fillWithRandomMoves(ab, r, getRandomMoveCount());
842+
return ab.getStateAndGenerator();
843+
}
844+
845+
protected static void fillWithRandomMoves(AlgorithmBuilder ab, Random r, int targetCost) {
846+
while(ab.getTotalCost() < targetCost) {
847+
String move = chooseRandomSuccessorMove(ab, r);
846848
try {
847-
do {
848-
move = choose(r, successors.keySet());
849-
// If this move happens to be redundant, there is no
850-
// reason to select this move again in vain.
851-
successors.remove(move);
852-
} while(ab.isRedundant(move));
853849
ab.appendMove(move);
854850
} catch(InvalidMoveException e) {
855851
l.log(Level.SEVERE, "", e);
856852
throw new RuntimeException(e);
857853
}
858854
}
859-
return ab.getStateAndGenerator();
855+
}
856+
857+
protected static String chooseRandomSuccessorMove(AlgorithmBuilder ab, Random r) {
858+
Map<String, ? extends PuzzleState> successors =
859+
ab.getState().getScrambleSuccessors();
860+
Set<String> moveSet = successors.keySet();
861+
862+
String move;
863+
try {
864+
do {
865+
move = choose(r, moveSet);
866+
// If this move happens to be redundant, there is no
867+
// reason to select this move again in vain.
868+
successors.remove(move);
869+
} while(ab.isRedundant(move));
870+
} catch(InvalidMoveException e) {
871+
l.log(Level.SEVERE, "", e);
872+
throw new RuntimeException(e);
873+
}
874+
875+
return move;
860876
}
861877

862878
public static int[] cloneArr(int[] src) {

0 commit comments

Comments
 (0)