Lynix_

Monster Code Mk1

Jun 11th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Module Module1
  2.  
  3.     Sub Main()
  4.         Dim Choice As Integer = 0
  5.         While Choice <> 9
  6.             DisplayMenu()
  7.             Choice = GetMainMenuChoice()
  8.             Select Case Choice
  9.                 Case 1
  10.                     Dim MyGame As New Game(False, False)
  11.                 Case 2
  12.                     Dim MyGame As New Game(True, False)
  13.                 Case 3
  14.                     Dim MyGame As New Game(False, True)
  15.             End Select
  16.         End While
  17.     End Sub
  18.     Public Sub DisplayMenu()
  19.         Console.WriteLine("MAIN MENU")
  20.         Console.WriteLine()
  21.         Console.WriteLine("1. Start new game")
  22.         Console.WriteLine("2. Play training game")
  23.         Console.WriteLine("3. Load Game")
  24.         Console.WriteLine("9. Quit")
  25.         Console.WriteLine()
  26.         Console.Write("Please enter your choice: ")
  27.     End Sub
  28.     Public Function GetMainMenuChoice() As Integer
  29.         Dim Choice As Integer
  30.         Choice = CInt(Console.ReadLine())
  31.         Console.WriteLine()
  32.         Return Choice
  33.     End Function
  34.     Structure CellReference
  35.         Dim NoOfCellsEast As Integer
  36.         Dim NoOfCellsSouth As Integer
  37.     End Structure
  38.     Class Game
  39.         Const NS As Integer = 4
  40.         Const WE As Integer = 6
  41.         Private Player As New Character
  42.         Private Cavern As New Grid(NS, WE)
  43.         Private Monster As New Enemy
  44.         Private Flask As New Item
  45.         Private Trap1 As New Trap
  46.         Private Trap2 As New Trap
  47.         Private TrainingGame As Boolean
  48.         Private Load As Boolean
  49.         Public Sub New(ByVal IsATrainingGame As Boolean, ByVal IsALoadGame As Boolean)
  50.             TrainingGame = IsATrainingGame
  51.             Load = IsALoadGame
  52.             Randomize()
  53.             SetUpGame()
  54.             Play()
  55.         End Sub
  56.         Public Sub Play()
  57.             Dim Count As Integer
  58.             Dim Eaten As Boolean
  59.             Dim FlaskFound As Boolean
  60.             Dim MoveDirection As Char
  61.             Dim ValidMove As Boolean
  62.             Dim Position As CellReference
  63.             Eaten = False
  64.             FlaskFound = False
  65.             Cavern.Display(Monster.GetAwake)
  66.             Do
  67.                 Do
  68.                     DisplayMoveOptions()
  69.                     MoveDirection = GetMove()
  70.                     ValidMove = CheckValidMove(MoveDirection)
  71.                 Loop Until ValidMove
  72.                 If MoveDirection <> "M" Then
  73.                     Cavern.PlaceItem(Player.GetPosition, " ")
  74.                     Player.MakeMove(MoveDirection)
  75.                     Cavern.PlaceItem(Player.GetPosition, "*")
  76.                     Cavern.Display(Monster.GetAwake)
  77.                     FlaskFound = Player.CheckIfSameCell(Flask.GetPosition)
  78.                     If FlaskFound Then
  79.                         DisplayWonGameMessage()
  80.                     End If
  81.                     Eaten = Monster.CheckIfSameCell(Player.GetPosition)
  82.                     'This selection structure checks to see if the player has triggered one of the traps in the cavern
  83.                    If Not Monster.GetAwake And Not FlaskFound And Not Eaten And
  84.                    (Player.CheckIfSameCell(Trap1.GetPosition) And Not Trap1.GetTriggered Or
  85.                    Player.CheckIfSameCell(Trap2.GetPosition) And Not Trap2.GetTriggered) Then
  86.                         Monster.ChangeSleepStatus()
  87.                         DisplayTrapMessage()
  88.                         Cavern.Display(Monster.GetAwake)
  89.                     End If
  90.                     If Monster.GetAwake And Not Eaten And Not FlaskFound Then
  91.                         Count = 0
  92.                         Do
  93.                             Cavern.PlaceItem(Monster.GetPosition, " ")
  94.                             Position = Monster.GetPosition
  95.                             Monster.MakeMove(Player.GetPosition)
  96.                             Cavern.PlaceItem(Monster.GetPosition, "M")
  97.                             If Monster.CheckIfSameCell(Flask.GetPosition) Then
  98.                                 Flask.SetPosition(Position)
  99.                                 Cavern.PlaceItem(Position, "F")
  100.                             End If
  101.                             Eaten = Monster.CheckIfSameCell(Player.GetPosition)
  102.                             Console.WriteLine()
  103.                             Console.WriteLine("Press Enter key to continue")
  104.                             Console.ReadLine()
  105.                             Cavern.Display(Monster.GetAwake)
  106.                             Count = Count + 1
  107.                         Loop Until Count = 2 Or Eaten
  108.                     End If
  109.                     If Eaten Then
  110.                         DisplayLostGameMessage()
  111.                     End If
  112.                 End If
  113.             Loop Until Eaten Or FlaskFound Or MoveDirection = "M"
  114.         End Sub
  115.         Public Sub DisplayMoveOptions()
  116.             Console.WriteLine()
  117.             Console.WriteLine("Enter N to move NORTH")
  118.             Console.WriteLine("Enter S to move SOUTH")
  119.             Console.WriteLine("Enter E to move EAST")
  120.             Console.WriteLine("Enter W to move WEST")
  121.             Console.WriteLine("Enter Q to Save Game")
  122.             Console.WriteLine("Enter M to return to the Main Menu")
  123.             Console.WriteLine()
  124.         End Sub
  125.         Public Function GetMove() As Char
  126.             Dim Move As Char
  127.             Move = Console.ReadLine
  128.             Console.WriteLine()
  129.             Return Move
  130.         End Function
  131.         Public Sub DisplayWonGameMessage()
  132.             Console.WriteLine("Well done! You have found the flask containing the Styxian potion.")
  133.             Console.WriteLine("You have won the game of MONSTER!")
  134.             Console.WriteLine()
  135.         End Sub
  136.         Public Sub DisplayTrapMessage()
  137.             Console.WriteLine("On no! You have set off a trap. Watch out, the monster is now awake!")
  138.             Console.WriteLine()
  139.         End Sub
  140.         Public Sub DisplayLostGameMessage()
  141.             Console.WriteLine("ARGHHHHHH! The monster has eaten you. GAMEOVER.")
  142.             Console.WriteLine("Maybe you will have better luck next time you play MONSTER! ")
  143.             Console.WriteLine()
  144.         End Sub
  145.         Public Function CheckValidMove(ByVal Direction As Char) As Boolean
  146.             Dim ValidMove As Boolean
  147.             ValidMove = True
  148.             If Not (Direction = "N" Or Direction = "S" Or Direction = "W" Or
  149.            Direction = "E" Or Direction = "M" Or Direction = "Q") Then
  150.                 ValidMove = False
  151.             End If
  152.             If Direction = "E" And Player.GetPosition.NoOfCellsEast = 6 Then ValidMove = False
  153.             If Direction = "W" And Player.GetPosition.NoOfCellsEast = 0 Then ValidMove = False
  154.             If Direction = "S" And Player.GetPosition.NoOfCellsSouth = 4 Then ValidMove = False
  155.             If Direction = "N" And Player.GetPosition.NoOfCellsSouth = 0 Then ValidMove = False
  156.             If Direction = "Q" Then SaveGame()
  157.             Return ValidMove
  158.         End Function
  159.         Public Sub SaveGame()
  160.             Dim Save As New System.IO.StreamWriter("savegame.txt")
  161.             Save.Write(Monster.GetPosition.NoOfCellsEast)
  162.             Save.Write(Monster.GetPosition.NoOfCellsSouth)
  163.             Save.Write(Player.GetPosition.NoOfCellsEast)
  164.             Save.Write(Player.GetPosition.NoOfCellsSouth)
  165.             Save.Write(Trap1.GetPosition.NoOfCellsEast)
  166.             Save.Write(Trap1.GetPosition.NoOfCellsSouth)
  167.             Save.Write(Trap2.GetPosition.NoOfCellsEast)
  168.             Save.Write(Trap2.GetPosition.NoOfCellsSouth)
  169.             Save.Write(Flask.GetPosition.NoOfCellsEast)
  170.             Save.Write(Flask.GetPosition.NoOfCellsSouth)
  171.             Save.WriteLine(Trap1.GetTriggered)
  172.             Save.WriteLine(Trap2.GetTriggered)
  173.             Save.WriteLine(Monster.GetAwake)
  174.             Save.Close()
  175.         End Sub
  176.         Public Sub LoadGame()
  177.             Dim Load As New System.IO.StreamReader("savegame.txt")
  178.             Dim Position As CellReference
  179.             Position.NoOfCellsEast = Val(Chr(Load.Read))
  180.             Position.NoOfCellsSouth = Val(Chr(Load.Read))
  181.             Monster.SetPosition(Position)
  182.             Cavern.PlaceItem(Position, "M")
  183.             Position.NoOfCellsEast = Val(Chr(Load.Read))
  184.             Position.NoOfCellsSouth = Val(Chr(Load.Read))
  185.             Player.SetPosition(Position)
  186.             Cavern.PlaceItem(Position, "*")
  187.             Position.NoOfCellsEast = Val(Chr(Load.Read))
  188.             Position.NoOfCellsSouth = Val(Chr(Load.Read))
  189.             Trap1.SetPosition(Position)
  190.             Position.NoOfCellsEast = Val(Chr(Load.Read))
  191.             Position.NoOfCellsSouth = Val(Chr(Load.Read))
  192.             Trap2.SetPosition(Position)
  193.             Position.NoOfCellsEast = Val(Chr(Load.Read))
  194.             Position.NoOfCellsSouth = Val(Chr(Load.Read))
  195.             Flask.SetPosition(Position)
  196.             Cavern.PlaceItem(Position, "F")
  197.             If Load.ReadLine = "True" Then Trap1.ToggleTrap()
  198.             If Load.ReadLine = "True" Then Trap2.ToggleTrap()
  199.             If Load.ReadLine = "True" Then Monster.ChangeSleepStatus()
  200.             Load.Close()
  201.         End Sub
  202.         Public Function SetPositionOfItem(ByVal Item As Char) As CellReference
  203.             Dim Position As CellReference
  204.             Do
  205.                 Position = GetNewRandomPosition()
  206.             Loop Until Cavern.IsCellEmpty(Position)
  207.             Cavern.PlaceItem(Position, Item)
  208.             Return Position
  209.         End Function
  210.         Public Sub SetUpGame()
  211.             Dim Position As CellReference
  212.             Cavern.Reset()
  213.             If Load Then
  214.                 LoadGame()
  215.             Else
  216.  
  217.                 If Not TrainingGame Then
  218.                     Position.NoOfCellsEast = 0
  219.                     Position.NoOfCellsSouth = 0
  220.                     Player.SetPosition(Position)
  221.                     Cavern.PlaceItem(Position, "*")
  222.                     Trap1.SetPosition(SetPositionOfItem("T"))
  223.                     Trap2.SetPosition(SetPositionOfItem("T"))
  224.                     Monster.SetPosition(SetPositionOfItem("M"))
  225.                     Flask.SetPosition(SetPositionOfItem("F"))
  226.                 Else
  227.                     Position.NoOfCellsEast = 4
  228.                     Position.NoOfCellsSouth = 2
  229.                     Player.SetPosition(Position)
  230.                     Cavern.PlaceItem(Position, "*")
  231.                     Position.NoOfCellsEast = 6
  232.                     Position.NoOfCellsSouth = 2
  233.                     Trap1.SetPosition(Position)
  234.                     Cavern.PlaceItem(Position, "T")
  235.                     Position.NoOfCellsEast = 4
  236.                     Position.NoOfCellsSouth = 3
  237.                     Trap2.SetPosition(Position)
  238.                     Cavern.PlaceItem(Position, "T")
  239.                     Position.NoOfCellsEast = 4
  240.                     Position.NoOfCellsSouth = 0
  241.                     Monster.SetPosition(Position)
  242.                     Cavern.PlaceItem(Position, "M")
  243.                     Position.NoOfCellsEast = 3
  244.                     Position.NoOfCellsSouth = 1
  245.                     Flask.SetPosition(Position)
  246.                     Cavern.PlaceItem(Position, "F")
  247.                 End If
  248.             End If
  249.         End Sub
  250.         Public Function GetNewRandomPosition() As CellReference
  251.             Dim Position As CellReference
  252.             Do
  253.                 Position.NoOfCellsSouth = Int(Rnd() * (NS + 1))
  254.                 Position.NoOfCellsEast = Int(Rnd() * (WE + 1))
  255.             Loop Until Position.NoOfCellsSouth > 0 Or Position.NoOfCellsEast > 0
  256.             Return Position
  257.         End Function
  258.     End Class
  259.     Class Grid
  260.         Private NS As Integer
  261.         Private WE As Integer
  262.         Private CavernState(,) As Char
  263.         Public Sub New(ByVal S As Integer, ByVal E As Integer)
  264.             NS = S
  265.             WE = E
  266.             ReDim CavernState(NS, WE)
  267.         End Sub
  268.         Public Sub Reset()
  269.             Dim Count1 As Integer
  270.             Dim Count2 As Integer
  271.             For Count1 = 0 To NS
  272.                 For Count2 = 0 To WE
  273.                     CavernState(Count1, Count2) = " "
  274.                 Next
  275.             Next
  276.         End Sub
  277.         Public Sub Display(ByVal MonsterAwake As Boolean)
  278.             Dim Count1 As Integer
  279.             Dim Count2 As Integer
  280.             For Count1 = 0 To NS
  281.                 Console.WriteLine(" ------------- ")
  282.  
  283.                 For Count2 = 0 To WE
  284.                     If CavernState(Count1, Count2) = " " Or CavernState(Count1,
  285.                    Count2) = "*" Or (CavernState(Count1, Count2) = "M" And MonsterAwake) Then
  286.                         Console.Write("|" & CavernState(Count1, Count2))
  287.                     Else
  288.                         Console.Write("| ")
  289.                     End If
  290.                 Next
  291.                 Console.WriteLine("|")
  292.             Next
  293.             Console.WriteLine(" ------------- ")
  294.             Console.WriteLine()
  295.         End Sub
  296.         Public Sub PlaceItem(ByVal Position As CellReference, ByVal Item As Char)
  297.             CavernState(Position.NoOfCellsSouth, Position.NoOfCellsEast) = Item
  298.         End Sub
  299.         Public Function IsCellEmpty(ByVal Position As CellReference) As Boolean
  300.             If CavernState(Position.NoOfCellsSouth, Position.NoOfCellsEast) = " " Then
  301.                 Return True
  302.             Else
  303.                 Return False
  304.             End If
  305.         End Function
  306.     End Class
  307.     Class Enemy
  308.         Inherits Item
  309.         Private Awake As Boolean
  310.         Public Overridable Sub MakeMove(ByVal PlayerPosition As CellReference)
  311.             If NoOfCellsSouth < PlayerPosition.NoOfCellsSouth Then
  312.                 NoOfCellsSouth = NoOfCellsSouth + 1
  313.             ElseIf NoOfCellsSouth > PlayerPosition.NoOfCellsSouth Then
  314.                 NoOfCellsSouth = NoOfCellsSouth - 1
  315.             ElseIf NoOfCellsEast < PlayerPosition.NoOfCellsEast Then
  316.                 NoOfCellsEast = NoOfCellsEast + 1
  317.             Else
  318.                 NoOfCellsEast = NoOfCellsEast - 1
  319.             End If
  320.         End Sub
  321.         Public Function GetAwake() As Boolean
  322.             Return Awake
  323.         End Function
  324.         Public Overridable Sub ChangeSleepStatus()
  325.             If Awake Then
  326.                 Awake = False
  327.             Else
  328.                 Awake = True
  329.             End If
  330.         End Sub
  331.         Public Sub New()
  332.             Awake = False
  333.         End Sub
  334.     End Class
  335.  
  336.     Class Character
  337.         Inherits Item
  338.         Public Sub MakeMove(ByVal Direction As Char)
  339.             Select Case Direction
  340.                 Case "N"
  341.                     NoOfCellsSouth = NoOfCellsSouth - 1
  342.                 Case "S"
  343.                     NoOfCellsSouth = NoOfCellsSouth + 1
  344.                 Case "W"
  345.                     NoOfCellsEast = NoOfCellsEast - 1
  346.                 Case "E"
  347.                     NoOfCellsEast = NoOfCellsEast + 1
  348.             End Select
  349.         End Sub
  350.     End Class
  351.     Class Trap
  352.         Inherits Item
  353.         Private Triggered As Boolean
  354.         Public Function GetTriggered() As Boolean
  355.             Return Triggered
  356.         End Function
  357.         Public Sub New()
  358.             Triggered = False
  359.         End Sub
  360.         Public Sub ToggleTrap()
  361.             Triggered = Not Triggered
  362.         End Sub
  363.     End Class
  364.     Class Item
  365.         Protected NoOfCellsEast As Integer
  366.         Protected NoOfCellsSouth As Integer
  367.         Public Function GetPosition() As CellReference
  368.             Dim Position As CellReference
  369.             Position.NoOfCellsEast = NoOfCellsEast
  370.             Position.NoOfCellsSouth = NoOfCellsSouth
  371.             Return Position
  372.         End Function
  373.         Public Sub SetPosition(ByVal Position As CellReference)
  374.             NoOfCellsEast = Position.NoOfCellsEast
  375.             NoOfCellsSouth = Position.NoOfCellsSouth
  376.         End Sub
  377.         Public Function CheckIfSameCell(ByVal Position As CellReference) As Boolean
  378.             If NoOfCellsEast = Position.NoOfCellsEast And NoOfCellsSouth =
  379.            Position.NoOfCellsSouth Then
  380.                 Return True
  381.             Else
  382.                 Return False
  383.             End If
  384.         End Function
  385.     End Class
  386. End Module
Advertisement
Add Comment
Please, Sign In to add comment