started with some necoding
[mc1516pa.git] / code / SokobanObjectwise.icl
1 module SokobanObjectwise
2
3 import StdString
4 import StdFile
5 import StdTuple
6 import StdFunc
7 import StdOrdList
8 import StdList
9 import Sokoban
10
11 Start :: *World -> *World
12 Start w
13 # (io, w) = stdio w
14 # (puzzle, io) = parseFromFile io
15 # io = io <<< encode puzzle <<< "\n"
16 = snd (fclose io w)
17
18 encode :: SokobanPuzzle -> String
19 encode p = foldr ((+++) o (+++) "\n") "" ([
20 "MODULE",
21 "main",
22 "VAR":encodeBoxes p maxX maxY])
23 where
24 annot = annotate p
25 (maxX, maxY) = getMetrics annot
26
27 :: AnnotatedSokoban :== [(Int, Int, SokobanTile)]
28
29 annotate :: SokobanPuzzle -> AnnotatedSokoban
30 annotate (Sokoban p) = flatten [[(x, y, t)\\t<-r & x<-[0..]]\\r<-p & y<-[0..]]
31
32 getMetrics :: AnnotatedSokoban -> (Int, Int)
33 getMetrics p = (maxList (map fst3 p),maxList (map snd3 p))
34
35 getBoxes :: AnnotatedSokoban -> [(Int, Int)]
36 getBoxes p = [t\\t=:(_, _, Box)<-p] ++
37
38 where
39 getBox` _ [] = []
40 getBox` x [b:bs] = let r = getBox` (x+1) bs in case b of
41 Box = [x:r]
42 TargetBox = [x:r]
43 _ = r
44
45 (<+) infixr 5 :: a b -> String | toString a & toString b
46 (<+) a b = toString a +++ toString b
47
48 encodeBoxes :: SokobanPuzzle Int Int -> [String]
49 encodeBoxes p mx my = [
50 "\tbox" <+ i <+ "x: " <+ "0 .. " <+ mx <+ ";\n\tbox" <+
51 i <+ "y: " <+ "0 .. " <+ my <+ ";"\\(bx, by)<-getBoxes p & i<-[0..]]