0

I have written a maze solver program. I want the PathFinder function to return True if a path is found and then print the path or else simply return False. But my program always keeps returning False even if a path is found. It would be great if you guys can help me figure this out.

def findPaths(m,path,i,j):
    r,c = len(m), len(m[0])
    if i == r-1 and j == c-1:
        print(path)
        return True

    #explore
    path.append(m[i][j])
    # move down
    if  i != r-1 and m[i+1][j] == '↓ ':
        findPaths(m,path,i+2,j)

    #move up
    if i < 0 and m[i-1][j] == '↑ ':
        findPaths(m,path,i-2,j)

    #move right
    if j != c-1 and m[i][j+1] == '→':
        findPaths(m,path,i,j+2)

    #move left
    if j > 0 and m[i][j-1] == '←':
        findPaths(m,path,i,j-2)

    path.pop()


def maze(r,c):
    m = []
    for i in range(r):
        row = []
        for j in range(1, c + (c)):
            rand_num = str(randint(1, 99))
            if j % 2 != 0:
                if len(rand_num) == 1:
                    row.append(' ' + rand_num)
                else:
                    row.append(rand_num)
            else:
                row.append('?')
        m.append(row)

    up_left_count = r * c // 3

    down_right_count = r * c * 2 // 3

    for v in range(r + (r - 1)):
        vertical_lst = []
        for each in range(1, c + c):
            if each % 2 == 0:
                vertical_lst.append(' ')
            else:
                vertical_lst.append('? ')
        if v % 2 != 0:
            m.insert(v, vertical_lst)

    idx_i = []
    idx_j = []

    idx_v_i = []
    idx_v_j = []

    for i in range(len(m)):
        for j in range(len(m[0])):
            if i % 2 == 0 and j % 2 != 0:
                idx_i.append(i)
                idx_j.append(j)

    for v_i in range(len(m)):
        for v_j in range(len(m[0])):
            if v_i % 2 != 0 and v_j % 2 == 0:
                idx_v_i.append(v_i)
                idx_v_j.append(v_j)

    idx_i = list(set(idx_i))
    idx_j = list(set(idx_j))

    idx_v_i = list(set(idx_v_i))
    idx_v_j = list(set(idx_v_j))

    for count in range(up_left_count):
        i_int = randint(0, len(idx_i) - 1)
        j_int = randint(0, len(idx_j) - 1)
        if m[i_int][j_int] != '←':
            m[idx_i[i_int]][idx_j[j_int]] = '←'

    for count_v in range(up_left_count):
        i_v_int = randint(0, len(idx_v_i) - 1)
        j_v_int = randint(0, len(idx_v_j) - 1)
        if m[i_v_int][j_v_int] != '↑':
            m[idx_v_i[i_v_int]][idx_v_j[j_v_int]] = '↑ '

    for i in range(len(m)):
        for j in range(len(m[0])):
            if i % 2 == 0 and j % 2 != 0:
                if m[i][j] == '?':
                    m[i][j] = '→'

    for i in range(len(m)):
        for j in range(len(m[0])):
            if i % 2 != 0 and j % 2 == 0:
                if m[i][j] != '↑ ':
                    m[i][j] = '↓ '

    m[0][0] = "S"
    m[-1][-1] = "D"

    for i in range(len(m)):
        for j in range(len(m[0])):
            print(m[i][j], end=" ")
        print()

    path = []

    return findPaths(m, path, 0,0)


if maze(5,6):
    print('True')
else:
    print('False')
martineau
  • 119,623
  • 25
  • 170
  • 301
David
  • 1
  • Your recursive function doesn't return the return value of its recursive calls. To pass the value back out of the recursion chain, the `findPaths(...)` calls must be turned into `return findPaths(...)` – Christoph Burschka Oct 21 '21 at 18:08
  • To follow up on Christoph's comment; your `findPaths(m,path,i+2,j)` line, for instance, *calls* the recursive step, but then drops the result since it's not assigning it to anything. You will have other problems once you resolve this, but that is your basic issue here. – Nathaniel Ford Oct 21 '21 at 18:11
  • Reply to Christoph: Did you mean I had to write a line of code that returns the function call. If so I did return the function call at the end of maze function. 'return findPaths(m, path, 0,0)', this is what you mean right? – David Oct 22 '21 at 04:09

0 Answers0