Answer To: please make sure to deliver on time
Snehil answered on Oct 25 2020
maze.py
import sys
import copy
import os
import traceback
class MazeError(Exception):
def __init__(self,message):
super().__init__(message)
self.message = message
class Point:
def __init__(self,val,right_val,down_val):
self.val = val
self.right_val = right_val
self.down_val = down_val
self.dir = [True,True,True,True]
if self.val == 1:
self.dir[0] = False
elif self.val == 2:
self.dir[2] = False
elif self.val == 3:
self.dir[0] = False
self.dir[2] = False
if self.right_val == 2 or self.right_val == 3:
self.dir[3] = False
if self.down_val == 1 or self.down_val == 3:
self.dir[1] = False
self.vis = False
class Wall:
def __init__(self,up_val,down_val,left_val,right_val):
self.dir = [0,0,0,0]
self.up_val = up_val
self.down_val = down_val
self.left_val = left_val
self.right_val = right_val
if self.up_val == 1:
self.dir[0] = 1
if self.down_val == 1:
self.dir[1] = 1
if self.left_val == 1:
self.dir[2] = 1
if self.right_val == 1:
self.dir[3] = 1
self.vis = False
self.draw_v_row = False
self.draw_v_col = False
class InnerPoint:
def __init__(self,val,right_val,down_val):
self.val = val
self.right_val = right_val
self.down_val = down_val
self.dir = [True,True,True,True]
if self.val == 1:
self.dir[0] = False
elif self.val == 2:
self.dir[2] = False
elif self.val == 3:
self.dir[0] = False
self.dir[2] = False
if self.right_val == 2 or self.right_val == 3:
self.dir[3] = False
if self.down_val == 1 or self.down_val == 3:
self.dir[1] = False
self.vis = False
class Ways:
def __init__(self,val,right_val,down_val):
self.val = val
self.right_val = right_val
self.down_val = down_val
self.dir = [True,True,True,True]
if self.val == 1:
self.dir[0] = False
elif self.val == 2:
self.dir[2] = False
elif self.val == 3:
self.dir[0] = False
self.dir[2] = False
if self.right_val == 2 or self.right_val == 3:
self.dir[3] = False
if self.down_val == 1 or self.down_val == 3:
self.dir[1] = False
self.vis = False
self.row_vis = False
self.col_vis = False
class NodeCul:
def __init__(self,val):
self.val = val
self.vis = False
class Maze:
def __init__(self, file_name):
self.gate_val = 0
self.walls_val = 0
self.inner_points = 0
self.areas = 0
self.cul_de_sacs = 0
self.paths = 0
self.maze=[]
self.walls = []
self.points = []
self.gates = []
self.ways = []
self.innerPoints = []
self.c=[]
self.culDeSacs=[]
self.length=0
self.width=0
self.cul = None
self.route = None
self.fileName = file_name
self.open_file(self.fileName)
def open_file(self,file_name):
try:
file_object = open(file_name)
temp=[]
temp2=[]
Read=file_object.readlines()
if (Read==[]):
raise MazeError("Incorrect input.")
for line in Read:
data = line.split()
temp.append(data)
for i in temp:
if i!=[]:
temp2.append(i)
for i in temp2:
row = []
if len(i)==1:
for x in i:
for y in x:
row.append(int(y))
if not 0 <= int(y) <= 3:
raise MazeError("Incorrect input.")
else:
for k in i:
row.append(int(k))
if not 0 <= int(k) <= 3:
raise MazeError("Incorrect input.")
self.maze.append(row)
# row columns should be more than 2
if len(self.maze) < 2 or len(self.maze[0]) < 2:
raise MazeError("Incorrect input.")
# all row lengths should be same
for i in range(len(self.maze)-1):
if len(self.maze[i]) != len(self.maze[i+1]):
raise MazeError("Incorrect input.")
# row and coloumn lengths should be less than equal to 31 and 41 respectively
if len(self.maze) > 31 or len(self.maze[0]) > 41:
raise MazeError("Incorrect input.")
# val constraint on the right line and the bottom line
for i in range(len(self.maze)):
for j in range(len(self.maze[0])):
if i == len(self.maze) - 1:
if self.maze[i][j] == 2 or self.maze[i][j] == 3:
raise MazeError("Input does not represent a maze.")
if j == len(self.maze[0]) - 1:
if self.maze[i][j] == 1 or self.maze[i][j] == 3:
raise MazeError("Input does not represent a maze.")
self.analyse()
except ValueError:
traceback.print_exc()
print('maze.MazeError: Incorrect input.')
except MazeError as m:
traceback.print_exc()
print('maze.MazeError:',m.message)
file_object.close()
def VisitWall(self,me,i,j):
if 0 <= i <= self.length and 0 <= j <= self.width:
if me.vis == False:
me.vis = True
if me.dir[0] == 1:
self.VisitWall(self.walls[i-1][j],i-1,j)
if me.dir[1] == 1:
self.VisitWall(self.walls[i+1][j],i+1,j)
if me.dir[2] == 1:
self.VisitWall(self.walls[i][j-1],i,j-1)
if me.dir[3] == 1:
self.VisitWall(self.walls[i][j+1],i,j+1)
def VisitInnerPoint (self,i,j):
if 0 <= i < self.length-1 and 0 <= j < self.width-1:
if self.innerPoints[i][j].vis == False:
self.innerPoints[i][j].vis = True
if self.innerPoints[i][j].dir[0] == True:
self.VisitInnerPoint(i-1,j)
if self.innerPoints[i][j].dir[1] == True:
self.VisitInnerPoint(i+1,j)
if self.innerPoints[i][j].dir[2] == True:
self.VisitInnerPoint(i,j-1)
if self.innerPoints[i][j].dir[3] == True:
self.VisitInnerPoint(i,j+1)
def FindTrue(self,arr,n):
count = 0
for i in range(len(arr)):
if arr[i] == True and i!=n :
count = count + 1
return count
def FindPath(self,i,j,L_reach):
if 0 <= i < self.length-1 and 0 <= j < self.width-1 and self.cul[i][j].val == 2 and (i,j) not in self.culDeSacs:
if self.cul[i][j].vis == False:
self.cul[i][j].vis = True
if self.ways[i][j].dir[0] == True:
self.FindPath(i-1,j,L_reach)
if self.ways[i][j].dir[1] == True:
self.FindPath(i+1,j,L_reach)
if self.ways[i][j].dir[2] == True:
self.FindPath(i,j-1,L_reach)
if self.ways[i][j].dir[3] == True:
self.FindPath(i,j+1,L_reach)
L_reach.append((i,j))
def Check_Diff(self,arr1,arr2):
temp = []
for i in arr1:
if i in arr2:
temp.append(i)
if len(temp) == 1:
return True
else:
return False
def CheckValue(self,cul):
for i in range(0,len(cul)):
for j in range(0,len(cul[0])):
if cul[i][j].val == 1:
return False
return True
def FindCuls(self,i,j,l_cul_de):
if 0 <= i < self.length-1 and 0 <= j < self.width-1 and self.cul[i][j].val <= -1:
if self.cul[i][j].vis == False:
self.cul[i][j].vis = True
if self.ways[i][j].dir[0] == True:
self.FindCuls(i-1,j,l_cul_de)
if self.ways[i][j].dir[1] == True:
self.FindCuls(i+1,j,l_cul_de)
if self.ways[i][j].dir[2] == True:
self.FindCuls(i,j-1,l_cul_de)
if self.ways[i][j].dir[3] == True:
self.FindCuls(i,j+1,l_cul_de)
l_cul_de.append((i,j))
def analyse(self):
for i in range(0,len(self.maze)-1):
row = []
for j...