I have tried many times to make a program that finds a path by itself in a table. However I always manage to make a function that works only once.
Here is the function that I've made:
def PathSearch(StartingPoint, Table, List= [], PointsAlreadyVisited = []): #tuple, double List, List, List
#Initialisation des variables
LU, LL, LR = [], [], []
PointsAlreadyVisited += [StartingPoint]
PositionAbove = (StartingPoint[0],StartingPoint[1]-1)
value_above = Table[PositionAbove[1]][PositionAbove[0]]
PositionRight = (StartingPoint[0]+1,StartingPoint[1])
value_right = Table[PositionRight[1]][PositionRight[0]]
PositionLeft = (StartingPoint[0]-1,StartingPoint[1])
value_left= Table[PositionLeft[1]][PositionLeft[0]]
Retour = []
if value_above== 0 and (PositionAbove not in PointsAlreadyVisited ):
LU = Liste + [PositionAbove]
if PositionAbove[1] != 1:
LU = PathSearch(PositionAbove, Table, LU)
if value_right== 0 and (PositionRight not in PointsAlreadyVisited ): #This checks that there is a path on the right and that we haven't checked that position already.
LR = PathSearch(PositionRight, Table, List +[PositionRight])
if value_left == 0 and (PositionLeft not in PointsAlreadyVisited ): #This checks that there is a path on the left and that we haven't checked that position already.
LL = PathSearch(PositionLeft, Table, List +[PositionLeft])
if value_left != 0 and value_right!= 0 and value_above!= 0: #This is the case where there is no more path (either on left, right or above.
retour =[-1]
#This part is to determine the shortest path:
else:
minimum = Minimum_Liste(LL, LR, LU)
if minimum == [-1]:
minimum_bis = Minimum_Liste(Maximum_Liste(LL,LU), Maximum_Liste(LU, LR))
if minimum_bis == [-1]:
retour = Maximum_Liste(Maximum_Liste(LL, LU), Maximum_Liste(LR, LU))
else:
retour = minimum_bis
else:
retour = minimum
return(retour)
The function works when I call it once, however when I call it a second time with the same parameters, it doesn't work.
The problem lies in the PointsAlreadyVisited list: When I run the function a second time, the list is still full of the position checked during the last call.
I have tried to use the "clear" function for lists but I can't manage to implement it without breaking the function.
If anyone has any thoughts on how to solve this problem, I'd be grateful, Thanks !