common starting seed is a good idea (as jdweng pointed out)
I just needed repair that typo as rookies might not know seen
should be seed
. This way both compared algorithms would have the same conditions.
nested for loops
not familiar with 7-Riffle Shuffle Algorithm but backtracking solver should have nested for loops. Right now you got single loop that goes 9 times (why?).
If you have N=7
turns shuffled cube than you need 7 nested for loops each iterating over all possible turns 3*3*2=18
. If the N
is changing you need dynamically nested for loops for more info see:
or maskable nested for loops up to gods number (20
).
Each for loop on each iteration turns previous loop state cube by selected movement and in last loop should be also detecting solved case and break if found.
So something like this (solver):
cube0=solved_cube();
for (cube1=cube0,i1=0; i1<18; i1++,cube1=turn_cube(cube0,i1))
for (cube2=cube1,i2=0; i2<18; i2++,cube2=turn_cube(cube1,i2))
...
for (cube7=cube6,i7=0; i7<18; i7++,cube7=turn_cube(cube1,i7))
if (cube7 == solved_cube()) return { i1,i2,...,i7 }; // solution found
return false; // unsolved
where turn_cube(cube a,int turn)
will return cube a
turned by turn
where turn
selects which slice is turned in which direction (which of the 18 possible turns) ...
Also this might interests you:
[Edit1] shuffler
As I mentioned I am not familiar with 7 riffle shuffle algo so if you just want to have a cube 7 turns from solved state then you're almost right. You should have single for
loop as you have but inside you need to make valid random moves something like this:
cube=solved_cube();
for (i=0; i<7; i++)
cube=turn_cube(cube,Random(18));
Now the real problem is to code the turn_cube function. To help with that we would need to know much more about how you represent your Rubik cube internally.
So is it 1D,2D or 3D array? What is the topology? What are the element values (HEX color may be or just 0..5
or some enum or transform matrix)?
In the link above is example of mine solver with source code to function void RubiCube::cube_rotate(int axis,int slice,double ang)
which is more or less what the cube_turn should do. There are 18 possible turns:
- 3 axises (
axis
)
- each axis has 3 slices (
slice
)
- and we can turn CW or CCW by 90 deg (
ang
beware mine ang is in radians and allows arbitarry angles as I animate the turns)
so you need to map those 18 cases into int turn = <0,17>
which will be processed in the cube_turn and applied to your cube...