Hey guys, Connor here.
I made a program that solves a Rubik's cube if you enter all the colors.
It works okay, but it takes so long. It takes 30 seconds to solve a 6
turn solution and about 10 minutes to solve a 7 move solution. I've seen
it done before in 8 moves in only 20 seconds. I was trying to look over
my code so that I could find what was making it slow down. I commented
out the part of the code that actually checks every solution, and it still
took a long time. This must mean that the part that is taking a while is
the part that simulates every combination of 'n' moves. Here is the code
that solves the cube.
If anyone can see a spot in my code that may be taking a long time, I
would be glad to hear your ideas or solutions.
Thanks,
Connor
I made a program that solves a Rubik's cube if you enter all the colors.
It works okay, but it takes so long. It takes 30 seconds to solve a 6
turn solution and about 10 minutes to solve a 7 move solution. I've seen
it done before in 8 moves in only 20 seconds. I was trying to look over
my code so that I could find what was making it slow down. I commented
out the part of the code that actually checks every solution, and it still
took a long time. This must mean that the part that is taking a while is
the part that simulates every combination of 'n' moves. Here is the code
that solves the cube.
public void solveIt() { int[] turns = new int[12]; int[] moves = new int[numOfTurns]; for(int i=0;i<12;i++) { turns[i] = i; } for(int i=0;i<numOfTurns;i++) { moves[i] = -1; } for(int i=0;i<numOfTurns;i++) { displayMoves[i] = "-"; } for(long w=0;w<Math.pow(12,numOfTurns);w++) { moves[0]++; for(int u=0;u<numOfTurns-1;u++) { if(moves[u]==12) { moves[u+1]++; moves[u]=0; } } bestCheck(moves); System.out.print(1.0*w/Math.pow(12,numOfTurns)); } for(int z=0;z<numOfTurns;z++) { if(bestMoves[z]==0) { displayMoves[z] = "F"; } else if(bestMoves[z]==1) { displayMoves[z] = "R"; } else if(bestMoves[z]==2) { displayMoves[z] = "U'"; } else if(bestMoves[z]==3) { displayMoves[z] = "L"; } else if(bestMoves[z]==4) { displayMoves[z] = "D'"; } else if(bestMoves[z]==5) { displayMoves[z] = "B"; } else if(bestMoves[z]==6) { displayMoves[z] = "F'"; } else if(bestMoves[z]==7) { displayMoves[z] = "R'"; } else if(bestMoves[z]==8) { displayMoves[z] = "U"; } else if(bestMoves[z]==9) { displayMoves[z] = "L'"; } else if(bestMoves[z]==10) { displayMoves[z] = "D"; } else if(bestMoves[z]==11) { displayMoves[z] = "B'"; } else { displayMoves[z] = "-"; } } System.out.print(displayMoves[0]); for(int q=1;q<numOfTurns;q++) { System.out.print(", " + displayMoves[q]); } System.out.println(); System.out.println("Solutions: " + solutions); System.out.println("BestCorrect: " + bestCorrect); for(int i:bestMoves) { if(i==0) mFace[0].rotateCW(); if(i==1) mFace[1].rotateCW(); if(i==2) mFace[2].rotateCW(); if(i==3) mFace[3].rotateCW(); if(i==4) mFace[4].rotateCW(); if(i==5) mFace[5].rotateCW(); if(i==6) { mFace[0].rotateCW(); mFace[0].rotateCW(); mFace[0].rotateCW(); } if(i==7) { mFace[1].rotateCW(); mFace[1].rotateCW(); mFace[1].rotateCW(); } if(i==8) { mFace[2].rotateCW(); mFace[2].rotateCW(); mFace[2].rotateCW(); } if(i==9) { mFace[3].rotateCW(); mFace[3].rotateCW(); mFace[3].rotateCW(); } if(i==10) { mFace[4].rotateCW(); mFace[4].rotateCW(); mFace[4].rotateCW(); } if(i==11) { mFace[5].rotateCW(); mFace[5].rotateCW(); mFace[5].rotateCW(); } } } public boolean bestCheck(int[] array) { for(int i:array) { if(i==0) mFace[0].rotateCW(); if(i==1) mFace[1].rotateCW(); if(i==2) mFace[2].rotateCW(); if(i==3) mFace[3].rotateCW(); if(i==4) mFace[4].rotateCW(); if(i==5) mFace[5].rotateCW(); if(i==6) { mFace[0].rotateCW(); mFace[0].rotateCW(); mFace[0].rotateCW(); } if(i==7) { mFace[1].rotateCW(); mFace[1].rotateCW(); mFace[1].rotateCW(); } if(i==8) { mFace[2].rotateCW(); mFace[2].rotateCW(); mFace[2].rotateCW(); } if(i==9) { mFace[3].rotateCW(); mFace[3].rotateCW(); mFace[3].rotateCW(); } if(i==10) { mFace[4].rotateCW(); mFace[4].rotateCW(); mFace[4].rotateCW(); } if(i==11) { mFace[5].rotateCW(); mFace[5].rotateCW(); mFace[5].rotateCW(); } } int num = correctCounter(); if(num>bestCorrect) { bestCorrect=num; for(int r=0;r<numOfTurns;r++) { bestMoves[r] = array[r]; } } for(int q=numOfTurns-1;q>=0;q--) { int i = array[q]; if(i==6) mFace[0].rotateCW(); if(i==7) mFace[1].rotateCW(); if(i==8) mFace[2].rotateCW(); if(i==9) mFace[3].rotateCW(); if(i==10) mFace[4].rotateCW(); if(i==11) mFace[5].rotateCW(); if(i==0) { mFace[0].rotateCW(); mFace[0].rotateCW(); mFace[0].rotateCW(); } if(i==1) { mFace[1].rotateCW(); mFace[1].rotateCW(); mFace[1].rotateCW(); } if(i==2) { mFace[2].rotateCW(); mFace[2].rotateCW(); mFace[2].rotateCW(); } if(i==3) { mFace[3].rotateCW(); mFace[3].rotateCW(); mFace[3].rotateCW(); } if(i==4) { mFace[4].rotateCW(); mFace[4].rotateCW(); mFace[4].rotateCW(); } if(i==5) { mFace[5].rotateCW(); mFace[5].rotateCW(); mFace[5].rotateCW(); } } if(bestCorrect==48) { return true; } else { return false; } } public int correctCounter() { int num = 0; solutions++; String pieceCheck = ""; String centerCheck = ""; for(int h=0;h<6;h++) { for(int j=0;j<8;j++) { pieceCheck = "" + mFace[h].getColor(j); centerCheck = "" + mFace[h].getCenterColor(); if(pieceCheck.equals(centerCheck)) { num++; } } } return num; }
If anyone can see a spot in my code that may be taking a long time, I
would be glad to hear your ideas or solutions.
Thanks,
Connor