-CLM:=clm
-CLMFLAGS:=
+CLM:=~/projects/clean/bin/clm
+CLMFLAGS:=-nr -nt
all: SokobanObjectwise
instance toString SokobanPuzzle
parse :: String *World -> (SokobanPuzzle, *World)
+parseFromFile :: *File -> (SokobanPuzzle, *File)
+
+numberOfBlocks :: SokobanPuzzle -> Int
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
module SokobanObjectwise
+import StdString
+import StdFile
+import StdTuple
+import StdFunc
+import StdOrdList
+import StdList
import Sokoban
-Start = "hi"
+Start :: *World -> *World
+Start w
+# (io, w) = stdio w
+# (puzzle, io) = parseFromFile io
+# io = io <<< encode puzzle <<< "\n"
+= snd (fclose io w)
+
+encode :: SokobanPuzzle -> String
+encode p = foldr ((+++) o (+++) "\n") "" ([
+ "MODULE",
+ "main",
+ "VAR":encodeBoxes p maxX maxY])
+ where
+ annot = annotate p
+ (maxX, maxY) = getMetrics annot
+
+:: AnnotatedSokoban :== [(Int, Int, SokobanTile)]
+
+annotate :: SokobanPuzzle -> AnnotatedSokoban
+annotate (Sokoban p) = flatten [[(x, y, t)\\t<-r & x<-[0..]]\\r<-p & y<-[0..]]
+
+getMetrics :: AnnotatedSokoban -> (Int, Int)
+getMetrics p = (maxList (map fst3 p),maxList (map snd3 p))
+
+getBoxes :: AnnotatedSokoban -> [(Int, Int)]
+getBoxes p = [t\\t=:(_, _, Box)<-p] ++
+
+ where
+ getBox` _ [] = []
+ getBox` x [b:bs] = let r = getBox` (x+1) bs in case b of
+ Box = [x:r]
+ TargetBox = [x:r]
+ _ = r
+
+(<+) infixr 5 :: a b -> String | toString a & toString b
+(<+) a b = toString a +++ toString b
+
+encodeBoxes :: SokobanPuzzle Int Int -> [String]
+encodeBoxes p mx my = [
+ "\tbox" <+ i <+ "x: " <+ "0 .. " <+ mx <+ ";\n\tbox" <+
+ i <+ "y: " <+ "0 .. " <+ my <+ ";"\\(bx, by)<-getBoxes p & i<-[0..]]