所以,正如标题所说,我正在尝试模拟一个2x2魔方,但r((函数似乎不起作用。我已经通读了,但我找不到问题,有人知道如何解决这个问题吗?提前感谢!
立方体:
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((函数:
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((函数:
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((时的输出:
{'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://i.stack.imgur.com/Df8xE.jpg
我发现r()
函数实际上就是问题所在,我已经解决了它。new_back
变量取自顶面的错误一侧。
以下是改进后的功能:
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)