首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在编写一个2x2Rubik的多维数据集模拟器,但其中一个功能没有工作

我正在编写一个2x2Rubik的多维数据集模拟器,但其中一个功能没有工作
EN

Stack Overflow用户
提问于 2022-02-18 10:29:43
回答 1查看 96关注 0票数 0

因此,正如标题所述,我试图模拟2x2Rubik的立方体,但是r()函数似乎不起作用。我已经读过了,我找不到问题,有人知道如何解决这个问题吗?提前感谢!

立方体:

代码语言:javascript
复制
cube = {"front": ["w", "w", "w", "w"], "left": ["r", "r", "r", "r"], "right": ["o", "o", "o", "o"],
    "top": ["g", "g", "g", "g"], "bottom": ["b", "b", "b", "b"], "back": ["y", "y", "y", "y"]}

r()函数:

代码语言:javascript
复制
def r(repeats=0):
    new_front = [cube["front"][0], cube["bottom"][1], cube["front"][2], cube["bottom"][3]]
    new_top = [cube["top"][0], cube["front"][1], cube["top"][2], cube["front"][3]]
    new_back = [cube["back"][0], cube["top"][1], cube["back"][2], cube["top"][3]]
    new_bottom = [cube["bottom"][0], cube["back"][1], cube["bottom"][2], cube["back"][3]]

    new_right = [cube["right"][3], cube["right"][0], cube["right"][1], cube["right"][2]]
    cube["right"] = new_right

    cube["front"] = new_front
    cube["back"] = new_back
    cube["top"] = new_top
    cube["bottom"] = new_bottom

    if repeats - 1 > 0:
        r(repeats - 1)

u()函数:

代码语言:javascript
复制
def u(repeats=0):
    new_front = [cube["right"][0], cube["right"][1], cube["front"][2], cube["front"][3]]
    new_right = [cube["back"][0], cube["back"][1], cube["right"][2], cube["right"][3]]
    new_back = [cube["back"][0], cube["back"][1], cube["left"][2], cube["left"][3]]
    new_left = [cube["front"][0], cube["front"][1], cube["left"][2], cube["left"][3]]

    new_top = [cube["top"][3], cube["top"][0], cube["top"][1], cube["top"][2]]
    cube["top"] = new_top

    cube["front"] = new_front
    cube["right"] = new_right
    cube["back"] = new_back
    cube["left"] = new_left

    if repeats - 1 > 0:
        u(repeats - 1)

当我调用u()然后r()时的输出:

代码语言:javascript
复制
{'front': ['o', 'b', 'w', 'b'], 'left': ['w', 'w', 'r', 'r'], 'right': ['o', 'y', 'y', 'o'], 'top': ['g', 'o', 'g', 'w'], 'bottom': ['b', 'y', 'b', 'r'], 'back': ['y', 'g', 'r', 'g']}

正如您在库中所看到的,“右”面是不正确的。

编辑:我已经制作了一个应用程序来可视化正在发生的事情,下面是它的样子。看来问题就出在后面了。https://imgur.com/a/F2SvhXP

EN

回答 1

Stack Overflow用户

发布于 2022-02-19 07:07:08

我发现r()函数实际上就是问题所在,我已经修复了它。new_back变量是从顶部的错误侧面取来的。

以下是改进的功能:

代码语言:javascript
复制
def r(repeats=0):
    new_front = [cube["front"][0], cube["bottom"][1], cube["front"][2], cube["bottom"][3]]
    new_top = [cube["top"][0], cube["front"][1], cube["top"][2], cube["front"][3]]
    new_back = [cube["back"][0], cube["top"][1], cube["back"][2], cube["top"][3]]
    new_bottom = [cube["bottom"][0], cube["back"][1], cube["bottom"][2], cube["back"][3]]

    new_right = [cube["right"][3], cube["right"][0], cube["right"][1], cube["right"][2]]
    cube["right"] = new_right

    cube["front"] = new_front
    cube["back"] = new_back
    cube["top"] = new_top
    cube["bottom"] = new_bottom

    if repeats - 1 > 0:
        r(repeats - 1)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71171969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档