簡體   English   中英

無限循環-Rubiks立方體加密器

[英]Infinite loop - Rubiks cube scrambler

我在python中使用Rubiks Cube擾碼器遇到了一些問題。
有我的代碼:

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)

因此,當我運行程序時,他將

  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])

而且他正在循環播放...我嘗試使用“ i”而不是“ length-1”,但是它沒有用(索引超出范圍等)。

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)

例如,在第二個代碼中,我把“ i”插入了長度,並且當我的程序正在執行功能時,他告訴我“索引超出范圍”,我不知道為什么,如果i> = 2則不能超出范圍,因為“長度”(在函數中)== 1,2,3,依此類推,而“長度-1” == 0,1,2。 任何想法如何解決這個問題?

BTW。 例如,正確地爭奪Rubiks Cube:

R2 U2 R2 B' U2 B2 R2 F' U2 L' B2 F2 U' F2 R' B D R B R'
    if good_move(scramble, lenght - 1) == False:
        print ("I'm here")
        while (good_move(scramble, lenght - 1)) != False:

這是第一個問題。 在這里永遠不會輸入while循環,因為當您到達print行時, good_move肯定為false。 也許您的意思是每次都具有相同的條件。

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

        scramble.remove(lenght - 1)

這是第二個問題。 list.remove(x)不會從列表中刪除list[x] 無論在哪里,它都會在列表中搜索x的第一個實例並將其刪除。 如果要刪除列表的最后一個元素,可以將其切掉。

            scramble = scramble[:-1]

或刪除它。

            del scramble[-1]

現在您的程序應正確結束。 樣本結果:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM