@@ -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