Unit dataUnit; Interface Type TMoverType = (glider, walker, pusher); TMoverDir = (up, left, down, right); Const maxHeight = 20; maxWidth = 40; empty = '.'; man = '&'; gem = '*'; wall = '#'; wallSlip = '@'; earth = 'H'; boulder = '0'; block = 'M'; magnetx = 'N'; magnety = 'Z'; turnerRight = 'a'; turnerLeft = 'c'; monster = '%'; walkable = [empty,earth,gem]; movers : array[TMoverType, TMoverDir] Of char = ( ('^','>','v','<'), ('u','r','d','l'), ('U','R','D','L') ); gliders = ['^','>','v','<']; walkers = ['u','r','d','l']; pushers = ['U','R','D','L']; UpMovers = ['u', 'U', '^']; DownMovers = ['d', 'D', 'v']; LeftMovers = ['l', 'L', '<']; RightMovers = ['r', 'R', '>']; magnets = [magnetx,magnety]; pushable = [block,boulder,turnerLeft,turnerRight] + magnets + gliders + walkers + pushers; slipery = [wallSlip,boulder,gem] + gliders; enemies = ['%']; Type Position = Record col: integer; row: integer; End; Type GameState = Record ID : string; map : array [1..maxHeight, 1..maxWidth] Of char; mapHeight : integer; mapWidth : integer; manRow : integer; manCol : integer; goal : integer; enemy : array [1..10] Of Position; numEnemies : integer; clue : string; next : string; End; Function readLevel(levelFile, levelID:String; Var succes: Boolean): GameState; Implementation Uses sysutils; Function convert(x: Char): Char; Begin Case x Of 'K': Exit(man); ' ': Exit(empty); '5': Exit(wall); '1'..'4','6'..'9': Exit(wallSlip); 'e': Exit(earth); 'b': Exit(block); 's': Exit(magnety); '~','E','C','[': Exit(monster); 'B': Exit(boulder); Else Exit(x); End; End; Function readLevel(levelFile, levelID:String; Var succes: Boolean): GameState; Var aFile: textfile; thisID: String; row, col: integer; x : Char; Begin succes := false; Assign(aFile,levelFile); Reset(aFile); readLn(aFile, thisID); If length(levelID) <> 0 Then Repeat If EOF(aFile) Then Exit; readLn(aFile, thisID); Until compareStr(thisID,levelID) = 0 Else readLn(aFile, thisID); readLevel.ID := thisID; readLn(aFile, readLevel.clue); readLn(aFile); readLevel.mapHeight := 20; readLevel.mapWidth := 30; readLevel.numEnemies := 0; readLevel.goal := 0; For row := 1 To readLevel.mapHeight Do Begin For col:= 1 To readLevel.mapWidth Do Begin read(aFile, x); readLevel.map[row,col] := convert(x); Case readLevel.map[row,col] Of man: Begin readLevel.manRow := row; readLevel.manCol := col; End; monster: Begin inc (readLevel.numEnemies); readLevel.enemy[readLevel.numEnemies].col := col; readLevel.enemy[readLevel.numEnemies].row := row; End; gem: inc(readLevel.goal); End; End; readLn(aFile); End; If Not EOF(aFile) Then readLn(aFile,readLevel.next) Else readLevel.next := ''; Close(aFile); succes := true; End; End.