Python:尝试制作一个优先考虑新路径的随机迷宫求解器

时间:2014-05-27 13:35:03

标签: python random infinite-loop maze

我现在已经编写了大约一个星期,并且认为我已经掌握了它,但是我无法让这个工作。我需要编写一个迷宫求解器。迷宫应该从左边开始到达右边,如果x和y中只有一个是奇数,我只能向右移动,如果x和y中只有一个是奇数,我只能向左移动。这是我到目前为止所拥有的。当我到达这一行时,我会卡住(我认为,最后一次通话的第21行)

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

我看到"argument 0 - R to O possible "argument 0 - R to O possible",即使右边的位置是" X"或者" S"。它只是重复输出"argument 0 - R to O possible"。任何人都可以帮我解决这个问题吗?

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

2 个答案:

答案 0 :(得分:0)

原始代码

(board[y][x+1] == "O") and (board[y][x+1] != "S") and (board[y][x+1] != "X")

两个建议:

  1. 第一个==可能应该是!=

  2. 考虑使用集合与几个ifs - 它更短,更多 可靠

    board[y][x+1] not in ('O', 'S', 'X')

答案 1 :(得分:0)

你很亲密。我认为要改进的行有两件事,您可能希望在整个代码中进行这些更改:

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

1)如果你想要坐标改变只有当右边的坐标是&#34; O&#34;那么!=&#34; S&#34;和!=&#34; X&#34;是多余的。

2)你需要在该行的最后一位围绕另一组括号,以便满足所有条件。

应该是:

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