Lynix_

Monster Code MK II

Jun 11th, 2018
106
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 = 6
  40.         Const WE As Integer = 8
  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 T to Teleport")
  123.             Console.WriteLine("Enter M to return to the Main Menu")
  124.             Console.WriteLine()
  125.         End Sub
  126.         Public Function GetMove() As Char
  127.             Dim Move As Char
  128.             Move = Console.ReadLine
  129.             Console.WriteLine()
  130.             Return Move
  131.         End Function
  132.         Public Sub DisplayWonGameMessage()
  133.             Console.WriteLine("Well done! You have found the flask containing the Styxian potion.")
  134.             Console.WriteLine("You have won the game of MONSTER!")
  135.             Console.WriteLine()
  136.         End Sub
  137.         Public Sub DisplayTrapMessage()
  138.             Console.WriteLine("On no! You have set off a trap. Watch out, the monster is now awake!")
  139.             Console.WriteLine()
  140.         End Sub
  141.         Public Sub DisplayLostGameMessage()
  142.             Console.WriteLine("ARGHHHHHH! The monster has eaten you. GAMEOVER.")
  143.             Console.WriteLine("Maybe you will have better luck next time you play MONSTER! ")
  144.             Console.WriteLine()
  145.         End Sub
  146.         Public Function CheckValidMove(ByVal Direction As Char) As Boolean
  147.             Dim ValidMove As Boolean
  148.             Dim Position As CellReference
  149.             ValidMove = True
  150.             If Not (Direction = "N" Or Direction = "S" Or Direction = "W" Or
  151.            Direction = "E" Or Direction = "M" Or Direction = "Q" Or Direction = "T") Then
  152.                 ValidMove = False
  153.             End If
  154.             If Direction = "E" And Player.GetPosition.NoOfCellsEast = 8 Then ValidMove = False
  155.             If Direction = "W" And Player.GetPosition.NoOfCellsEast = 0 Then ValidMove = False
  156.             If Direction = "S" And Player.GetPosition.NoOfCellsSouth = 6 Then ValidMove = False
  157.             If Direction = "N" And Player.GetPosition.NoOfCellsSouth = 0 Then ValidMove = False
  158.             If Direction = "Q" Then SaveGame()
  159.             If Direction = "T" Then
  160.                 Position = Player.GetPosition
  161.                 Cavern.PlaceItem(Position, " ")
  162.                 Position = GetNewRandomPosition()
  163.                 Player.SetPosition(Position)
  164.             End If
  165.             Return ValidMove
  166.         End Function
  167.         Public Sub SaveGame()
  168.             Dim Save As New System.IO.StreamWriter("savegame.txt")
  169.             Save.Write(Monster.GetPosition.NoOfCellsEast)
  170.             Save.Write(Monster.GetPosition.NoOfCellsSouth)
  171.             Save.Write(Player.GetPosition.NoOfCellsEast)
  172.             Save.Write(Player.GetPosition.NoOfCellsSouth)
  173.             Save.Write(Trap1.GetPosition.NoOfCellsEast)
  174.             Save.Write(Trap1.GetPosition.NoOfCellsSouth)
  175.             Save.Write(Trap2.GetPosition.NoOfCellsEast)
  176.             Save.Write(Trap2.GetPosition.NoOfCellsSouth)
  177.             Save.Write(Flask.GetPosition.NoOfCellsEast)
  178.             Save.Write(Flask.GetPosition.NoOfCellsSouth)
  179.             Save.WriteLine(Trap1.GetTriggered)
  180.             Save.WriteLine(Trap2.GetTriggered)
  181.             Save.WriteLine(Monster.GetAwake)
  182.             Save.Close()
  183.         End Sub
  184.         Public Sub LoadGame()
  185.             Dim Load As New System.IO.StreamReader("savegame.txt")
  186.             Dim Position As CellReference
  187.             Position.NoOfCellsEast = Val(Chr(Load.Read))
  188.             Position.NoOfCellsSouth = Val(Chr(Load.Read))
  189.             Monster.SetPosition(Position)
  190.             Cavern.PlaceItem(Position, "M")
  191.             Position.NoOfCellsEast = Val(Chr(Load.Read))
  192.             Position.NoOfCellsSouth = Val(Chr(Load.Read))
  193.             Player.SetPosition(Position)
  194.             Cavern.PlaceItem(Position, "*")
  195.             Position.NoOfCellsEast = Val(Chr(Load.Read))
  196.             Position.NoOfCellsSouth = Val(Chr(Load.Read))
  197.             Trap1.SetPosition(Position)
  198.             Position.NoOfCellsEast = Val(Chr(Load.Read))
  199.             Position.NoOfCellsSouth = Val(Chr(Load.Read))
  200.             Trap2.SetPosition(Position)
  201.             Position.NoOfCellsEast = Val(Chr(Load.Read))
  202.             Position.NoOfCellsSouth = Val(Chr(Load.Read))
  203.             Flask.SetPosition(Position)
  204.             Cavern.PlaceItem(Position, "F")
  205.             If Load.ReadLine = "True" Then Trap1.ToggleTrap()
  206.             If Load.ReadLine = "True" Then Trap2.ToggleTrap()
  207.             If Load.ReadLine = "True" Then Monster.ChangeSleepStatus()
  208.             Load.Close()
  209.         End Sub
  210.         Public Function SetPositionOfItem(ByVal Item As Char) As CellReference
  211.             Dim Position As CellReference
  212.             Do
  213.                 Position = GetNewRandomPosition()
  214.             Loop Until Cavern.IsCellEmpty(Position)
  215.             Cavern.PlaceItem(Position, Item)
  216.             Return Position
  217.         End Function
  218.         Public Sub SetUpGame()
  219.             Dim Position As CellReference
  220.             Cavern.Reset()
  221.             If Load Then
  222.                 LoadGame()
  223.             Else
  224.  
  225.                 If Not TrainingGame Then
  226.                     Position.NoOfCellsEast = 0
  227.                     Position.NoOfCellsSouth = 0
  228.                     Player.SetPosition(Position)
  229.                     Cavern.PlaceItem(Position, "*")
  230.                     Trap1.SetPosition(SetPositionOfItem("T"))
  231.                     Trap2.SetPosition(SetPositionOfItem("T"))
  232.                     Monster.SetPosition(SetPositionOfItem("M"))
  233.                     Flask.SetPosition(SetPositionOfItem("F"))
  234.                 Else
  235.                     Position.NoOfCellsEast = 4
  236.                     Position.NoOfCellsSouth = 2
  237.                     Player.SetPosition(Position)
  238.                     Cavern.PlaceItem(Position, "*")
  239.                     Position.NoOfCellsEast = 6
  240.                     Position.NoOfCellsSouth = 2
  241.                     Trap1.SetPosition(Position)
  242.                     Cavern.PlaceItem(Position, "T")
  243.                     Position.NoOfCellsEast = 4
  244.                     Position.NoOfCellsSouth = 3
  245.                     Trap2.SetPosition(Position)
  246.                     Cavern.PlaceItem(Position, "T")
  247.                     Position.NoOfCellsEast = 4
  248.                     Position.NoOfCellsSouth = 0
  249.                     Monster.SetPosition(Position)
  250.                     Cavern.PlaceItem(Position, "M")
  251.                     Position.NoOfCellsEast = 3
  252.                     Position.NoOfCellsSouth = 1
  253.                     Flask.SetPosition(Position)
  254.                     Cavern.PlaceItem(Position, "F")
  255.                 End If
  256.             End If
  257.         End Sub
  258.         Public Function GetNewRandomPosition() As CellReference
  259.             Dim Position As CellReference
  260.             Do
  261.                 Position.NoOfCellsSouth = Int(Rnd() * (NS + 1))
  262.                 Position.NoOfCellsEast = Int(Rnd() * (WE + 1))
  263.             Loop Until Position.NoOfCellsSouth > 0 Or Position.NoOfCellsEast > 0
  264.             Return Position
  265.         End Function
  266.     End Class
  267.     Class Grid
  268.         Private NS As Integer
  269.         Private WE As Integer
  270.         Private CavernState(,) As Char
  271.         Public Sub New(ByVal S As Integer, ByVal E As Integer)
  272.             NS = S
  273.             WE = E
  274.             ReDim CavernState(NS, WE)
  275.         End Sub
  276.         Public Sub Reset()
  277.             Dim Count1 As Integer
  278.             Dim Count2 As Integer
  279.             For Count1 = 0 To NS
  280.                 For Count2 = 0 To WE
  281.                     CavernState(Count1, Count2) = " "
  282.                 Next
  283.             Next
  284.         End Sub
  285.         Public Sub Display(ByVal MonsterAwake As Boolean)
  286.             Dim Count1 As Integer
  287.             Dim Count2 As Integer
  288.             For Count1 = 0 To NS
  289.                 Console.WriteLine(" ----------------- ")
  290.  
  291.                 For Count2 = 0 To WE
  292.                     If CavernState(Count1, Count2) = " " Or CavernState(Count1,
  293.                    Count2) = "*" Or (CavernState(Count1, Count2) = "M" And MonsterAwake) Then
  294.                         Console.Write("|" & CavernState(Count1, Count2))
  295.                     Else
  296.                         Console.Write("| ")
  297.                     End If
  298.                 Next
  299.                 Console.WriteLine("|")
  300.             Next
  301.             Console.WriteLine(" ----------------- ")
  302.             Console.WriteLine()
  303.         End Sub
  304.         Public Sub PlaceItem(ByVal Position As CellReference, ByVal Item As Char)
  305.             CavernState(Position.NoOfCellsSouth, Position.NoOfCellsEast) = Item
  306.         End Sub
  307.         Public Function IsCellEmpty(ByVal Position As CellReference) As Boolean
  308.             If CavernState(Position.NoOfCellsSouth, Position.NoOfCellsEast) = " " Then
  309.                 Return True
  310.             Else
  311.                 Return False
  312.             End If
  313.         End Function
  314.     End Class
  315.     Class Enemy
  316.         Inherits Item
  317.         Private Awake As Boolean
  318.         Public Overridable Sub MakeMove(ByVal PlayerPosition As CellReference)
  319.             If NoOfCellsSouth < PlayerPosition.NoOfCellsSouth Then
  320.                 NoOfCellsSouth = NoOfCellsSouth + 1
  321.             ElseIf NoOfCellsSouth > PlayerPosition.NoOfCellsSouth Then
  322.                 NoOfCellsSouth = NoOfCellsSouth - 1
  323.             ElseIf NoOfCellsEast < PlayerPosition.NoOfCellsEast Then
  324.                 NoOfCellsEast = NoOfCellsEast + 1
  325.             Else
  326.                 NoOfCellsEast = NoOfCellsEast - 1
  327.             End If
  328.         End Sub
  329.         Public Function GetAwake() As Boolean
  330.             Return Awake
  331.         End Function
  332.         Public Overridable Sub ChangeSleepStatus()
  333.             If Awake Then
  334.                 Awake = False
  335.             Else
  336.                 Awake = True
  337.             End If
  338.         End Sub
  339.         Public Sub New()
  340.             Awake = False
  341.         End Sub
  342.     End Class
  343.  
  344.     Class Character
  345.         Inherits Item
  346.         Public Sub MakeMove(ByVal Direction As Char)
  347.             Select Case Direction
  348.                 Case "N"
  349.                     NoOfCellsSouth = NoOfCellsSouth - 1
  350.                 Case "S"
  351.                     NoOfCellsSouth = NoOfCellsSouth + 1
  352.                 Case "W"
  353.                     NoOfCellsEast = NoOfCellsEast - 1
  354.                 Case "E"
  355.                     NoOfCellsEast = NoOfCellsEast + 1
  356.             End Select
  357.         End Sub
  358.     End Class
  359.     Class Trap
  360.         Inherits Item
  361.         Private Triggered As Boolean
  362.         Public Function GetTriggered() As Boolean
  363.             Return Triggered
  364.         End Function
  365.         Public Sub New()
  366.             Triggered = False
  367.         End Sub
  368.         Public Sub ToggleTrap()
  369.             Triggered = Not Triggered
  370.         End Sub
  371.     End Class
  372.     Class Item
  373.         Protected NoOfCellsEast As Integer
  374.         Protected NoOfCellsSouth As Integer
  375.         Public Function GetPosition() As CellReference
  376.             Dim Position As CellReference
  377.             Position.NoOfCellsEast = NoOfCellsEast
  378.             Position.NoOfCellsSouth = NoOfCellsSouth
  379.             Return Position
  380.         End Function
  381.         Public Sub SetPosition(ByVal Position As CellReference)
  382.             NoOfCellsEast = Position.NoOfCellsEast
  383.             NoOfCellsSouth = Position.NoOfCellsSouth
  384.         End Sub
  385.         Public Function CheckIfSameCell(ByVal Position As CellReference) As Boolean
  386.             If NoOfCellsEast = Position.NoOfCellsEast And NoOfCellsSouth =
  387.            Position.NoOfCellsSouth Then
  388.                 Return True
  389.             Else
  390.                 Return False
  391.             End If
  392.         End Function
  393.     End Class
  394. End Module
Advertisement
Add Comment
Please, Sign In to add comment