pythonloopsrubiks-cube

Infinite loop - Rubiks cube scrambler


I'm having a little problem with Rubiks Cube scrambler in python.
There is my code:

from random import randint


moves = ["F", "F'", "R", "R'", "L", "L'", "U", "U'", "D", "D'", "B", "B'", "F2", "R2", "L2", "U2", "D2", "B2"]
scramble = []

lenght = len(scramble)
lenght_moves = len(moves) - 1

def good_move(scramble, lenght):
    if scramble[lenght]  ==  "R" or scramble[lenght] == "R'" or scramble[lenght] == "R2":
        if scramble[lenght - 1]  ==  "R" or scramble[lenght - 1] == "R'" or scramble[lenght - 1] == "R2":
            return False
    if scramble[lenght]  ==  "L" or scramble[lenght] == "L'" or scramble[lenght] == "L2":
        if scramble[lenght - 1]  ==  "L" or scramble[lenght - 1] == "L'" or scramble[lenght - 1] == "L2":
            return False
    if scramble[lenght]  ==  "F" or scramble[lenght] == "F'" or scramble[lenght] == "F2":
        if scramble[lenght - 1]  ==  "F" or scramble[lenght - 1] == "F'" or scramble[lenght - 1] == "F2":
            return False
    if scramble[lenght]  ==  "U" or scramble[lenght] == "U'" or scramble[lenght] == "U2":
        if scramble[lenght - 1]  ==  "U" or scramble[lenght - 1] == "U'" or scramble[lenght - 1] == "U2":
            return False
    if scramble[lenght]  ==  "D" or scramble[lenght] == "D'" or scramble[lenght] == "D2":
        if scramble[lenght - 1]  ==  "D" or scramble[lenght - 1] == "D'" or scramble[lenght - 1] == "D2":
            return False
    if scramble[lenght]  ==  "B" or scramble[lenght] == "B'" or scramble[lenght] == "B2":
        if scramble[lenght - 1]  ==  "B" or scramble[lenght - 1] == "B'" or scramble[lenght - 1] == "B2":
            return False

    return True





while (lenght < 20):
    print (lenght)
    print (scramble)
    random = randint(0, lenght_moves)
    if lenght - 1 >= 1:
        if good_move(scramble, lenght - 1) == False:
            print ("I'm here")
            while (good_move(scramble, lenght - 1)) != False:
                random = randint(0, lenght_moves)
                print (random)
                scramble.remove(lenght - 1)
                scramble.append(moves[random])
        else:
            scramble.append(moves[random])

    else:
        scramble.append(moves[random])

    lenght = len(scramble)

print (scramble)

So, when I'm running my program, he is going to

  if lenght - 1 >= 1:
    if good_move(scramble, lenght - 1) == False:
        print ("I'm here")
        while (good_move(scramble, lenght - 1)) != False:
            random = randint(0, lenght_moves)
            print (random)
            scramble.remove(lenght - 1)
            scramble.append(moves[random])

And he is looping up... I tried with "i" instead of "length - 1" but it didn't work (index out of range etc.).

moves = ["F", "F'", "R", "R'", "L", "L'", "U", "U'", "D", "D'", "B", "B'", "F2", "R2", "L2", "U2", "D2", "B2"]
scramble = []

length = len(scramble)
length_moves = len(moves) - 1

def good_move(scramble, length):
    if scramble[length]  ==  "R" or scramble[length] == "R'" or scramble[length] == "R2":
         if scramble[length - 1]  ==  "R" or scramble[length - 1] == "R'" or      scramble[length - 1] == "R2":
        return False
     if scramble[length]  ==  "L" or scramble[length] == "L'" or scramble[length] == "L2":
    if scramble[length - 1]  ==  "L" or scramble[length - 1] == "L'" or      scramble[length - 1] == "L2":
        return False
if scramble[length]  ==  "F" or scramble[length] == "F'" or scramble[length] == "F2":
    if scramble[length - 1]  ==  "F" or scramble[length - 1] == "F'" or scramble[length - 1] == "F2":
        return False
if scramble[length]  ==  "U" or scramble[length] == "U'" or scramble[length] == "U2":
    if scramble[length - 1]  ==  "U" or scramble[length - 1] == "U'" or scramble[length - 1] == "U2":
        return False
if scramble[length]  ==  "D" or scramble[length] == "D'" or scramble[length] == "D2":
    if scramble[length - 1]  ==  "D" or scramble[length - 1] == "D'" or scramble[length - 1] == "D2":
        return False
if scramble[length]  ==  "B" or scramble[length] == "B'" or scramble[length] == "B2":
    if scramble[length - 1]  ==  "B" or scramble[length - 1] == "B'" or scramble[length - 1] == "B2":
        return False

return True




i = 0
while (i < 20):
    print (length)
    print (scramble)
    random = randint(0, length_moves)
    if i >= 2:
        if good_move(scramble, i) == False:
            print ("I'm here")
            while (good_move(scramble, i)) != False:
                random = randint(0, length_moves)
                print (random)
                scramble.remove(i)
                scramble.append(moves[random])
        else:
            scramble.append(moves[random])

    else:
        scramble.append(moves[random])

    i += 1

print (scramble)

For example, in second code i put "i" insted of lenght and when my program is meeting function, he is telling that "index is out of range" I don't know why, if i >= 2 it can't be out of range because "lenght"(in function) == 1,2,3 and so on and "lenght - 1" == 0,1,2. Any ideas how to solve this problem?

BTW. For example correct scramble to Rubiks Cube:

R2 U2 R2 B' U2 B2 R2 F' U2 L' B2 F2 U' F2 R' B D R B R'

Solution

  •     if good_move(scramble, lenght - 1) == False:
            print ("I'm here")
            while (good_move(scramble, lenght - 1)) != False:
    

    This is the first problem. The while loop will never be entered here, because good_move will certainly be false when you reach the print line. Perhaps you meant to have the same condition each time.

        if good_move(scramble, lenght - 1) == False:
            print ("I'm here")
            while (good_move(scramble, lenght - 1)) == False:
    

            scramble.remove(lenght - 1)
    

    This is the second problem. list.remove(x) does not remove list[x] from the list. It searches through the list for the first instance of x and removes it, no matter where it is. If you want to remove the last element of the list, you can slice it off.

                scramble = scramble[:-1]
    

    Or delete it.

                del scramble[-1]
    

    Now your program should end properly. Sample result:

    ["F'", 'D', 'B', 'D', 'B2', "U'", 'R2', 'L2', "D'", 'B2', 'F', "R'", 'B2', 'R', "F'", "R'", "B'", 'U2', 'F', 'L2']