hey yall

I've been working on this python program for a while and I need some help please. This program consists of classes and functions to make a basic maze. I'm not using Tkinter or any gui's. Just a printout where python does all the calculating. I have three files, main.py, maze.py, and path.py. User inputs dimensions...then follow the menu. I can print the maze, when I try to solve it, the program prints an endless loop of errors. Can you help fix it because I can't find it.

This is the code where I can't 'solve' the maze, bc python would be reading these functions. (i think)

def findPath(self): # start the recursion
                    return self.rfindPath(self.start[0],self.start[1])
        
          def rfindPath(self,i,j):
                    if(i < 0 or i > self.m or j < 0 or j > self.n):
                              return False
                    elif(self.theMaze[i][j] == ' X' or self.theMaze[i][j] ==' v' or self.theMaze[i][j] == ' *'):
                              if(self.theMaze[i][j] == ' *'):
                                        self.theMaze[i][j] == ' v'
                                        self.thePath.pop()       
                              return False

          def removeVs(self):
                    for i in range(0, self.m):
                              for j in range(0, self.n):
                                        if self.theMaze[i][j] == ' v':
                                                  self.theMaze[i][j] = ' O'
                    return

the attachments are .txt. rename them to .py, save them in the same folder together, and run main.py to try it out.

THANK YOU FOR YOUR HELP! :)

Attachments
#file: main.py main function for pr1, first programming project.
# mcs275, spring 2009, lowman
import sys
from random import randrange
import maze
from maze import *
def printMenu():
          print "=========================="
          print "enter number for choice"
          print "0: quit"
          print "1: new maze"
          print "2: print maze"
          print "3: solve maze"
          print "4: remove v's"
          print "=========================="
    
def quit():
          # This function should cause the game to quit.
          sys.exit(1)
          return
    
def newMaze():
          # This function will be called when a person starts with chioce.
          m,n = input("Enter maze dimensions separated by a comma. m,n: ")
          xfactor = input("Enter xfactor (fraction of maze with X's): ")
          # The following testing statements will determine how to respond to what the user types in.
          while True :
                    choice = input("Type 1 for: standard start/finish, or 2 for: random start/finish: ")
                    if choice == 1 :
                              # functions for a standard start/finish
                              maze = Maze(m,n,(0,0),(m-1,n-1),xfactor)
                    elif choice == 2 :
                              # this function is to print the maze
                              start = (randrange(m),randrange(n))
                              finish = (randrange(m),randrange(n))
                              maze = Maze(m,n,start,finish,xfactor)
                    else:
                              # this is when user types in something other than 1 or 2
                              print "bad choice"
                              continue
                    break
          return maze # when this function is done, it returns to the main menu.

def main():
          # This is the tester function that calls all of the functions for the maze to work.
          while True :
                    printMenu()
                    choice = input("Enter choice: ")
                    if choice == 0 :
                              quit()
                    elif choice == 1 :
                              m1 = newMaze()
                    elif choice == 2 :
                              m1.printMaze()
                    elif choice == 3 :
                              m1.findPath()
                    elif choice == 4 :
                              m1.removeVs()
                    else:
                              print "Nothing to do!"
          return
# end def main()

if __name__ == "__main__":
          main()
#file: maze.py partial code for part one
# mcs275, spring 2009, lowman, Fatima N Mohammed
import path
from random import randrange
class Maze:
          def __init__( self, m=5, n=5, start=(0,0) ,finish=(4,4), xFactor=.4 ):
                    #This is the constructor for the maze class. The above values are defaults for those arguments.
                    self.m=m
                    self.n=n
                    self.start = start
                    self.finish = finish
                    self.xFactor = xFactor
                    self.makeMaze()
                    return
          
          def makeMaze(self):
                    # 'O' => Open, 'X' => wall
                    # This function will define the basic outline of the maze and how it looks when it is drawn.
                    self.theMaze = [['  O' for j in range(0,self.n)] for i in range(0,self.m)]
                    numX = int(self.xFactor*(self.m * self.n))
                    for i in range(0,numX):
                              while True :
                                        ix=randrange(self.m)
                                        iy=randrange(self.n)
                                        if self.theMaze[ix][iy] != ' X' :
                                                  break
                              self.theMaze[randrange(self.m)][randrange(self.n)] = '  X'
                    self.theMaze[self.start[0]][self.start[1]] = '  S' #open start position
                    self.theMaze[self.finish[0]][self.finish[1]] = '  F' #open stop position
                    return

          def printMaze(self):
          # this function will print the maze
                   for i in range(0,self.m):
                              print self.theMaze[i]

          def findPath(self): # start the recursion
                    return self.rfindPath(self.start[0],self.start[1])
        
          def rfindPath(self,i,j):
                    if(i < 0 or i > self.m or j < 0 or j > self.n):
                              return False
                    elif(self.theMaze[i][j] == ' X' or self.theMaze[i][j] ==' v' or self.theMaze[i][j] == ' *'):
                              if(self.theMaze[i][j] == ' *'):
                                        self.theMaze[i][j] == ' v'
                                        self.thePath.pop()       
                              return False

          def removeVs(self):
                    for i in range(0, self.m):
                              for j in range(0, self.n):
                                        if self.theMaze[i][j] == ' v':
                                                  self.theMaze[i][j] = ' O'
                    return

