From 3108bc9aad4327c577be0409dc378df7c9c82a44 Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Tue, 10 Jan 2012 01:25:14 +0530 Subject: [PATCH] Added comments --- chapter4/SlidingPuzzle.hs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/chapter4/SlidingPuzzle.hs b/chapter4/SlidingPuzzle.hs index 53caf14..bc3ab7e 100644 --- a/chapter4/SlidingPuzzle.hs +++ b/chapter4/SlidingPuzzle.hs @@ -1,3 +1,6 @@ +-- Solves the sliding puzzle problem (http://en.wikipedia.org/wiki/Sliding_puzzle) +-- using A* algorithm + import Data.Ix import Data.Array import Data.List @@ -31,7 +34,7 @@ type Cost = Int class Eq a => GameState a where succs :: a -> [(a, Cost)] --- A* algorithm: Find a path from initialState to goalState using heuristic +-- A* algorithm: Find a path from initial state to goal state using heuristic astar :: (GameState a, Show a, Ord a) => a -> a -> (a -> a -> Cost) -> [a] astar initState goalState hueristic = astar' (PQ.singleton (hueristic initState goalState) (initState, 0)) S.empty M.empty @@ -163,8 +166,8 @@ puzzlePairty pz = i = inversions pz b = fst . blankPos $ pz --- Solves an n sliding puzzle from initState to goalState using heuristic. --- Return Nothing if the goalState is not reachable from initState +-- Solves a sliding puzzle from initial state to goal state using the given heuristic. +-- Return Nothing if the goal state is not reachable from initial state -- else returns Just solution. solvePuzzle :: (Show a, Ord a) => Puzzle a -> Puzzle a -> (Puzzle a -> Puzzle a -> Cost) -> Maybe [Puzzle a] @@ -173,7 +176,7 @@ solvePuzzle initState goalState hueristic = then Nothing else Just (astar initState goalState hueristic) --- Returns number of tiles in wrong position in givenState compared to goalState +-- Returns number of tiles in wrong position in given state compared to goal state wrongTileCount :: Eq a => Puzzle a -> Puzzle a -> Cost wrongTileCount givenState goalState = length . filter (\(a, b) -> a /= b) @@ -191,19 +194,19 @@ sumManhattanDistance givenState goalState = . assocs . pzState $ givenState where revM = M.fromList . map (\(x, y) -> (y, x)) . assocs . pzState $ goalState - --- The classic 15 puzzle + +-- The classic 15 puzzle (http://en.wikipedia.org/wiki/Fifteen_puzzle) fifteenPuzzle :: IO () fifteenPuzzle = do -- Random generator gen <- newStdGen - + -- The goal let goalState = fromJust $ fromList 0 4 [0..15] -- Shuffle the goal to get a random puzzle state let initState = evalState (shufflePuzzle 50 goalState) gen -- Solve using sum manhattan distance heuristic let solution = fromJust $ solvePuzzle initState goalState sumManhattanDistance - + + -- Print the solution forM_ solution $ \s -> print s - \ No newline at end of file