Hi, I'm having some issues with a maze solver that I'm coding for an assignment. Here's what I have so far:

def main():
    maze = open(raw_input("Please enter the filename of the maze:"),'r')
    x=0
    y=0
    with maze as maze_file:
        maze = [list(line.strip()) for line in maze_file]


    for i in maze: #print blank maze
        for a in i:
            print a,
        print
    print
    findStart(maze,x,y)
    print maze[y][x]
    ##mazeSolve(maze,x,y) #call solver funcion

def findStart(maze,x,y):
    for x and y in range(0,len(maze)):
        if maze[y][x] == "S":
        print maze[y][x]
    return x,y

I do have the solver function coded already, but do not want to post it. My problem lies in the findStart function. I know how to find a single character in a string, but I have absolutely no clue how to do it with a list of lists. Other than that, the only thing I'm wondering is how to remove the spaces that are printed in between characters when I open my maze file.

Here's a sample maze to see the problem with. I threw it in a pastebin because the editor on here messed up the spacing.
http://paste.pocoo.org/show/155511/

I have absolutely no clue how to do it with a list of lists

test_matrix = [ ["A", "b", "c"], \
                ["R", "S", "s"], \
                ["X", "Y", "Z"] ]
for sublist in test_matrix:
   print sublist
   for ch in sublist:
      if ch == "S":
         print "\nS found in", sublist, "\n"

So once i've found the start, how would i store it in a way that I could index it as such:

maze[y][x]

to easily work through the maze by adding and subtracting from x and y

Member Avatar for masterofpuppets

hi,
using woooee's example, this becomes:

test_matrix = [ ["A", "b", "c"], \
                ["R", "S", "s"], \
                ["X", "Y", "Z"] ]

for x in range( len( test_matrix ) ):
   for y in range( len( test_matrix[ x ] ) ):
      if test_matrix[ x ][ y ] == "S":
         start = ( x, y )

# now you can use start here as a tuple.
print "\nS found in", start, "\n"
 
# output
# -> S found in (1 1)

hope this helps :)

Thanks for the help, but I managed to solve it using the enumerate function.

for rowIndex,row in enumerate(maze): #find y coord

        for colIndex,col in enumerate(row): #find x coord

            if col == "S":

                y = rowIndex #set y coord

                x = colIndex #set x coord
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.