X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=code%2FSokoban.icl;h=804a7cd86d37ac243c8f99dc8edd020dd31a14e6;hb=528cda8a5116dbed02a1e603d7fca2033dd0c100;hp=9102bc1f8ae0a8cd171b96258e57e8e930098868;hpb=ce272707c750ffd26760802cde66e8da29c246b7;p=mc1516pa.git diff --git a/code/Sokoban.icl b/code/Sokoban.icl index 9102bc1..804a7cd 100644 --- a/code/Sokoban.icl +++ b/code/Sokoban.icl @@ -22,16 +22,27 @@ instance toString SokobanTile where toString Box = "$" toString Agent = "@" toString Target = "." + toString TargetBox = "*" + toString TargetAgent = "+" + +parseFromFile :: *File -> (SokobanPuzzle, *File) +parseFromFile f +# (contents, f) = readEntireFile f +| isEmpty contents = abort "File is empty or unreadable" += (Sokoban (parseRows contents), f) parse :: String *World -> (SokobanPuzzle, *World) parse fp w # (ok, f, w) = fopen fp FReadText w | not ok = abort ("Couldn't open file: '" +++ fp +++ "'") -# (contents, f) = readEntireFile f -| isEmpty contents = abort "File is empty or unreadable" +# (puzzle, f) = parseFromFile f # (ok, w) = fclose f w | not ok = abort "Couldn't close file" -= (Sokoban (parseRows contents), w) += (puzzle, w) + +numberOfBlocks :: SokobanPuzzle -> Int +numberOfBlocks (Sokoban bs) = let fbs = flatten bs in + length ([1\\(Box)<-fbs] ++ [1\\(TargetBox)<-fbs]) parseRows :: [Char] -> [[SokobanTile]] parseRows cs = case parseRow cs of @@ -48,6 +59,8 @@ parseRow [x:xs] '$' = Box '@' = Agent '.' = Target + '+' = TargetAgent + '*' = TargetBox ' ' = Free _ = abort ("Unknown char: '" +++ toString x +++ "'") = ([current:rest], xs)