如何以纪念迷宫解决途径:处理“IF”递归条件条件“==真”

时间:2019-02-01 23:46:02

标签: python recursion maze

我正在尝试复制此解决方案: Maze solving with python

我唯一的目标是使用胡萝卜显示迷宫中溶液路径的方向。我已经解决了这个迷宫,但道路是由一个字符(2)的。我想用适当的胡萝卜来代替它('>','v','<','^')

我的问题在于:

if move(x + 1, y) == True:

由于我将递归函数作为IF条件的一部分进行调用,因此它一直循环执行而未确认“ == True”条件-因此它不会返回“ True”语句,也不会忽略我的条件以替换为适当的胡萝卜。

如何强制它处理整个IF条件,包括“ == True”。

示例输出: 注意:在IF条件下,它会跳过打印语句(例如:“ EAST IS TRUE”)。但是-它正在识别else语句(“ EAST NOT TRUE”)。

LOOP START:  X: 54 Y: 25
LOOP START:  X: 55 Y: 25
LOOP START:  X: 56 Y: 25
LOOP START:  X: 57 Y: 25
LOOP START:  X: 58 Y: 25
LOOP START:  X: 59 Y: 25
LOOP START:  X: 60 Y: 25
That is wall X: 60 Y: 25
EAST NOT TRUE X: 59 Y: 25
LOOP START:  X: 59 Y: 26
LOOP START:  X: 60 Y: 26
That is wall X: 60 Y: 26
...etc

我的代码:

def move(x, y):
    global FoundWayOut
    if FoundWayOut:
        return

    print('LOOP START: ' + ' X: ' + str(x) + ' Y: ' + str(y))


    if (x < 0 or x > width - 1 or y < 0 or y > height - 1):
        print('Out of bounds' + ' X: ' + str(x) + ' Y: ' + str(y)); return
    if (myMaze[y][x] == 7):
        FoundWayOut = True
        print('Got it!' + ' X: ' + str(x) + ' Y: ' + str(y)); return
    if (myMaze[y][x] == 1):
        print('That is wall' + ' X: ' + str(x) + ' Y: ' + str(y)); return
    if (myMaze[y][x] == 2):
        print('Was here before' + ' X: ' + str(x) + ' Y: ' + str(y)); return
    myMaze[y][x] = 2

    if move(x + 1, y):
        myMaze[y][x] = ">"
        print("EAST IS TRUE")
    else:
        print("EAST NOT TRUE" + " X: " + str(x) + " Y: " + str(y))

    if move(x, y + 1):
        myMaze[y][x] = "v"
        print("SOUTH IS TRUE")
    else:
        print("SOUTH NOT TRUE" + " X: " + str(x) + " Y: " + str(y))

    if move(x - 1, y):
        myMaze[y][x] = "<"
        print("WEST IS TRUE")
    else:
        print("WEST NOT TRUE" + " X: " + str(x) + " Y: " + str(y))

    if move(x, y - 1):
        myMaze[y][x] = "^"
        print("NORTH IS TRUE")
    else:
        print("NORTH NOT TRUE" + " X: " + str(x) + " Y: " + str(y))

如果条件为真,我期望的结果将是替换字符。

0 个答案:

没有答案