0

For a school assignment we need to implement a 7-Riffle Algorithm Method in C# which shuffles the faces of a Rubik's Cube. Unfortunately there is not enough resources on the web that show how it should be coded. I implemented the Stopwatch already to calculate the elapsed ticks it takes for different Rubik's cube sizes.

This code works for the shuffling bit, but the time it takes doesn't seem to make sense as it is faster than that of Fisher Yates.

        Random rand = new Random();

        for (int i = rubikCubeArray.Length - 1; i > 7; i--)
        {
            int n = rand.Next(i + 1);
            int temp = rubikCubeArray[i];
            rubikCubeArray[i] = rubikCubeArray[n];
            rubikCubeArray[n] = temp;
        }

Any help please?

Mandy
  • 1
  • 5
  • Are you running both algorithms on the same machine? For comparison I would put a fixed seen into new Random(123) so you get accurate comparisons. Or perform algorithm 100 times and get average. – jdweng Apr 08 '20 at 13:53
  • mm don't know if I understood you correctly. In the program I have this code which is repeated for different Rubik sizes ex 5x5, 10x10 ... but the repetitions stay the same as 1000 TimeTaken = TimeShufflingAlgorithm(new RiffleShuffle(), 2, repetitions); //repetitions = 1000 Console.WriteLine("Time taken to Riffle Shuffle a 2 x 2 Rubik Cube : " + TimeTaken); And yes i have both the FisherYates and the Riffle together – Mandy Apr 08 '20 at 14:16
  • 1
    Your for loop is wrong. If length is 9 you loop only goes through one loop instead of 9. – jdweng Apr 08 '20 at 17:58

1 Answers1

0
  1. 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.

  2. 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...

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • 1
    Hey Spektre. I don't really understand your code sorry :/ Are you solving the cube like that, or shuffling? Basically I need a for loop which repeats 7 times, hence the 7 riffle shuffle, to SHUFFLE the cube, not solve it. Is there a common algorithm for a riffle shuffle in general? – Mandy Apr 09 '20 at 10:47
  • @Mandy heh that is the backtracking solver ... I added edit1 with the shuffler example. – Spektre Apr 09 '20 at 12:36