#------driver to test class Maze--------------------------
def main():
          "This function is to test the the Maze class. Here, we will call functions and make sure they work."
          m = 6
          n = 6
          start = (randrange(m), randrange(n))
          finish = (randrange(m), randrange(n))
          maze = Maze(m,n,(0,0),(m-1,n-1),.4)
          maze.printMaze()
          print "====================================="
          maze = Maze(m,n,start,finish,.4)
          maze.printMaze()

if __name__ == "__main__":
          main()
#file: path.py Partial code to be completed for pr1
# mcs275, spring 2009, lowman, Fatima N Mohammed

class Path:  # it's a list used like a Stack to store the path
        def __init__(self):
                self.P = []
        def push(self,t):
                self.P.append(t)
        def pop(self, t):
                self.pop()
        def top(self):
                return self.path[-1]
        def isEmpty(self):
                return (len(self.P)==0)
        def printPath(self):
                print self.P


#-----------test driver for class Path-----------------
def main():
        m=3
        n=5
        path = Path()
        start = (0,0)
        finish = (m-1,n-1)
        path.push(start)
        path.push( (1,0) )
        path.push( (1,1) )
        path.push( (1,2) )
        path.push( (2,2) )
        path.push( (2,3) )
        path.push(finish)
        path.printPath()
        grid = [['0' for j in range(n)] for i in range(m)]
        for t in path.P:
                i,j = t[0],t[1]
                grid[i][j] = '*'
        grid[start[0]][start[1]]= 'S'
        grid[finish[0]][finish[1]]= 'F'
        for i in range(m):
                print grid[i]

if __name__ == '__main__':
        main()

This code doesn't really do anything for me... can you explain the concept a little bit? When none of the menu choices do anything except for the 'new maze' bit.

Printing shows the maze but neither solve nor "Remove v's" does anything whatsoever. I'm not getting errors though...

hi, the point of these three files is to make a maze. when you start you get a menu. 0 quits, 1 makes the maze, and 2 prints it. I want to solve the maze by hitting three. @ this point, nothing will print. Python will 'solve' the maze. I want to print what python came up with, so I hit option 2. I should see the steps python took to solve the maze. This involves a recursion formula and a stack trace by keeping track of where I've visited. I should see v's for where I've visted and numbers that lead to the finish of the maze. The v's mean visited but those down count in the steps to solve the maze. When I hit 4, I should see only the numbers in the spaces python took to get to 'finish'. The v's should change back to their original spaces, which would be O's because those are 'open' spaces.

Is that a better explanation? I keep getting a loop in the maze.py file for line 58 and line 62.

I can't get any numbers to print or any v's to show. I can't get menu function three to print.

Thanks.

First things first, self.theMaze[i][j] == ' v' is a comparative statement. Double '=' compare two values while a single '=' assigns variables. So change that to self.theMaze[i][j] = ' v' I would suggest putting some tracing print statements throughout your maze class to verify that things are working the way you expect... I noticed that you mix up a single space and double spaces in your maze class which will definitely always fail, ie: ' v' is not equal to ' v' ... This comparison will always fail.

Hi:

When I change the == to =, I get 'syntax error'. Do I change all the variables x, o, s, f... I don't know what I did but when I hit 3, and try to print the maze, the original maze prints (and i don't get errors...wierd). How do I get the v's to print? Anyone have any clues? I don't see what I'm doing wrong. I can't test the path class either, I'm not sure how to do it.

Thank you for all of your help!

Well I notice in your maze class under the rfindPath function you have the following comparison: if(self.theMaze[i][j] == ' *') ; hoewver none of the entries in the maze EVER have an asterisk in them. Did you mean this to be a wildcard?

In your maze class under the main function you should also test this "solve" method and just work on that until you get it working. That should get you on your way.

This article has been dead for over six months. Start a new discussion instead.