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

2024-04-18 19:44:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了大约一个星期了,本以为我已经掌握了窍门,但我不能让这个工作。我需要编写一个迷宫解算器。迷宫应该从左边开始,到达右边,如果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()

Tags: orandtoboardlenifdefargument
2条回答

原始代码

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

两条建议:

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

  2. 考虑使用一个集合而不是几个if,因为它更短、更大 可靠的

    board[y][x+1] not in ('O', 'S', '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)):

1)如果您希望坐标仅在右侧坐标为“O”时更改,则!=“S”和!=“X”是多余的。你知道吗

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

相关问题 更多 >