```
#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()
```