diff --git a/dataUnit.pas b/dataUnit.pas index 59a470a..02cc588 100644 --- a/dataUnit.pas +++ b/dataUnit.pas @@ -3,13 +3,13 @@ Unit dataUnit; Interface -type +Type TMoverType = (glider, walker, pusher); TMoverDir = (up, left, down, right); -Const - maxHeight = 20; - maxWidth = 40; +Const + maxHeight = 20; + maxWidth = 40; empty = '.'; man = '&'; gem = '*'; @@ -20,27 +20,27 @@ Const block = 'M'; magnetx = 'N'; magnety = 'Z'; - turnerRight='a'; - turnerLeft='c'; + 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']; + 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; + magnets = [magnetx,magnety]; + pushable = [block,boulder,turnerLeft,turnerRight] + + magnets + gliders + walkers + pushers; + slipery = [wallSlip,boulder,gem] + gliders; enemies = ['%']; @@ -64,47 +64,47 @@ Type GameState = Record next : string; End; -function readLevel(levelFile, levelID:String; var succes: Boolean): GameState; +Function readLevel(levelFile, levelID:String; Var succes: Boolean): GameState; Implementation Uses sysutils; -function convert(x: Char):Char; +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; + 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; +Function readLevel(levelFile, levelID:String; Var succes: Boolean): GameState; -Var +Var aFile: textfile; thisID: String; row, col: integer; - x :Char; + x : Char; Begin succes := false; Assign(aFile,levelFile); Reset(aFile); readLn(aFile, thisID); - If length(levelID) <> 0 then - repeat - If EOF(aFile) then - Exit; + If length(levelID) <> 0 Then + Repeat + If EOF(aFile) Then + Exit; readLn(aFile, thisID); - until compareStr(thisID,levelID) = 0 - else + Until compareStr(thisID,levelID) = 0 + Else readLn(aFile, thisID); readLevel.ID := thisID; readLn(aFile, readLevel.clue); @@ -119,30 +119,29 @@ Begin Begin read(aFile, x); readLevel.map[row,col] := convert(x); - Case readLevel.map[row,col] Of + 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; + 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 := ''; + If Not EOF(aFile) Then + readLn(aFile,readLevel.next) + Else + readLevel.next := ''; Close(aFile); succes := true; End; End. - diff --git a/gameUnit.pas b/gameUnit.pas index 6b1eebc..dd35e4a 100644 --- a/gameUnit.pas +++ b/gameUnit.pas @@ -1,15 +1,16 @@ + Unit GameUnit; Interface -Uses +Uses dataUnit; -Procedure playLevel(var theGS: dataUnit.GameState); +Procedure playLevel(Var theGS: dataUnit.GameState); Implementation -Uses +Uses crt, Math, heroEnemy, @@ -18,7 +19,7 @@ moversProc; Procedure writeMap(Var theGS: dataUnit.GameState); -Var +Var col: integer; row: integer; Begin @@ -31,9 +32,9 @@ Begin End; End; -Procedure playLevel(var theGS: dataUnit.GameState); +Procedure playLevel(Var theGS: dataUnit.GameState); -Var +Var move: boolean; Begin randomize; @@ -47,7 +48,7 @@ Begin (* Player turn *) Repeat move := True; - Case readKey Of + Case readKey Of 'i': moveMan(theGS, -1, 0); 'j': moveMan(theGS, 0, -1); 'k': moveMan(theGS, 1, 0); @@ -67,11 +68,11 @@ Begin writeMap(theGS); gotoxy(theGS.mapWidth Div 2 - 4, theGS.mapHeight Div 2); If theGS.goal = 0 Then - Begin - Write('You Win!'); - End + Begin + Write('You Win!'); + End Else - Write('You Lose!'); + Write('You Lose!'); gotoxy(1, theGS.mapHeight + 2); writeLn('Press ENTER to continue.'); readLn; diff --git a/heroEnemy.pas b/heroEnemy.pas index ea1cbe4..f3b8cf9 100644 --- a/heroEnemy.pas +++ b/heroEnemy.pas @@ -12,7 +12,7 @@ Implementation Procedure moveMan(Var theGS:GameState;rowStep,colStep:integer); -Var +Var move: boolean = false; nextTile: char; Begin @@ -41,7 +41,7 @@ End; Procedure moveEnemies(Var theGS:GameState); -Var +Var i, row, col, rowstep, colstep, tries: integer; Begin For i := 1 To theGS.numEnemies Do @@ -68,10 +68,10 @@ Begin (* If there's an obstacle. *) If theGS.map[row+rowstep, col+colstep] <> empty Then Begin - If random(1) = 0 then - colstep := -colstep + If random(1) = 0 Then + colstep := -colstep Else - rowstep := -rowstep; + rowstep := -rowstep; If tries Mod 2 = 0 Then Begin (* Ol' swap trick *) diff --git a/moversProc.pas b/moversProc.pas index e2ebfe4..d93fba8 100644 --- a/moversProc.pas +++ b/moversProc.pas @@ -1,151 +1,154 @@ -unit moversProc; -interface +Unit moversProc; -uses dataUnit; +Interface -procedure moveMovers(var g: GameState); +Uses dataUnit; -implementation +Procedure moveMovers(Var g: GameState); -function turnMover(c: char; d: integer): char; -const +Implementation + +Function turnMover(c: char; d: integer): char; + +Const period = ord(high(TMoverDir))+1; -var + +Var j, x: integer; - i :TMoverType; -begin - for i := low(TMoverType) to high(TMoverType) do - for j := ord(low(TMoverDir)) to ord(high(TMoverDir)) do - if c = movers[i, TMoverDir(j)] then - begin - x := (j+d) mod period; - Exit(movers[i, TMoverDir(x)]); - end; + i : TMoverType; +Begin + For i := low(TMoverType) To high(TMoverType) Do + For j := ord(low(TMoverDir)) To ord(high(TMoverDir)) Do + If c = movers[i, TMoverDir(j)] Then + Begin + x := (j+d) Mod period; + Exit(movers[i, TMoverDir(x)]); + End; Exit(c); -end; +End; -procedure rotateStep(var rstep, cstep: integer; clockWise: boolean); -begin +Procedure rotateStep(Var rstep, cstep: integer; clockWise: boolean); +Begin cstep := cstep + rstep; rstep := cstep - rstep; cstep := cstep - rstep; - if clockWise then + If clockWise Then cstep := -cstep - else + Else rstep := -rstep; -end; +End; + +Procedure moveMover(Var g: GameState; row, col, rstep, cstep: integer); -procedure moveMover(var g: GameState; row, col, rstep, cstep: integer); -var - rns : integer = 0; - cns : integer = 0; -begin +Var + rns : integer = 0; + cns : integer = 0; +Begin (* I don't move if I'm cought by a magnet *) - If (rstep <> 0) and (g.map[row - rstep, col] = magnety) then Exit; - If (cstep <> 0) and (g.map[row, col - cstep] = magnetx) then Exit; + If (rstep <> 0) And (g.map[row - rstep, col] = magnety) Then Exit; + If (cstep <> 0) And (g.map[row, col - cstep] = magnetx) Then Exit; (* I don't move beyond the map *) - if (col + cstep > g.mapWidth) or (col + cstep < 1) or - (row + rstep > g.mapHeight) or (row + rstep < 1) then - begin - if g.map[row,col] in pushers then - g.map[row, col] := turnMover(g.map[row,col], 2); - Exit; - end; + If (col + cstep > g.mapWidth) Or (col + cstep < 1) Or + (row + rstep > g.mapHeight) Or (row + rstep < 1) Then + Begin + If g.map[row,col] In pushers Then + g.map[row, col] := turnMover(g.map[row,col], 2); + Exit; + End; (* Turners turn me *) - case g.map[row + rstep, col + cstep] of + Case g.map[row + rstep, col + cstep] Of 'a': - begin - g.map[row, col] := turnMover(g.map[row,col], 1); - rotateStep(rstep, cstep, True); - end; + Begin + g.map[row, col] := turnMover(g.map[row,col], 1); + rotateStep(rstep, cstep, True); + End; 'c': - begin - g.map[row, col] := turnMover(g.map[row,col], -1); - rotateStep(rstep, cstep, False); - end; - end; + Begin + g.map[row, col] := turnMover(g.map[row,col], -1); + rotateStep(rstep, cstep, False); + End; + End; (* Simple movement *) - if g.map[row + rstep, col + cstep] = empty then - begin + If g.map[row + rstep, col + cstep] = empty Then + Begin g.map[row + rstep, col + cstep] := g.map[row,col]; g.map[row, col] := empty; Exit; - end; + End; (* Pushers turn around and push pushables *) - if (g.map[row,col] in pushers) then - begin - if (g.map[row + rstep, col + cstep] in pushable) and - (g.map[row + 2 * rstep, col + 2 * cstep] = empty) then - begin - g.map[row + 2 * rstep, col + 2 * cstep] := g.map[row + rstep, col + cstep]; - g.map[row + rstep, col + cstep] := empty; - end; - g.map[row, col] := turnMover(g.map[row,col], 2); - Exit; - end; + If (g.map[row,col] In pushers) Then + Begin + If (g.map[row + rstep, col + cstep] In pushable) And + (g.map[row + 2 * rstep, col + 2 * cstep] = empty) Then + Begin + g.map[row + 2 * rstep, col + 2 * cstep] := g.map[row + rstep, col + cstep]; + g.map[row + rstep, col + cstep] := empty; + End; + g.map[row, col] := turnMover(g.map[row,col], 2); + Exit; + End; (* Gliders slip on slipery things*) - if (g.map[row,col] in gliders) and - (g.map[row + rstep, col + cstep] in slipery) then - begin - if cstep <> 0 then - rns := 2 * random(1) - 1 - else - cns := 2 * random(1) - 1; - - if (g.map[row + rns, col + cns] = empty) and - (g.map[row + rstep + rns, col + cstep + cns] = empty) then - begin - g.map[row + rstep + rns, col + cstep + cns] := g.map[row,col]; - g.map[row, col] := empty; - end - else if (g.map[row - rns, col - cns] = empty) and - (g.map[row + rstep - rns, col + cstep - cns] = empty) then - begin - g.map[row + rstep - rns, col + cstep - cns] := g.map[row,col]; - g.map[row, col] := empty; - end; - end; -end; - -procedure moveMovers(var g: GameState); - -var + If (g.map[row,col] In gliders) And + (g.map[row + rstep, col + cstep] In slipery) Then + Begin + If cstep <> 0 Then + rns := 2 * random(1) - 1 + Else + cns := 2 * random(1) - 1; + + If (g.map[row + rns, col + cns] = empty) And + (g.map[row + rstep + rns, col + cstep + cns] = empty) Then + Begin + g.map[row + rstep + rns, col + cstep + cns] := g.map[row,col]; + g.map[row, col] := empty; + End + Else If (g.map[row - rns, col - cns] = empty) And + (g.map[row + rstep - rns, col + cstep - cns] = empty) Then + Begin + g.map[row + rstep - rns, col + cstep - cns] := g.map[row,col]; + g.map[row, col] := empty; + End; + End; +End; + +Procedure moveMovers(Var g: GameState); + +Var idx, row, col, Width, Height: integer; -begin +Begin Width := g.mapWidth; Height := g.mapHeight; - for idx := 0 to Height * Width - 1 do - begin + For idx := 0 To Height * Width - 1 Do + Begin (* for up movers *) - row := idx div Width + 1; - col := idx mod Width + 1; - if (g.map[row, col] in UpMovers) then - moveMover(g, row, col, -1, 0); + row := idx Div Width + 1; + col := idx Mod Width + 1; + If (g.map[row, col] In UpMovers) Then + moveMover(g, row, col, -1, 0); (* for left movers *) - col := idx div Height + 1; - row := idx mod Height + 1; - if (g.map[row, col] in LeftMovers) then - moveMover(g, row, col, 0, -1); + col := idx Div Height + 1; + row := idx Mod Height + 1; + If (g.map[row, col] In LeftMovers) Then + moveMover(g, row, col, 0, -1); (* for down movers *) - row := Height - idx div Width + 1; - col := Width - idx mod Width + 1; - if (g.map[row, col] in DownMovers) then - moveMover(g, row, col, 1, 0); + row := Height - idx Div Width + 1; + col := Width - idx Mod Width + 1; + If (g.map[row, col] In DownMovers) Then + moveMover(g, row, col, 1, 0); (* for Right movers *) - col := Width - idx div Height + 1; - row := Height - idx mod Height + 1; - if (g.map[row, col] in RightMovers) then - moveMover(g, row, col, 0, 1); - end; -end; - -end. - + col := Width - idx Div Height + 1; + row := Height - idx Mod Height + 1; + If (g.map[row, col] In RightMovers) Then + moveMover(g, row, col, 0, 1); + End; +End; + +End. diff --git a/toolsProc.pas b/toolsProc.pas index e9af2d1..80862ed 100644 --- a/toolsProc.pas +++ b/toolsProc.pas @@ -22,7 +22,7 @@ End; Procedure doMagnet(Var g:GameState); -Var +Var row, col, i, s: integer; rstep: integer = 0; cstep: integer = 0; @@ -30,7 +30,7 @@ Begin For row := 1 To g.mapHeight Do For col := 1 To g.mapWidth Do Begin - Case g.map[row,col] Of + Case g.map[row,col] Of magnetx: cstep := 1; magnety: rstep := 1; Else continue;