Simplified BFS.getPathTo()

This commit is contained in:
Yair Morgenstern 2019-07-12 15:40:03 +03:00
parent d9942aec7c
commit 0fe3de0d50

View File

@ -39,11 +39,11 @@ class BFS(val startingPoint: TileInfo, val predicate : (TileInfo) -> Boolean){
val path = ArrayList<TileInfo>() val path = ArrayList<TileInfo>()
path.add(destination) path.add(destination)
var currentNode = destination var currentNode = destination
while(currentNode != startingPoint){ while(currentNode != startingPoint) {
tilesReached[currentNode]?.let { val parent = tilesReached[currentNode]
currentNode = it if (parent == null) return ArrayList()// destination is not in our path
path.add(currentNode) currentNode = parent
} ?: return ArrayList() // destination is not in our path path.add(currentNode)
} }
return path return path
} }