0

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

  • Could you show how it looks after just u? "when I call u() then r()" is two steps already and you say only r doesn't work – h4z3 Feb 18 '22 at 10:32
  • Here is what it looks like after u(): {'front': ['o', 'o', 'w', 'w'], 'left': ['w', 'w', 'r', 'r'], 'right': ['y', 'y', 'o', 'o'], 'top': ['g', 'g', 'g', 'g'], 'bottom': ['b', 'b', 'b', 'b'], 'back': ['y', 'y', 'r', 'r']} – Atomic Peanut Feb 18 '22 at 10:33
  • From this `u` result I noticed you're addressing `back` side differently. Notice in your code you use back0 and back1 twice, and left2, left3 twice - this is not right. – h4z3 Feb 18 '22 at 10:37
  • I have now fixed this issue however this has not resolved the primary problem. – Atomic Peanut Feb 18 '22 at 10:49

1 Answers1

0

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)