Skip to content

Instantly share code, notes, and snippets.

@ibebrett
Created September 25, 2014 18:40
Show Gist options
  • Save ibebrett/f242dcef85b836789f64 to your computer and use it in GitHub Desktop.
Save ibebrett/f242dcef85b836789f64 to your computer and use it in GitHub Desktop.
Shortest Path
pathRecurse :: Board -> [Pos] -> Pos -> Pos -> Maybe [Pos]
pathRecurse b visited pa pb
| pa == pb = Just (visited++[pb])
| null neighbors = Nothing
| null subpaths = Nothing
| otherwise = Just ( minimumBy (comparePath) subpaths )
where subpaths = catMaybes ( map (\n -> pathRecurse b (visited++[pa]) n pb) neighbors)
neighbors = filter (\x -> (walkable b x) && not (x `elem` visited)) (getNeighbors b pa)
comparePath x y = compare (length x) (length y)
path :: Board -> Pos -> Pos -> Maybe [Pos]
path b pa pb = pathRecurse b [] pa pb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment