Python:尝试创建一个随机迷宫解算器,并优先选择新路径

Python:尝试创建一个随机迷宫解算器,并优先选择新路径,python,random,infinite-loop,maze,Python,Random,Infinite Loop,Maze,我已经编写了大约一周的代码,本来以为我已经掌握了诀窍,但我无法让这一个工作。我需要编写一个迷宫解算器。迷宫应该从左边开始,到达右边,如果x和y中有一个是奇数,我只能向右移动,如果x和y中没有一个是奇数,我只能向左移动。这是我到目前为止所拥有的。当我到达这条线路时,我被卡住了(我想是最后一个电话的21号线) elif(board[y][x+1]=“O”)和(board[y][x+1]!=“S”)和(board[y][x+1]!=“x”)和((y%2==0或y==0)和x%2!=0)或(y%2!=0

我已经编写了大约一周的代码,本来以为我已经掌握了诀窍,但我无法让这一个工作。我需要编写一个迷宫解算器。迷宫应该从左边开始,到达右边,如果x和y中有一个是奇数,我只能向右移动,如果x和y中没有一个是奇数,我只能向左移动。这是我到目前为止所拥有的。当我到达这条线路时,我被卡住了(我想是最后一个电话的21号线)

elif(board[y][x+1]=“O”)和(board[y][x+1]!=“S”)和(board[y][x+1]!=“x”)和((y%2==0或y==0)和x%2!=0)或(y%2!=0和(x%2==0或x==0)):

我看到了
“参数0-R到O可能”参数0-R到O可能“
,即使右边的点是“X”或“S”。它只是重复输出
“参数0-R到O可能”
。有人能帮我解决这个问题吗

from random import randint
length_of_board = int(raw_input("Please enter the length of the board: "))
10
board = []
for i in range(length_of_board):
    board.append(["O"]*length_of_board)

def print_board(board):
    for row in board:
        print " ".join(row)


def placement_of_blocks():
    number_block = int(float(raw_input("p ="))*(int(len(board)-3)**2))
    print number_block
    while number_block > 0:
        rand_row = randint(1, len(board) -2)
        rand_col = randint(1, len(board) -2)
        if (board[rand_row][rand_col] != "X"):
            board[rand_row][rand_col] = "X"
            number_block = number_block - 1
        else:
            continue

placement_of_blocks()
0.30

def borders():
    bottom = 0
    top = 0
    while bottom < (int(len(board)-1)):
        board[int(len(board)-1)][bottom] = "X"
        bottom = bottom + 1
    while top < (int(len(board)-1)):
        board[0][top] = "X"
        top = top + 1


def ending_coordinates():
    col_goal = 0
    while col_goal < (int(len(board)-1)):
        board[col_goal][int(len(board)-1)] = "G"
        col_goal = col_goal + 1


def randomizer_a():
    return randint(0,3)


def randomizer_b():
    return randint(0,2)


def randomizer_c():
    return randint(0,1)


def solve_maze():
    borders()
    ending_coordinates()
    nuf = 1
    while nuf > 0:
        y = randint(0, len(board)-1)
        x = 0
        if (board[y][x] != "X"):
            board[y][x] = "S"
            nuf = nuf - 1
        else:
            continue
    i = 100
    while i > 0:
        i = i - 1
        if x == int(len(board)-2):
            print_board(board)
            print "This maze has a solution"
            break
        # This code gives preference for new spaces
        # R and L cannot occur on the same space
        elif (board[y][x+1] == "O") and (board[y][x+1] != "S") and (board[y][x+1] != "X") and ((y%2 == 0 or y == 0) and x%2 != 0) or (y%2 != 0 and (x%2 == 0 or  x == 0)):
            print "argument 0 - R to O possible"
            if board[y+1][x] == "O" and y < int(len(board)-2):
                if board[y-1][x] == "O" and y > 0:
                    # if R, D, U = "O"
                    if randomizer_b() == 0:
                        print "argument 1 - all directions to O possible, R"
                        x = x + 1
                        board[y][x] = "S"
                    else:
                        if randomizer_c() == 0:
                            print "argument 3 - all directions to O possible, D"
                            y = y +1
                            board[y][x] = "S"
                        else:
                            print "argument 4 - all directions to O possible, U"
                            y = y - 1
                            board[y][x] = "S"
                    # if R, D = "O"
                else:
                    if randomizer_c() == 0:
                        print "argument 5 - R D to O possible, R"
                        x = x + 1
                        board[y][x] = "S"
                    else:
                        print "argument 7 - R D to O possible, D"
                        y = y +1
                        board[y][x] = "S"
            # if R, U = "O"
            elif board[y-1][x] == "O" and y > 0:
                print "argument 14 - R U to O possible"
                if randomizer_c() == 0 and board[y][x+1] != "X":
                    print "argument 14.25 - R"
                    x = x + 1
                    board[y][x] = "S"
                elif board[y-1][x] != "X" and board[y-1][x] != "S":
                    print "argument 14.5 - U"
                    y = y - 1
                    board[y][x] = "S"
            # if R = "O"
           elif board[y][x+1] != "X" and board[y][x+1] == "O" and board[y][x+1] != "S":
                print "argument 15 - R to O possible, R"
                x = x + 1
                board[y][x] = "S"
        elif board[y+1][x] == "O" and y < int(len(board)-2):
            if (board[y][x-1] == "O") and (x != 0) and (board[y][x-1] != "X") and (board[y][x-1] != "S") and (y%2 == 0 or y == 0) and (x%2 == 0 or x == 0) or (y%2 != 0 and x%2 != 0):
                if board[y-1][x] == "O" and y > 0:
                    # If D, L U = "O"
                    print "argument 16 - D L U to O possible"
                    if (randomizer_b() == 0 and board[y][x-1] == "O" and board[y][x-1] != "S"):
                        print "argument 16.25 - L"
                        x = x - 1
                        board[y][x] = "S"
                    else:
                        if randomizer_c() == 0 and board[y+1][x] != "X":
                            print "argument 16.5 - D"
                            y = y +1
                            board[y][x] = "S"
                        else:
                            print "argument 16.75 - U"
                            y = y - 1
                            board[y][x] = "S"
                # If D, L = "O"
                else:
                    print "argument 16b - D L to O possible"
                    if randomizer_c() == 0:
                        print "argument 16b.25 - L"
                        x = x - 1
                        board[y][x] = "S"
                    else:
                        print "argument 16b.5 - D"
                        y = y +1
                        board[y][x] = "S"
            # If D, U = "O"
            elif board[y-1][x] == "O" and y > 0:
                print "argument 17 - D U to O possible"
                if randomizer_c() == 0:
                    print "argument 17.25 - D"
                    y = y +1
                    board[y][x] = "S"
                else:
                    print "argument 17.5 - U"
                    y = y - 1
                    board[y][x] = "S"
            # If D = "O"
            else:
                print "argument 17b - D to O possible, D"
                y = y +1
                board[y][x] = "S"
        elif (board[y][x-1] == "O" and x != 0 and (y%2 == 0 or y == 0) and (x%2 == 0 or x == 0) or (y%2 != 0 and x%2 != 0) and board[y][x-1] != "S" and board[y][x-1] != "X"):
            if (board[y-1][x] == "O" and y > 0 and board[y][x-1] != "S" and board[y][x-1] != "X" and board[y-1][x] != "X" and board[y-1][x] != "S"):
                # If L, U = "O"
                if randomizer_c() == 0:
                    print "argument 18.25 - L U to O possible, L"
                    x = x - 1
                    board[y][x] = "S"
                else:
                    print "argument 18.5 - L U to O possible, U"
                    y = y - 1
                    board[y][x] = "S"
            # If L = "O"
            else:
                if board[y][x-1] != "S" and board[y][x-1] != "X":
                    print "argument 18.75 - L to O possible, L"
                    x = x - 1
                    board[y][x] = "S"
        # If U = "O"
        elif board[y-1][x] == "O" and y > 0:
            print "argument 19 - U to O possible, U"
            y = y - 1
            board[y][x] = "S"
        # This is the end of the preference coding
        # If you can and a = 0, move forward
        elif True:
            if randomizer_a() == 0 and ((y%2 == 0 or y == 0) and x%2 != 0) or (y%2 != 0 and (x%2 == 0 or  x == 0)) and (board[y][x+1] != "X"):
                print "argument 20"
                if ((y%2 == 0 or y == 0) and x%2 != 0) or (y%2 != 0 and (x%2 == 0 or  x == 0)) and (board[y][x+1] != "X"):
                    x = x + 1
                    board[y][x] = "S"
            else:
                if (randomizer_b() == 0 and ((y%2 == 0 or y == 0) and (x%2 == 0 or x == 0) or (y%2 != 0 and x%2 != 0)) and (board[y][x-1] != "X")  and x != 0):
                # If you can and b = 0, move back
                    print "argument 21"
                    x = x - 1
                    board[y][x] = "S"
                # If you can and a = 2, move down
                else:
                    if randomizer_c() == 0 and (board[y+1][x] != "X") and (y < int(len(board)-2)):
                        print "argument 22"
                        y = y +1
                        board[y][x] = "S"
                     # If you can and a = 3, move up
                     else:
                        if (board[y-1][x] != "X") and (y > 0): 
                            print "argument 23"
                            y = y - 1
                            board[y][x] = "S"
        # If you can't move at all
        else:
            print_board(board)
            print "No solution"
            break
        print i
        print_board(board)
    if i == 0:
        print "No solution found"   

solve_maze()
来自随机导入randint
电路板的长度=int(原始输入(“请输入电路板的长度:”)
10
董事会=[]
对于范围内的i(板的长度):
板。追加([“O”]*板的长度)
def打印板(板):
对于板中的行:
打印“”连接(行)
定义块的位置()
数字块=整数(浮点(原始输入(“p=”)*(整数(长(板)-3)**2))
打印数字块
当数字块>0时:
rand_row=randint(1,列(板)-2)
rand_col=randint(1,len(板)-2)
如果(板[rand_row][rand_col]!=“X”):
板[rand_row][rand_col]=“X”
数字块=数字块-1
其他:
持续
_块的放置_()
0.30
def borders():
底部=0
top=0
而底部<(内部(透镜(板)-1)):
板[int(len(board)-1)][底部]=“X”
底部=底部+1
当顶部<(内部(长(板)-1)时:
板[0][顶部]=“X”
顶部=顶部+1
def ending_坐标():
col_目标=0
而col_目标<(内部(董事会)-1)):
董事会[col_goal][int(董事会-1)]=“G”
col_目标=col_目标+1
def随机化器_a():
返回randint(0,3)
def随机化器_b():
返回randint(0,2)
def随机化器_c():
返回randint(0,1)
def solve_maze():
边界()
结束_坐标()
nuf=1
当nuf>0时:
y=randint(0,长(板)-1)
x=0
如果(板[y][x]!=“x”):
板[y][x]=“S”
nuf=nuf-1
其他:
持续
i=100
当i>0时:
i=i-1
如果x==int(len(board)-2):
印刷电路板
打印“此迷宫有解决方案”
打破
#此代码优先选择新空格
#R和L不能出现在同一个空间上
elif(board[y][x+1]=“O”)和(board[y][x+1]!=“S”)和(board[y][x+1]!=“x”)和((y%2==0或y==0)和x%2!=0)或(y%2!=0和(x%2==0或x==0)):
打印“参数0-R到O可能”
如果电路板[y+1][x]=“O”和y0:
#如果R,D,U=“O”
如果随机化器_b()==0:
打印“参数1-O可能的所有方向,R”
x=x+1
板[y][x]=“S”
其他:
如果随机化器_c()==0:
打印“参数3-O可能的所有方向,D”
y=y+1
板[y][x]=“S”
其他:
打印“参数4-所有指向O可能,U的方向”
y=y-1
板[y][x]=“S”
#如果R,D=“O”
其他:
如果随机化器_c()==0:
打印“参数5-R D到O可能,R”
x=x+1
板[y][x]=“S”
其他:
打印“参数7-R D到O可能,D”
y=y+1
板[y][x]=“S”
#如果R,U=“O”
elif板[y-1][x]=“O”和y>0:
打印“参数14-RU到O可能”
如果随机化器_c()==0且电路板[y][x+1]!=“x”:
打印“参数14.25-R”
x=x+1
板[y][x]=“S”
elif板[y-1][x]!=“x”和板[y-1][x]!=“S”:
打印“参数14.5-U”
y=y-1
板[y][x]=“S”
#如果R=“O”
elif板[y][x+1]!=“x”和板[y][x+1]=“O”和板[y][x+1]!=“S”:
打印“参数15-R到O可能,R”
x=x+1
板[y][x]=“S”
elif板[y+1][x]=“O”和y0:
#如果D,L U=“O”
打印“参数16-D L U到O可能”
如果(随机化器_b()==0和板[y][x-1]==“O”和板[y][x-1]!=“S”):
打印“参数16.25-L”
x=x-1
板[y][x]=“S”
其他:
如果随机化器_c()==0且电路板[y+1][x]!=“x”:
打印“参数16.5-D”
y=y+1
板[y][x]=“S”
其他:
打印“参数16.75-U”
y=y-1
板[y][x]=“S”
#如果D,L=“O”
其他:
“打印”参数
(board[y][x+1] == "O") and (board[y][x+1] != "S") and (board[y][x+1] != "X")
elif (board[y][x+1] == "O") and (board[y][x+1] != "S") and (board[y][x+1] != "X") and ((y%2 == 0 or y == 0) and x%2 != 0) or (y%2 != 0 and (x%2 == 0 or  x == 0)):
elif (board[y][x+1] == "O" and (((y%2 == 0 or y == 0) and x%2 != 0) or (y%2 != 0 and (x%2 == 0 or  x == 0))):