jhylands

preliminary matireal

Apr 9th, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 13.15 KB | None | 0 0
  1. 'Skeleton Program code for the AQA COMP1 Summer 2012 examination
  2. 'this code should be used in conjunction with the Preliminary Material
  3. 'written by the AQA COMP1 Programmer Team
  4. 'developed in the Visual Studio 2008 (Console Mode) programming environment (VB.NET)
  5.  
  6. Module Module1
  7.   Const NoOfTraps = 2
  8.   Const NSDistance = 5
  9.   Const WEDistance = 7
  10.  
  11.   Structure CellReference
  12.     Dim NoOfCellsSouth As Integer
  13.     Dim NoOfCellsEast As Integer
  14.   End Structure
  15.  
  16.   Structure GameData
  17.     Dim TrapPositions() As CellReference
  18.     Dim MonsterPosition As CellReference
  19.     Dim PlayerPosition As CellReference
  20.     Dim FlaskPosition As CellReference
  21.     Dim MonsterAwake As Boolean
  22.   End Structure
  23.  
  24.   Sub Main()
  25.     Dim Cavern(NSDistance, WEDistance) As Char
  26.     Dim Choice As Integer
  27.     Dim FlaskPosition As CellReference
  28.     Dim MonsterAwake As Boolean
  29.     Dim MonsterPosition As CellReference
  30.     Dim PlayerPosition As CellReference
  31.     Dim TrapPositions(NoOfTraps) As CellReference
  32.     Randomize()
  33.     Do
  34.       DisplayMenu()
  35.       Choice = GetMainMenuChoice()
  36.       Select Case Choice
  37.         Case 1
  38.           SetUpGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake, True)
  39.           PlayGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake)
  40.         Case 2
  41.           LoadGame(TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake)
  42.           SetUpGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake, False)
  43.           PlayGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake)
  44.         Case 3 : SaveGame(TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake)
  45.         Case 4
  46.           SetUpTrainingGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake)
  47.           PlayGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake)
  48.       End Select
  49.     Loop Until Choice = 9
  50.   End Sub
  51.  
  52.   Sub DisplayMenu()
  53.     Console.WriteLine("MAIN MENU")
  54.     Console.WriteLine()
  55.     Console.WriteLine("1.  Start new game")
  56.     Console.WriteLine("2.  Load game")
  57.     Console.WriteLine("3.  Save game")
  58.     Console.WriteLine("4.  Play training game")
  59.     Console.WriteLine("9.  Quit")
  60.     Console.WriteLine()
  61.     Console.Write("Please enter your choice: ")
  62.   End Sub
  63.  
  64.   Function GetMainMenuChoice() As Integer
  65.     Dim Choice As Integer
  66.     Choice = CInt(Console.ReadLine())
  67.     Console.WriteLine()
  68.     GetMainMenuChoice = Choice
  69.   End Function
  70.  
  71.   Sub ResetCavern(ByRef Cavern(,) As Char)
  72.     Dim Count1 As Integer
  73.     Dim Count2 As Integer
  74.     For Count1 = 1 To NSDistance
  75.       For Count2 = 1 To WEDistance
  76.         Cavern(Count1, Count2) = " "
  77.       Next
  78.     Next
  79.   End Sub
  80.  
  81.   Function GetNewRandomPosition() As CellReference
  82.     Dim Position As CellReference
  83.     Do
  84.       Position.NoOfCellsSouth = Int(Rnd() * NSDistance) + 1
  85.       Position.NoOfCellsEast = Int(Rnd() * WEDistance) + 1
  86.     Loop Until Position.NoOfCellsSouth > 1 Or Position.NoOfCellsEast > 1
  87.     'a random coordinate of (1,1) needs to be rejected as this is the starting position of the player
  88.     GetNewRandomPosition = Position
  89.   End Function
  90.  
  91.   Sub SetPositionOfItem(ByRef Cavern(,) As Char, ByRef ObjectPosition As CellReference, ByVal Item As Char, ByVal NewGame As Boolean)
  92.     Dim Position As CellReference
  93.     If NewGame And Item <> "*" Then
  94.       Do
  95.         Position = GetNewRandomPosition()
  96.       Loop Until Cavern(Position.NoOfCellsSouth, Position.NoOfCellsEast) = " "
  97.       ObjectPosition = Position
  98.     End If
  99.     Cavern(ObjectPosition.NoOfCellsSouth, ObjectPosition.NoOfCellsEast) = Item
  100.   End Sub
  101.  
  102.   Sub SetUpGame(ByRef Cavern(,) As Char, ByRef TrapPositions() As CellReference, ByRef MonsterPosition As CellReference, ByRef PlayerPosition As CellReference, ByRef FlaskPosition As CellReference, ByRef MonsterAwake As Boolean, ByVal NewGame As Boolean)
  103.     Dim Count As Integer
  104.     ResetCavern(Cavern)
  105.     If NewGame Then
  106.       PlayerPosition.NoOfCellsSouth = 1
  107.       PlayerPosition.NoOfCellsEast = 1
  108.       MonsterAwake = False
  109.     End If
  110.     For Count = 1 To NoOfTraps
  111.       SetPositionOfItem(Cavern, TrapPositions(Count), "T", NewGame)
  112.     Next
  113.     SetPositionOfItem(Cavern, MonsterPosition, "M", NewGame)
  114.     SetPositionOfItem(Cavern, FlaskPosition, "F", NewGame)
  115.     SetPositionOfItem(Cavern, PlayerPosition, "*", NewGame)
  116.   End Sub
  117.  
  118.   Sub SetUpTrainingGame(ByRef Cavern(,) As Char, ByRef TrapPositions() As CellReference, ByRef MonsterPosition As CellReference, ByRef PlayerPosition As CellReference, ByRef FlaskPosition As CellReference, ByRef MonsterAwake As Boolean)
  119.     ResetCavern(Cavern)
  120.     PlayerPosition.NoOfCellsSouth = 3
  121.     PlayerPosition.NoOfCellsEast = 5
  122.     MonsterAwake = False
  123.     TrapPositions(1).NoOfCellsSouth = 2
  124.     TrapPositions(1).NoOfCellsEast = 7
  125.     TrapPositions(2).NoOfCellsSouth = 4
  126.     TrapPositions(2).NoOfCellsEast = 5
  127.     MonsterPosition.NoOfCellsSouth = 1
  128.     MonsterPosition.NoOfCellsEast = 4
  129.     FlaskPosition.NoOfCellsSouth = 5
  130.     FlaskPosition.NoOfCellsEast = 6
  131.     SetUpGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake, False)
  132.   End Sub
  133.  
  134.   Sub LoadGame(ByRef TrapPositions() As CellReference, ByRef MonsterPosition As CellReference, ByRef PlayerPosition As CellReference, ByRef FlaskPosition As CellReference, ByRef MonsterAwake As Boolean)
  135.     Dim Filename As String
  136.     Dim LoadedGameData As GameData
  137.     Console.Write("Enter the name of the file to load: ")
  138.     Filename = Console.ReadLine
  139.     Console.WriteLine()
  140.     FileOpen(1, Filename, OpenMode.Binary, OpenAccess.Read)
  141.     FileGet(1, LoadedGameData)
  142.     FileClose(1)
  143.     TrapPositions = LoadedGameData.TrapPositions
  144.     MonsterPosition = LoadedGameData.MonsterPosition
  145.     PlayerPosition = LoadedGameData.PlayerPosition
  146.     FlaskPosition = LoadedGameData.FlaskPosition
  147.     MonsterAwake = LoadedGameData.MonsterAwake
  148.   End Sub
  149.  
  150.   Sub SaveGame(ByVal TrapPositions() As CellReference, ByVal MonsterPosition As CellReference, ByVal PlayerPosition As CellReference, ByVal FlaskPosition As CellReference, ByVal MonsterAwake As Boolean)
  151.     Dim Filename As String
  152.     Dim CurrentGameData As GameData
  153.     CurrentGameData.TrapPositions = TrapPositions
  154.     CurrentGameData.MonsterPosition = MonsterPosition
  155.     CurrentGameData.PlayerPosition = PlayerPosition
  156.     CurrentGameData.FlaskPosition = FlaskPosition
  157.     CurrentGameData.MonsterAwake = MonsterAwake
  158.     Console.Write("Enter new file name: ")
  159.     Filename = Console.ReadLine
  160.     Console.WriteLine()
  161.     FileOpen(1, Filename, OpenMode.Binary, OpenAccess.Write)
  162.     FilePut(1, CurrentGameData)
  163.     FileClose(1)
  164.   End Sub
  165.  
  166.   Sub DisplayCavern(ByVal Cavern(,) As Char, ByVal MonsterAwake As Boolean)
  167.     Dim Count1 As Integer
  168.     Dim Count2 As Integer
  169.     For Count1 = 1 To NSDistance
  170.       Console.WriteLine(" ------------- ")
  171.       For Count2 = 1 To WEDistance
  172.         If Cavern(Count1, Count2) = " " Or Cavern(Count1, Count2) = "*" Or (Cavern(Count1, Count2) = "M" And MonsterAwake) Then
  173.           Console.Write("|" & Cavern(Count1, Count2))
  174.         Else
  175.           Console.Write("| ")
  176.         End If
  177.       Next
  178.       Console.WriteLine("|")
  179.     Next
  180.     Console.WriteLine(" ------------- ")
  181.     Console.WriteLine()
  182.   End Sub
  183.  
  184.   Sub DisplayMoveOptions()
  185.     Console.WriteLine()
  186.     Console.WriteLine("Enter N to move NORTH")
  187.     Console.WriteLine("Enter E to move EAST")
  188.     Console.WriteLine("Enter S to move SOUTH")
  189.     Console.WriteLine("Enter W to move WEST")
  190.     Console.WriteLine("Enter M to return to the Main Menu")
  191.     Console.WriteLine()
  192.   End Sub
  193.  
  194.   Function GetMove() As Char
  195.     Dim Move As Char
  196.     Move = Console.ReadLine
  197.     Console.WriteLine()
  198.     GetMove = Move
  199.   End Function
  200.  
  201.   Sub MakeMove(ByRef Cavern(,) As Char, ByVal Direction As Char, ByRef PlayerPosition As CellReference)
  202.     Cavern(PlayerPosition.NoOfCellsSouth, PlayerPosition.NoOfCellsEast) = " "
  203.     Select Case Direction
  204.       Case "N"
  205.         PlayerPosition.NoOfCellsSouth = PlayerPosition.NoOfCellsSouth - 1
  206.       Case "S"
  207.         PlayerPosition.NoOfCellsSouth = PlayerPosition.NoOfCellsSouth + 1
  208.       Case "W"
  209.         PlayerPosition.NoOfCellsEast = PlayerPosition.NoOfCellsEast - 1
  210.       Case "E"
  211.         PlayerPosition.NoOfCellsEast = PlayerPosition.NoOfCellsEast + 1
  212.     End Select
  213.     Cavern(PlayerPosition.NoOfCellsSouth, PlayerPosition.NoOfCellsEast) = "*"
  214.   End Sub
  215.  
  216.   Function CheckValidMove(ByVal PlayerPosition As CellReference, ByVal Direction As Char) As Boolean
  217.     Dim ValidMove As Boolean
  218.     ValidMove = True
  219.     If Not (Direction = "N" Or Direction = "S" Or Direction = "W" Or Direction = "E" Or Direction = "M") Then
  220.       ValidMove = False
  221.     End If
  222.     CheckValidMove = ValidMove
  223.   End Function
  224.  
  225.   Function CheckIfSameCell(ByVal FirstCellPosition As CellReference, ByVal SecondCellPosition As CellReference) As Boolean
  226.     Dim InSameCell As Boolean
  227.     InSameCell = False
  228.     If FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth And FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast Then
  229.       InSameCell = True
  230.     End If
  231.     CheckIfSameCell = InSameCell
  232.   End Function
  233.  
  234.   Sub DisplayWonGameMessage()
  235.     Console.WriteLine("Well done!  You have found the flask containing the Styxian potion.")
  236.     Console.WriteLine("You have won the game of MONSTER!")
  237.     Console.WriteLine()
  238.   End Sub
  239.  
  240.   Sub DisplayTrapMessage()
  241.     Console.WriteLine("Oh no!  You have set off a trap.  Watch out, the monster is now awake!")
  242.     Console.WriteLine()
  243.   End Sub
  244.  
  245.   Sub MoveFlask(ByRef Cavern(,) As Char, ByVal NewCellForFlask As CellReference, ByRef FlaskPosition As CellReference)
  246.     Cavern(NewCellForFlask.NoOfCellsSouth, NewCellForFlask.NoOfCellsEast) = "F"
  247.     Cavern(FlaskPosition.NoOfCellsSouth, FlaskPosition.NoOfCellsEast) = " "
  248.     FlaskPosition = NewCellForFlask
  249.   End Sub
  250.  
  251.   Sub MakeMonsterMove(ByRef Cavern(,) As Char, ByRef MonsterPosition As CellReference, ByRef FlaskPosition As CellReference, ByVal PlayerPosition As CellReference)
  252.     Dim OriginalMonsterPosition As CellReference
  253.     Dim MonsterMovedToSameCellAsFlask As Boolean
  254.     OriginalMonsterPosition = MonsterPosition
  255.     Cavern(MonsterPosition.NoOfCellsSouth, MonsterPosition.NoOfCellsEast) = " "
  256.     If MonsterPosition.NoOfCellsSouth < PlayerPosition.NoOfCellsSouth Then
  257.       MonsterPosition.NoOfCellsSouth = MonsterPosition.NoOfCellsSouth + 1
  258.     Else
  259.       If MonsterPosition.NoOfCellsSouth > PlayerPosition.NoOfCellsSouth Then
  260.         MonsterPosition.NoOfCellsSouth = MonsterPosition.NoOfCellsSouth - 1
  261.       Else
  262.         If MonsterPosition.NoOfCellsEast < PlayerPosition.NoOfCellsEast Then
  263.           MonsterPosition.NoOfCellsEast = MonsterPosition.NoOfCellsEast + 1
  264.         Else
  265.           MonsterPosition.NoOfCellsEast = MonsterPosition.NoOfCellsEast - 1
  266.         End If
  267.       End If
  268.     End If
  269.     MonsterMovedToSameCellAsFlask = CheckIfSameCell(MonsterPosition, FlaskPosition)
  270.     If MonsterMovedToSameCellAsFlask Then
  271.       MoveFlask(Cavern, OriginalMonsterPosition, FlaskPosition)
  272.     End If
  273.     Cavern(MonsterPosition.NoOfCellsSouth, MonsterPosition.NoOfCellsEast) = "M"
  274.   End Sub
  275.  
  276.   Sub DisplayLostGameMessage()
  277.     Console.WriteLine("ARGHHHHHH!  The monster has eaten you.  GAME OVER.")
  278.     Console.WriteLine("Maybe you will have better luck next time you play MONSTER!")
  279.     Console.WriteLine()
  280.   End Sub
  281.  
  282.   Sub PlayGame(ByRef Cavern(,) As Char, ByVal TrapPositions() As CellReference, ByRef MonsterPosition As CellReference, ByRef PlayerPosition As CellReference, ByRef FlaskPosition As CellReference, ByRef MonsterAwake As Boolean)
  283.     Dim Count As Integer
  284.     Dim Eaten As Boolean
  285.     Dim FlaskFound As Boolean
  286.     Dim MoveDirection As Char
  287.     Dim ValidMove As Boolean
  288.     Eaten = False
  289.     FlaskFound = False
  290.     DisplayCavern(Cavern, MonsterAwake)
  291.     Do
  292.       Do
  293.         DisplayMoveOptions()
  294.         MoveDirection = GetMove()
  295.         ValidMove = CheckValidMove(PlayerPosition, MoveDirection)
  296.       Loop Until ValidMove
  297.       If MoveDirection <> "M" Then
  298.         MakeMove(Cavern, MoveDirection, PlayerPosition)
  299.         DisplayCavern(Cavern, MonsterAwake)
  300.         FlaskFound = CheckIfSameCell(PlayerPosition, FlaskPosition)
  301.         If FlaskFound Then
  302.           DisplayWonGameMessage()
  303.         End If
  304.         Eaten = CheckIfSameCell(MonsterPosition, PlayerPosition)
  305.         If Not MonsterAwake And Not FlaskFound And Not Eaten Then
  306.           MonsterAwake = CheckIfSameCell(PlayerPosition, TrapPositions(1))
  307.           If Not MonsterAwake Then
  308.             MonsterAwake = CheckIfSameCell(PlayerPosition, TrapPositions(2))
  309.           End If
  310.           If MonsterAwake Then
  311.             DisplayTrapMessage()
  312.             DisplayCavern(Cavern, MonsterAwake)
  313.           End If
  314.         End If
  315.         If MonsterAwake And Not Eaten And Not FlaskFound Then
  316.           Count = 0
  317.           Do
  318.             MakeMonsterMove(Cavern, MonsterPosition, FlaskPosition, PlayerPosition)
  319.             Eaten = CheckIfSameCell(MonsterPosition, PlayerPosition)
  320.             Console.WriteLine()
  321.             Console.WriteLine("Press Enter key to continue")
  322.             Console.ReadLine()
  323.             DisplayCavern(Cavern, MonsterAwake)
  324.             Count = Count + 1
  325.           Loop Until Count = 2 Or Eaten
  326.         End If
  327.         If Eaten Then
  328.           DisplayLostGameMessage()
  329.         End If
  330.       End If
  331.     Loop Until Eaten Or FlaskFound Or MoveDirection = "M"
  332.   End Sub
  333. End Module
Advertisement
Add Comment
Please, Sign In to add comment