The problem is the statements like:
upData = currentData
This does not clone the list, but instead aliases it. Again, Java object model.
However, Python is friendlier here in that it offers a general-purpose copy module. Here you could solve the problem by invoking the module like:
# At the top, or just inside findPathimport copy# ...upData = copy.copy(currentData)# Use upData
When the structure is complex (i.e. contains nested lists), there is also copy.deepcopy which applies recursively to the contained things.
However, there is a much simpler and idiomatic solution: instead of "copying" the list and appending to it (the reason you get the garbage results is that all the changes get applied to the passed-in currentData via the upData etc. aliases), do the append in a way that implicitly makes a copy:
upData = currentData + ['U']
That said, lots of cleanup is possible here. The directions are all treated in similar ways, so there's code duplication there that we can clean up. Also, we can represent currentSquare not as a list of two ints (as it currently appears to be) but instead represent points with complex numbers, and then use plain old addition to generate the new points.
def findPath(self, currentData, currentSquare, destinationSquare, remaining): # I renamed 'movementLeft' because the 'left' is confusing ;) # Similarly, change the getTile interface to accept the coordinate directly. tileInfo = self.getTile(currentSquare) currentTileMovementCosts = self.mapKey.getMovementCost(tileInfo) # Guard clause: Can we no longer get there? if movementLeft < 0: currentData[0] = -1 return currentData # Guard clause: Did we just get there? if currentSquare == destinationSquare: return currentData # Otherwise, recurse over four neighbouring directions. currentData[0] += currentTileMovementCosts remaining -= currentTileMovementCosts # set the default ("failed") result, and improve it with the recursion # results if possible. bestResult = [-1] + currentData[1:] for direction in ([['L'], complex(-1, 0), ['U'], complex(0, -1), ['D'], complex(0, 1), ['R'], complex(1, 0)]): tempData = currentData + direction[0] # append direction code tempSquare = currentSquare + direction[1] # add direction vector result = self.findPath(tempData, tempSquare, destinationSquare, remaining) # If that's better than what we have, set it. if result[0] != -1 and (bestResult[0] == -1 or result[0] < bestResult[0]): bestResult = result # don't need to copy; we won't modify it, # but just possibly replace it :) return bestResult