I'm trying to code a 2x2 rubik's cube simulator but one of the functions isn't working

176 Views Asked by At

So, as the title says, I am trying to simulate a 2x2 Rubik's cube, however the r() function does not appear to be working. I have read through it and I can't find the problem, anyone know how to solve this? Thanks in advance!

The cube:

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"]}

The r() function:

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)

The u() function:

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)

The output when I call u() then 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']}

As you can see in the library, the "right" face is incorrect.

EDIT: I have made a app to visualize what is happening and here is what it looks like. It appears that the back face is the problem. https://i.stack.imgur.com/Df8xE.jpg

1

There are 1 best solutions below

0
Atomic Peanut On

I have found out that the r() function is in fact the problem and I have fixed it. The new_back variable was taking from the wrong side of the top face.

Here is the improved function:

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)