简体   繁体   English

C# 中用于魔方的 7-Riffle 随机播放算法

[英]A 7-Riffle Shuffle Algorithm in C# for a Rubik's cube

For a school assignment we need to implement a 7-Riffle Algorithm Method in C# which shuffles the faces of a Rubik's Cube.对于学校作业,我们需要在 C# 中实现 7-Riffle 算法方法,该方法对魔方的面进行洗牌。 Unfortunately there is not enough resources on the web that show how it should be coded.不幸的是,web 上没有足够的资源来显示它应该如何编码。 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.这段代码适用于洗牌位,但它所花费的时间似乎没有意义,因为它比 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?请问有什么帮助吗?

  1. common starting seed is a good idea (as jdweng pointed out)共同的起始种子是个好主意(正如jdweng指出的那样)

    I just needed repair that typo as rookies might not know seen should be seed .我只是需要修复那个错字,因为新手可能不知道seen的应该是seed This way both compared algorithms would have the same conditions.这样,两种比较算法将具有相同的条件。

  2. nested for loops嵌套for循环

    not familiar with 7-Riffle Shuffle Algorithm but backtracking solver should have nested for loops.不熟悉 7-Riffle Shuffle 算法,但回溯求解器应该嵌套了 for 循环。 Right now you got single loop that goes 9 times (why?).现在你有一个循环 9 次(为什么?)。

    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 .如果您有N=7转洗牌立方体,则需要 7 个嵌套的 for 循环,每个循环遍历所有可能的转3*3*2=18 If the N is changing you need dynamically nested for loops for more info see:如果N发生变化,您需要动态嵌套 for 循环以获取更多信息,请参阅:

    or maskable nested for loops up to gods number ( 20 ) .或可屏蔽的嵌套 for 循环,最多为神数 ( 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.每次迭代中的每个 for 循环都会通过选定的移动转动前一个循环 state 立方体,并且在最后一个循环中还应该检测已解决的情况,如果发现则中断。

    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)...其中turn_cube(cube a,int turn)将返回 cube a turn by turn where turn选择哪个切片朝哪个方向转动(18 个可能的转弯中的哪一个)...

Also this might interests you:您也可能对此感兴趣:

[Edit1] shuffler [Edit1] 洗牌器

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.正如我提到的,我不熟悉 7 riffle shuffle 算法,所以如果你只想从已解决的 state 得到一个 7 圈的立方体,那么你几乎是对的。 You should have single for loop as you have but inside you need to make valid random moves something like this:你应该有一个for循环,但在里面你需要进行有效的随机移动,如下所示:

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.现在真正的问题是编写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?那么它是一维、二维还是 3D 阵列? What is the topology?什么是拓扑? What are the element values (HEX color may be or just 0..5 or some enum or transform matrix)?元素值是什么(十六进制颜色可能是或只是0..5或一些枚举或变换矩阵)?

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.在上面的链接中是我的解算器示例,其源代码为 function void RubiCube::cube_rotate(int axis,int slice,double ang)这或多或少是 cube_turn 应该做的。 There are 18 possible turns:有 18 种可能的转弯:

  • 3 axises ( axis ) 3轴( axis
  • each axis has 3 slices ( slice )每个轴有 3 个切片( 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)我们可以将 CW 或 CCW 转动 90 度(请注意我的ang是弧度,并且在我为转弯设置动画时允许任意角度)

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...所以您需要将这 18 个案例 map 转换为int turn = <0,17> ,这将在 cube_turn 中处理并应用于您的多维数据集...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM