Advertisement
Guest User

Eughhh... Pascal

a guest
Aug 8th, 2016
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 15.14 KB | None | 0 0
  1. Program Monster;
  2.  
  3. {Skeleton Program code for the AQA COMP1 Summer 2012 examination
  4. this code should be used in conjunction with the Preliminary Material
  5. written by the AQA COMP1 Programmer Team developed in the
  6. Turbo Pascal v7 programming environment}
  7.  
  8. {Centres using Delphi should add the compiler directive that sets
  9. the application type to Console (other centres can ignore this
  10. comment).  Centres may also add the SysUtils library if their
  11. version of Pascal uses this}
  12.  
  13. {Permission to make these changes to the Skeleton Program does not
  14. need to be obtained from AQA/AQA Programmer - just remove the \ symbol from
  15. the next line of code  and remove the braces around Uses SysUtils;}
  16.  
  17. {$APPTYPE CONSOLE}
  18.  
  19. Uses
  20.   SysUtils;
  21.  
  22. Const NoOfTraps = 2;
  23. Const NSDistance = 5;
  24. Const WEDistance = 7;
  25.  
  26. Type
  27.   TCellReference = Record
  28.                      NoOfCellsSouth : Integer;
  29.                      NoOfCellsEast : Integer;
  30.                    End;
  31.   TCavern = Array[1..NSDistance, 1..WEDistance] Of Char;
  32.   TTrapPositions = Array[1..NoOfTraps] Of TCellReference;
  33.   TGameData = Record
  34.                 TrapPositions : TTrapPositions;
  35.                 MonsterPosition : TCellReference;
  36.                 PlayerPosition : TCellReference;
  37.                 FlaskPosition : TCellReference;
  38.                 MonsterAwake : Boolean;
  39.               End;
  40.  
  41. Var
  42.   Cavern : TCavern;
  43.   Choice : Integer;
  44.   FlaskPosition : TCellReference;
  45.   MonsterAwake : Boolean;
  46.   MonsterPosition : TCellReference;
  47.   PlayerPosition : TCellReference;
  48.   TrapPositions : TTrapPositions;
  49.  
  50. Procedure DisplayMenu;
  51.   Begin
  52.     Writeln('MAIN MENU');
  53.     Writeln;
  54.     Writeln('1.  Start new game');
  55.     Writeln('2.  Load game');
  56.     Writeln('3.  Save game');
  57.     Writeln('4.  Play training game');
  58.     Writeln('9.  Quit');
  59.     Writeln;
  60.     Write('Please enter your choice: ');
  61.   End;
  62.  
  63. Function GetMainMenuChoice : Integer;
  64.   Var
  65.     Choice : Integer;
  66.   Begin
  67.     Readln(Choice);
  68.     Writeln;
  69.     GetMainMenuChoice := Choice;
  70.   End;
  71.  
  72. Procedure ResetCavern(Var Cavern : TCavern);
  73.   Var
  74.     Count1 : Integer;
  75.     Count2 : Integer;
  76.   Begin
  77.     For Count1 := 1 to NSDistance
  78.       Do
  79.         For Count2 := 1 to WEDistance
  80.           Do Cavern[Count1, Count2] := ' ';
  81.   End;
  82.  
  83. Procedure GetNewRandomPosition(Var NewPosition : TCellReference);
  84.   Var
  85.     Position : TCellReference;
  86.   Begin
  87.     Repeat
  88.       Position.NoOfCellsSouth := Random(NSDistance) + 1;
  89.       Position.NoOfCellsEast := Random(WEDistance) + 1;
  90.     Until (Position.NoOfCellsSouth > 1) Or (Position.NoOfCellsEast > 1);
  91.     {a random coordinate of (1,1) needs to be rejected as this is the starting position of the player}
  92.     NewPosition := Position;
  93.   End;
  94.  
  95. Procedure SetPositionOfItem(Var Cavern : TCavern; Var ObjectPosition : TCellReference; Item : Char; NewGame : Boolean);
  96.   Var
  97.     Position : TCellReference;
  98.   Begin
  99.     If NewGame And (Item <> '*')
  100.       Then
  101.         Begin
  102.           Repeat
  103.             GetNewRandomPosition(Position);
  104.           Until Cavern[Position.NoOfCellsSouth, Position.NoOfCellsEast] = ' ';
  105.           ObjectPosition := Position;
  106.         End;
  107.     Cavern[ObjectPosition.NoOfCellsSouth, ObjectPosition.NoOfCellsEast] := Item;
  108.   End;
  109.  
  110. Procedure SetUpGame(Var Cavern : TCavern; Var TrapPositions : TTrapPositions; Var MonsterPosition,
  111.   PlayerPosition , FlaskPosition: TCellReference;  Var MonsterAwake : Boolean; NewGame : Boolean);
  112.   Var
  113.     Count : Integer;
  114.   Begin
  115.     ResetCavern(Cavern);
  116.     If NewGame
  117.       Then
  118.         Begin
  119.           PlayerPosition.NoOfCellsSouth := 1;
  120.           PlayerPosition.NoOfCellsEast := 1;
  121.           MonsterAwake := False;
  122.         End;
  123.     For Count := 1 To NoOfTraps
  124.       Do SetPositionOfItem(Cavern, TrapPositions[Count], 'T', NewGame);
  125.     SetPositionOfItem(Cavern, MonsterPosition, 'M', NewGame);
  126.     SetPositionOfItem(Cavern, FlaskPosition, 'F', NewGame);
  127.     SetPositionOfItem(Cavern, PlayerPosition, '*', NewGame);
  128.   End;
  129.  
  130. Procedure SetUpTrainingGame(Var Cavern : TCavern; Var TrapPositions : TTrapPositions; Var MonsterPosition,
  131.   PlayerPosition, FlaskPosition : TCellReference; Var MonsterAwake : Boolean);
  132.   Begin
  133.     ResetCavern(Cavern);
  134.     PlayerPosition.NoOfCellsSouth := 3;
  135.     PlayerPosition.NoOfCellsEast := 5;
  136.     MonsterAwake := False;
  137.     TrapPositions[1].NoOfCellsSouth := 2;
  138.     TrapPositions[1].NoOfCellsEast := 7;
  139.     TrapPositions[2].NoOfCellsSouth := 4;
  140.     TrapPositions[2].NoOfCellsEast := 5;
  141.     MonsterPosition.NoOfCellsSouth := 1;
  142.     MonsterPosition.NoOfCellsEast := 4;
  143.     FlaskPosition.NoOfCellsSouth := 5;
  144.     FlaskPosition.NoOfCellsEast := 6;
  145.     SetUpGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake, False);
  146.   End;
  147.  
  148. Procedure LoadGame(Var TrapPositions : TTrapPositions; Var MonsterPosition, PlayerPosition,
  149.                    FlaskPosition : TCellReference; Var MonsterAwake : Boolean);
  150.   Var
  151.     CurrentFile : File Of TGameData;
  152.     Filename : String;
  153.     LoadedGameData : TGameData;
  154.   Begin
  155.     Write('Enter the name of the file to load: ');
  156.     Readln(Filename);
  157.     Writeln;
  158.     Assign(CurrentFile, Filename);
  159.     Reset(CurrentFile);
  160.     Read(CurrentFile, LoadedGameData);
  161.     Close(CurrentFile);
  162.     TrapPositions := LoadedGameData.TrapPositions;
  163.     MonsterPosition := LoadedGameData.MonsterPosition;
  164.     PlayerPosition := LoadedGameData.PlayerPosition;
  165.     FlaskPosition := LoadedGameData.FlaskPosition;
  166.     MonsterAwake := LoadedGameData.MonsterAwake;
  167.   End;
  168.  
  169. Procedure SaveGame(TrapPositions : TTrapPositions; MonsterPosition, PlayerPosition,
  170.                    FlaskPosition : TCellReference; MonsterAwake : Boolean);
  171.   Var
  172.     CurrentFile : File Of TGameData;
  173.     Filename : String;
  174.     CurrentGameData : TGameData;
  175.   Begin
  176.     CurrentGameData.TrapPositions := TrapPositions;
  177.     CurrentGameData.MonsterPosition := MonsterPosition;
  178.     CurrentGameData.PlayerPosition := PlayerPosition;
  179.     CurrentGameData.FlaskPosition := FlaskPosition;
  180.     CurrentGameData.MonsterAwake := MonsterAwake;
  181.     Write('Enter new file name: ');
  182.     Readln(Filename);
  183.     Writeln;
  184.     Assign(CurrentFile, Filename);
  185.     Rewrite(CurrentFile);
  186.     Write(CurrentFile, CurrentGameData);
  187.     Close(CurrentFile);
  188.   End;
  189.  
  190. Procedure DisplayCavern(Cavern : TCavern; MonsterAwake : Boolean);
  191.   Var
  192.     Count1 : Integer;
  193.     Count2 : Integer;
  194.   Begin
  195.     For Count1 := 1 To NSDistance
  196.       Do
  197.         Begin
  198.           Writeln(' ------------- ');
  199.           For Count2 := 1 To WEDistance
  200.             Do
  201.               If (Cavern[Count1, Count2] In [' ','*']) Or ((Cavern[Count1,Count2] = 'M') And MonsterAwake)
  202.                 Then Write('|',Cavern[Count1, Count2])
  203.                 Else Write('| ');
  204.           Writeln('|');
  205.         End;
  206.     Writeln( ' ------------- ');
  207.     Writeln;
  208.   End;
  209.  
  210. Procedure DisplayMoveOptions;
  211.   Begin
  212.     Writeln;
  213.     Writeln('Enter N to move NORTH');
  214.     Writeln('Enter E to move EAST');
  215.     Writeln('Enter S to move SOUTH');
  216.     Writeln('Enter W to move WEST');
  217.     Writeln('Enter M to return to the Main Menu');
  218.     Writeln;
  219.   End;
  220.  
  221. Function GetMove : Char;
  222.   Var
  223.     Move : Char;
  224.   Begin
  225.     Readln(Move);
  226.     Writeln;
  227.     GetMove := Move;
  228.   End;
  229.  
  230. Procedure MakeMove(Var Cavern : TCavern; Direction : Char; Var PlayerPosition : TCellReference);
  231.   Begin
  232.     Cavern[PlayerPosition.NoOfCellsSouth, PlayerPosition.NoOfCellsEast] := ' ';
  233.     Case Direction Of
  234.       'N' : PlayerPosition.NoOfCellsSouth := PlayerPosition.NoOfCellsSouth - 1;
  235.       'S' : PlayerPosition.NoOfCellsSouth := PlayerPosition.NoOfCellsSouth + 1;
  236.       'W' : PlayerPosition.NoOfCellsEast := PlayerPosition.NoOfCellsEast - 1;
  237.       'E' : PlayerPosition.NoOfCellsEast := PlayerPosition.NoOfCellsEast + 1;
  238.     End;
  239.     Cavern[PlayerPosition.NoOfCellsSouth, PlayerPosition.NoOfCellsEast] := '*';
  240.   End;
  241.  
  242. Function CheckValidMove(PlayerPosition : TCellReference; Direction : Char) : Boolean;
  243.   Var
  244.     ValidMove : Boolean;
  245.   Begin
  246.     ValidMove := True;
  247.     If Not (Direction In ['N','S','W','E','M'])
  248.       Then ValidMove := False;
  249.     if (direction = 'N') and (PlayerPosition.NoOfCellsSouth = 1) then ValidMove := false;
  250.     CheckValidMove := ValidMove;
  251.   End;
  252.  
  253. Function CheckIfSameCell(FirstCellPosition, SecondCellPosition : TCellReference) : Boolean;
  254.   Var
  255.     InSameCell : Boolean;
  256.   Begin
  257.     InSameCell := False;
  258.     If (FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth)
  259.       And (FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast)
  260.       Then InSameCell := True;
  261.     CheckIfSameCell := InSameCell;
  262.   End;
  263.  
  264. Procedure DisplayWonGameMessage;
  265.   Begin
  266.     Writeln('Well done!  You have found the flask containing the Styxian potion.');
  267.     Writeln('You have won the game of MONSTER!');
  268.     Writeln;
  269.   End;
  270.  
  271. Procedure DisplayTrapMessage;
  272.   Begin
  273.     Writeln('Oh no!  You have set off a trap.  Watch out, the monster is now awake!');
  274.     Writeln;
  275.   End;
  276.  
  277. Procedure MoveFlask(Var Cavern : TCavern; NewCellForFlask : TCellReference; Var FlaskPosition : TCellReference);
  278.   Begin
  279.     Cavern[NewCellForFlask.NoOfCellsSouth, NewCellForFlask.NoOfCellsEast] := 'F';
  280.     Cavern[FlaskPosition.NoOfCellsSouth, FlaskPosition.NoOfCellsEast] := ' ';
  281.     FlaskPosition := NewCellForFlask;
  282.   End;
  283.  
  284. Procedure MakeMonsterMove(Var Cavern : TCavern; Var MonsterPosition, FlaskPosition : TCellReference;
  285.   PlayerPosition : TCellReference);
  286.   Var
  287.     OriginalMonsterPosition : TCellReference;
  288.     MonsterMovedToSameCellAsFlask : Boolean;
  289.   Begin
  290.     OriginalMonsterPosition := MonsterPosition;
  291.     Cavern[MonsterPosition.NoOfCellsSouth, MonsterPosition.NoOfCellsEast] := ' ';
  292.     If MonsterPosition.NoOfCellsSouth < PlayerPosition.NoOfCellsSouth
  293.       Then MonsterPosition.NoOfCellsSouth := MonsterPosition.NoOfCellsSouth + 1
  294.       Else
  295.         If MonsterPosition.NoOfCellsSouth > PlayerPosition.NoOfCellsSouth
  296.           Then MonsterPosition.NoOfCellsSouth := MonsterPosition.NoOfCellsSouth - 1
  297.           Else
  298.             If MonsterPosition.NoOfCellsEast < PlayerPosition.NoOfCellsEast
  299.               Then MonsterPosition.NoOfCellsEast := MonsterPosition.NoOfCellsEast + 1
  300.               Else MonsterPosition.NoOfCellsEast := MonsterPosition.NoOfCellsEast - 1;
  301.     MonsterMovedToSameCellAsFlask := CheckIfSameCell(MonsterPosition, FlaskPosition);
  302.     If MonsterMovedToSameCellAsFlask
  303.       Then MoveFlask(Cavern, OriginalMonsterPosition, FlaskPosition);
  304.     Cavern[MonsterPosition.NoOfCellsSouth, MonsterPosition.NoOfCellsEast] := 'M';
  305.   End;
  306.  
  307. Procedure DisplayLostGameMessage;
  308.   Begin
  309.     Writeln('ARGHHHHHH!  The monster has eaten you.  GAME OVER.');
  310.     Writeln('Maybe you will have better luck next time you play MONSTER!');
  311.     Writeln;
  312.   End;
  313.  
  314. Function TrapDetector(PlayerPosition: TCellReference; TrapPositions : TTrapPositions): Boolean;
  315.   var
  316.     currentCell: TCellReference;
  317.     I: integer;
  318.   begin
  319.     for I := 1 to 2 do
  320.     begin
  321.       currentCell := TrapPositions[I];
  322.       if((abs(PlayerPosition.NoOfCellsSouth-currentCell.NoOfCellsSouth) = 1) or
  323.          (abs(PlayerPosition.NoOfCellsEast-currentCell.NoOfCellsEast) = 1)) then
  324.           exit(true);
  325.     end;
  326.   end;
  327.  
  328. Procedure PlayGame(Var Cavern : TCavern; TrapPositions : TTrapPositions; Var MonsterPosition, PlayerPosition,
  329.   FlaskPosition : TCellReference; Var MonsterAwake : Boolean);
  330.   Var
  331.     Count : Integer;
  332.     Eaten : Boolean;
  333.     Score: Integer;
  334.     FlaskFound : Boolean;
  335.     MoveDirection : Char;
  336.     ValidMove : Boolean;
  337.   Begin
  338.     Score := 0;
  339.     Eaten:= False;
  340.     FlaskFound := False;
  341.     DisplayCavern(Cavern, MonsterAwake);
  342.     Repeat
  343.       Repeat
  344.         DisplayMoveOptions;
  345.         MoveDirection := GetMove;
  346.         ValidMove := CheckValidMove(PlayerPosition, MoveDirection);
  347.         if not ValidMove then writeln('That is not a valid move, please try again.');
  348.       Until ValidMove;
  349.       If MoveDirection <> 'M'
  350.         Then
  351.           Begin
  352.             Score := Score + 10;
  353.             MakeMove(Cavern, MoveDirection, PlayerPosition);
  354.             DisplayCavern(Cavern, MonsterAwake);
  355.             FlaskFound := CheckIfSameCell(PlayerPosition, FlaskPosition);
  356.             if FlaskFound then
  357.                 begin
  358.                   Score := Score + 50;
  359.                   DisplayWonGameMessage;
  360.                 end;
  361.             Eaten := CheckIfSameCell(MonsterPosition, PlayerPosition);
  362.             If Not MonsterAwake And Not FlaskFound And Not Eaten
  363.               Then
  364.                 Begin
  365.                   MonsterAwake := CheckIfSameCell(PlayerPosition, TrapPositions[1]);
  366.                   If Not MonsterAwake
  367.                     Then MonsterAwake := CheckIfSameCell(PlayerPosition, TrapPositions[2]);
  368.                   if not MonsterAwake then
  369.                     begin
  370.                       if TrapDetector(PlayerPosition, TrapPositions) then writeln('Trap detected')
  371.                       else writeln('No trap detected');
  372.                     end;
  373.                   If MonsterAwake
  374.                     Then
  375.                       Begin
  376.                         Score := Score - 10;
  377.                         DisplayTrapMessage;
  378.                         DisplayCavern(Cavern, MonsterAwake);
  379.                       End;
  380.                 End;
  381.             If MonsterAwake And Not Eaten And Not FlaskFound
  382.               Then
  383.                 Begin
  384.                   Count := 0;
  385.                   Repeat
  386.                     MakeMonsterMove(Cavern, MonsterPosition, FlaskPosition, PlayerPosition);
  387.                     Eaten := CheckIfSameCell(MonsterPosition, PlayerPosition);
  388.                     Writeln;
  389.                     Writeln('Press Enter key to continue');
  390.                     Readln;
  391.                     DisplayCavern(Cavern, MonsterAwake);
  392.                     Count := Count + 1;
  393.                   Until (Count = 2) Or Eaten;
  394.                 End;
  395.               If Eaten Then
  396.                 begin
  397.                   Score := Score - 50;
  398.                   DisplayLostGameMessage;
  399.                 end;
  400.           End;
  401.     Until Eaten Or FlaskFound Or (MoveDirection = 'M');
  402.     writeln('Your score was: ', Score);
  403.   End;
  404.  
  405.   Begin
  406.     Randomize;
  407.     Repeat
  408.       DisplayMenu;
  409.       Choice := GetMainMenuChoice;
  410.       Case Choice Of
  411.         1 : Begin
  412.               SetUpGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake, True);
  413.               PlayGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake);
  414.             End;
  415.         2 : Begin
  416.               LoadGame(TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake);
  417.               SetUpGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake, False);
  418.               PlayGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake);
  419.             End;
  420.         3 : SaveGame(TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake);
  421.         4 : Begin
  422.               SetUpTrainingGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake);
  423.               PlayGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake);
  424.             End;
  425.       End;
  426.     Until Choice = 9;
  427.   End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement