-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
3,241 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* FrontierCube.java | ||
*/ | ||
package com.parabon.rubik; | ||
import java.util.List; | ||
|
||
import ch.randelshofer.rubik.RubiksCube; | ||
|
||
public class FrontierCube extends RubiksCube { | ||
|
||
void apply(Move move) { | ||
switch (move) { | ||
case F: twistSide(0, false); break; | ||
case F_: twistSide(0, true); break; | ||
case F2: twistSide(0, true); | ||
twistSide(0, true); break; | ||
|
||
case R: twistSide(1, false); break; | ||
case R_: twistSide(1, true); break; | ||
case R2: twistSide(1, true); | ||
twistSide(1, true); break; | ||
|
||
case D: twistSide(2, false); break; | ||
case D_: twistSide(2, true); break; | ||
case D2: twistSide(2, true); | ||
twistSide(2, true); break; | ||
|
||
case B: twistSide(3, false); break; | ||
case B_: twistSide(3, true); break; | ||
case B2: twistSide(3, true); | ||
twistSide(3, true); break; | ||
|
||
case L: twistSide(4, false); break; | ||
case L_: twistSide(4, true); break; | ||
case L2: twistSide(4, true); | ||
twistSide(4, true); break; | ||
|
||
case U: twistSide(5, false); break; | ||
case U_: twistSide(5, true); break; | ||
case U2: twistSide(5, true); | ||
twistSide(5, true); break; | ||
} | ||
} | ||
|
||
public void apply(List<Move> moves) { | ||
for (Move m : moves) apply(m); | ||
} | ||
|
||
} |
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,18 @@ | ||
/* | ||
* Move.java | ||
*/ | ||
package com.parabon.rubik; | ||
|
||
public enum Move { | ||
|
||
F("F"), F_("F'"), F2("F2"), | ||
B("B"), B_("B'"), B2("B2"), | ||
U("U"), U_("U'"), U2("U2"), | ||
D("D"), D_("D'"), D2("D2"), | ||
L("L"), L_("L'"), L2("L2"), | ||
R("R"), R_("R'"), R2("R2"); | ||
|
||
private final String label; | ||
Move(String name) { this.label = name; } | ||
public String toString() { return label; } | ||
} |
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,22 @@ | ||
/* | ||
* Solver.java | ||
*/ | ||
package com.parabon.rubik; | ||
import java.util.List; | ||
|
||
|
||
/** | ||
* An interface for a class that can solve any arbitrarily scrambled | ||
* Rubik's Cube with the goal of doing so in the minimal number of moves. | ||
*/ | ||
public interface Solver { | ||
|
||
/** | ||
* Returns a minimal length list of moves that will solve the | ||
* specified cube. | ||
* @param cube an arbitrarily scrambled Rubik's Cube. | ||
* @return a minimal length list of moves that will solve the | ||
* specified cube. | ||
*/ | ||
List<Move> solve(FrontierCube cube); | ||
} |
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,110 @@ | ||
package ec.app.rubik; | ||
|
||
import java.io.Serializable; | ||
import java.util.Vector; | ||
|
||
import ec.EvolutionState; | ||
/** | ||
* Create and save new rubik's cubes from different difficulty. | ||
* @author nacho | ||
* | ||
*/ | ||
public class Cache implements Serializable{ | ||
/** | ||
* | ||
*/ | ||
private static final long serialVersionUID = 940890455361472998L; | ||
/** | ||
* Rubiks storage. | ||
* | ||
*/ | ||
private MyRubiksCube[][] cache; | ||
/** | ||
* Initializate storage. | ||
* @param maxcubes - max number of cubes a difficulty can have | ||
* @param diff - highest difficulty we will reach. | ||
* @param state | ||
*/ | ||
public Cache(int maxcubes, int diff, EvolutionState state) { | ||
this.init(maxcubes, diff, state); | ||
} | ||
public Cache(int maxcubes, int diff) { | ||
this.init(maxcubes, diff, null); | ||
} | ||
|
||
private void init(int maxcubes, int diff, EvolutionState state){ | ||
cache = new MyRubiksCube[diff][]; | ||
int posibilities = 12; | ||
int maxiters = 20000; | ||
for (int i = 0; i < cache.length; i++) { | ||
|
||
Vector<MyRubiksCube> auxvector = new Vector<MyRubiksCube>(); | ||
if(state!=null) | ||
state.output.message("Init Diff " + (i + 1)); | ||
if (posibilities <= maxcubes) { | ||
auxvector.addAll(generatePosibilities(i + 1)); | ||
} else { | ||
boolean exit = false; | ||
for (int j = 0; auxvector.size() < maxcubes && !exit; j++) { | ||
MyRubiksCube uno = new MyRubiksCube(); | ||
uno.desordenarCubo(i + 1); | ||
int z = 0; | ||
while (auxvector.contains(uno) && !exit) {// No quiero | ||
// repetidos. | ||
uno = new MyRubiksCube(); | ||
uno.desordenarCubo(i + 1); | ||
if (z > maxiters) { | ||
exit = true; | ||
} | ||
z++; | ||
} | ||
if (!exit) { | ||
auxvector.add(uno); | ||
} | ||
} | ||
} | ||
cache[i] = auxvector.toArray(new MyRubiksCube[0]); | ||
posibilities *= 6 * 2; | ||
} | ||
} | ||
private Vector<MyRubiksCube> generatePosibilities(int i) { | ||
return this.generatePosibilities(new MyRubiksCube(), 0, i); | ||
|
||
} | ||
private Vector<MyRubiksCube> generatePosibilities(MyRubiksCube cubo, | ||
int depth, int maxdepth) { | ||
Vector<MyRubiksCube> array = new Vector<MyRubiksCube>(); | ||
if (depth >= maxdepth) { | ||
array.add(cubo); | ||
} else { | ||
for (int i = 0; i < 6; i++) { | ||
MyRubiksCube clonClock = (MyRubiksCube) cubo.clone(); | ||
MyRubiksCube clonCClock = (MyRubiksCube) cubo.clone(); | ||
clonClock.twistSide(i, true); | ||
array.addAll(this.generatePosibilities(clonClock, | ||
depth + 1, maxdepth)); | ||
clonCClock.twistSide(i, false); | ||
array.addAll(this.generatePosibilities(clonCClock, | ||
depth + 1, maxdepth)); | ||
} | ||
} | ||
return array; | ||
} | ||
/** | ||
* Returns the number of cubes generated for the specified difficulty. | ||
* @param dif | ||
* @return number of cubes. | ||
*/ | ||
public int getNumCubos(int dif) { | ||
return this.cache[dif - 1].length; | ||
} | ||
/** | ||
* Returns a clone of a rubik's cube for the specified index and difficulty | ||
* @param index | ||
* @param diff | ||
* @return a rubik's cube | ||
*/ | ||
public MyRubiksCube getRubik(int index, int diff) { | ||
return (MyRubiksCube) this.cache[diff][index].clone(); | ||
} | ||
} |
Oops, something went wrong.