1 implementation module Sokoban
8 Start :: *World -> (String, *World)
10 # (s, w) = parse "../sokobanzip/screens/screen.1" w
11 = ("\n" +++ toString s, w)
13 instance toString SokobanPuzzle where
14 toString (Sokoban []) = ""
15 toString (Sokoban [[]:rows]) = "\n" +++ toString (Sokoban rows)
16 toString (Sokoban [[x:xs]:rows]) =
17 toString x +++ toString (Sokoban [xs:rows])
19 instance toString SokobanTile where
25 toString TargetBox = "*"
26 toString TargetAgent = "+"
28 parse :: String *World -> (SokobanPuzzle, *World)
30 # (ok, f, w) = fopen fp FReadText w
31 | not ok = abort ("Couldn't open file: '" +++ fp +++ "'")
32 # (contents, f) = readEntireFile f
33 | isEmpty contents = abort "File is empty or unreadable"
34 # (ok, w) = fclose f w
35 | not ok = abort "Couldn't close file"
36 = (Sokoban (parseRows contents), w)
38 parseRows :: [Char] -> [[SokobanTile]]
39 parseRows cs = case parseRow cs of
41 (x, rest) = [x:parseRows rest]
43 parseRow :: [Char] -> ([SokobanTile], [Char])
44 parseRow [] = ([], [])
45 parseRow ['\n':xs] = ([], xs)
47 # (rest, xs) = parseRow xs
56 _ = abort ("Unknown char: '" +++ toString x +++ "'")
57 = ([current:rest], xs)
59 readEntireFile :: *File -> *([Char], *File)
61 # (b, c, f) = freadc f
63 # (cs, f) = readEntireFile f