Advertisement
joemch

mONSTER WITH lOADbUG

May 18th, 2020
1,261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 16.21 KB | None | 0 0
  1. Imports MONSTER
  2. Imports System.IO
  3. Module MonsterOOP
  4.     Sub Main()
  5.  
  6.         Console.WriteLine("================OBJECT ORIENTED VERSION==========")
  7.         Dim Choice As Integer = 0
  8.         While Choice <> 9
  9.             DisplayMenu()
  10.             Choice = GetMainMenuChoice()
  11.             Select Case Choice
  12.                 Case 1
  13.                     Dim MyGame As New Game(False, False)
  14.                 Case 2
  15.                     Dim MyGame As New Game(True, False)
  16.                 Case 3
  17.                     Dim mygame As New Game(False, True)
  18.  
  19.             End Select
  20.         End While
  21.  
  22.  
  23.     End Sub
  24.     Public Sub DisplayMenu()
  25.         Console.WriteLine("MAIN MENU")
  26.         Console.WriteLine()
  27.         Console.WriteLine("1. Start new game")
  28.         Console.WriteLine("2. Play training game")
  29.         Console.WriteLine("3. Load Game")
  30.         Console.WriteLine("9. Quit")
  31.         Console.WriteLine()
  32.         Console.Write("Please enter your choice: ")
  33.     End Sub
  34.     Public Function GetMainMenuChoice() As Integer
  35.         Dim Choice As Integer
  36.         Choice = CInt(Console.ReadLine())
  37.         Console.WriteLine()
  38.         Return Choice
  39.     End Function
  40.     Structure CellReference
  41.         Dim NoOfCellsEast As Integer
  42.         Dim NoOfCellsSouth As Integer
  43.     End Structure
  44.     Class Game
  45.         Const NS As Integer = 4
  46.         Const WE As Integer = 6
  47.         Private Player As New Character
  48.         Private Cavern As New Grid(NS, WE)
  49.         Private Monster As New Enemy
  50.         Private Flask As New Item
  51.         Private Trap1 As New Trap
  52.         Private Trap2 As New Trap
  53.         Private TrainingGame As Boolean
  54.         Private IsLoad As Boolean
  55.         Public Sub New(ByVal IsATrainingGame As Boolean, ByVal isLoad As Boolean)
  56.             TrainingGame = IsATrainingGame
  57.             Me.IsLoad = isLoad
  58.             Randomize()
  59.             SetUpGame()
  60.             Play()
  61.         End Sub
  62.         Public Sub save()
  63.             Dim bw As New BinaryWriter(File.Open("game1.gm", FileMode.OpenOrCreate))
  64.             ' TrapPositions
  65.             bw.Write(Trap1.GetPosition.NoOfCellsEast)
  66.             bw.Write(Trap1.GetPosition.NoOfCellsSouth)
  67.             bw.Write(Trap2.GetPosition.NoOfCellsEast)
  68.             bw.Write(Trap2.GetPosition.NoOfCellsSouth)
  69.             ' = MonsterPosition
  70.             bw.Write(Monster.GetPosition.NoOfCellsEast)
  71.             bw.Write(Monster.GetPosition.NoOfCellsSouth)
  72.             ' = PlayerPosition
  73.             bw.Write(Player.GetPosition.NoOfCellsEast)
  74.             bw.Write(Player.GetPosition.NoOfCellsSouth)
  75.             ' = FlaskPosition
  76.             bw.Write(Flask.GetPosition.NoOfCellsEast)
  77.             bw.Write(Flask.GetPosition.NoOfCellsSouth)
  78.             ' = MonsterAwake
  79.             bw.Write(Monster.GetAwake)
  80.             bw.Close()
  81.         End Sub
  82.         Public Sub load()
  83.             Dim bw As New BinaryReader(File.Open("game1.gm", FileMode.Open))
  84.             Dim tPosition As CellReference
  85.             ' TrapPositions
  86.             tPosition.NoOfCellsEast = bw.Read()
  87.             tPosition.NoOfCellsSouth = bw.Read()
  88.             Trap1.SetPosition(tPosition)
  89.             Cavern.PlaceItem(tPosition, "T")
  90.             tPosition.NoOfCellsEast = bw.Read()
  91.             tPosition.NoOfCellsSouth = bw.Read()
  92.             Trap2.SetPosition(tPosition)
  93.             Cavern.PlaceItem(tPosition, "T")
  94.             ' = MonsterPosition
  95.             tPosition.NoOfCellsEast = bw.Read()
  96.             tPosition.NoOfCellsSouth = bw.Read()
  97.             Monster.SetPosition(tPosition)
  98.             Cavern.PlaceItem(tPosition, "M")
  99.             ' = PlayerPosition
  100.             tPosition.NoOfCellsEast = bw.Read()
  101.             tPosition.NoOfCellsSouth = bw.Read()
  102.             Player.SetPosition(tPosition)
  103.             Cavern.PlaceItem(tPosition, "*")
  104.             ' = FlaskPosition
  105.             tPosition.NoOfCellsEast = bw.Read()
  106.             tPosition.NoOfCellsSouth = bw.Read()
  107.             Flask.SetPosition(tPosition)
  108.             Cavern.PlaceItem(tPosition, "F")
  109.             ' = MonsterAwake
  110.             Monster.SetAwake(bw.Read())
  111.         End Sub
  112.         Public Sub Play()
  113.             Dim Count As Integer
  114.             Dim Eaten As Boolean
  115.             Dim FlaskFound As Boolean
  116.             Dim MoveDirection As Char
  117.             Dim ValidMove As Boolean
  118.             Dim Position As CellReference
  119.             Eaten = False
  120.             FlaskFound = False
  121.             Cavern.Display(Monster.GetAwake)
  122.             Do
  123.                 Do
  124.                     DisplayMoveOptions()
  125.                     MoveDirection = GetMove()
  126.                     ValidMove = CheckValidMove(MoveDirection)
  127.                 Loop Until ValidMove
  128.                 If MoveDirection <> "M" Or MoveDirection <> "F" Then
  129.                     Cavern.PlaceItem(Player.GetPosition, " ")
  130.                     Player.MakeMove(MoveDirection)
  131.                     Cavern.PlaceItem(Player.GetPosition, "*")
  132.                     Cavern.Display(Monster.GetAwake)
  133.                     FlaskFound = Player.CheckIfSameCell(Flask.GetPosition)
  134.                     If FlaskFound Then
  135.                         DisplayWonGameMessage()
  136.                     End If
  137.                     Eaten = Monster.CheckIfSameCell(Player.GetPosition)
  138.                     'This selection structure checks to see if the player has triggered one of the traps in the cavern
  139.                     If Not Monster.GetAwake And Not FlaskFound And Not Eaten And
  140.                        (Player.CheckIfSameCell(Trap1.GetPosition) And Not Trap1.GetTriggered Or
  141.                        Player.CheckIfSameCell(Trap2.GetPosition) And Not Trap2.GetTriggered) Then
  142.                         Monster.ChangeSleepStatus()
  143.                         DisplayTrapMessage()
  144.                         Cavern.Display(Monster.GetAwake)
  145.                     End If
  146.                     If Monster.GetAwake And Not Eaten And Not FlaskFound Then
  147.                         Count = 0
  148.                         Do
  149.                             Cavern.PlaceItem(Monster.GetPosition, " ")
  150.                             Position = Monster.GetPosition
  151.                             Monster.MakeMove(Player.GetPosition)
  152.                             Cavern.PlaceItem(Monster.GetPosition, "M")
  153.                             If Monster.CheckIfSameCell(Flask.GetPosition) Then
  154.                                 Flask.SetPosition(Position)
  155.                                 Cavern.PlaceItem(Position, "F")
  156.                             End If
  157.                             Eaten = Monster.CheckIfSameCell(Player.GetPosition)
  158.                             Console.WriteLine()
  159.                             Console.WriteLine("Press Enter key to continue")
  160.                             Console.ReadLine()
  161.                             Cavern.Display(Monster.GetAwake)
  162.                             Count = Count + 1
  163.                         Loop Until Count = 2 Or Eaten
  164.                     End If
  165.                     If Eaten Then
  166.                         DisplayLostGameMessage()
  167.                     End If
  168.                 End If
  169.                 If MoveDirection = "F" Then
  170.                     save()
  171.                 End If
  172.             Loop Until Eaten Or FlaskFound Or MoveDirection = "M"
  173.         End Sub
  174.         Public Sub DisplayMoveOptions()
  175.             Console.WriteLine()
  176.             Console.WriteLine("Enter N to move NORTH")
  177.             Console.WriteLine("Enter S to move SOUTH")
  178.             Console.WriteLine("Enter E to move EAST")
  179.             Console.WriteLine("Enter W to move WEST")
  180.             Console.WriteLine("Enter M to return to the Main Menu")
  181.             Console.WriteLine()
  182.         End Sub
  183.         Public Function GetMove() As Char
  184.             Dim Move As Char
  185.             Move = Console.ReadLine
  186.             Console.WriteLine()
  187.             Return Move
  188.         End Function
  189.         Public Sub DisplayWonGameMessage()
  190.             Console.WriteLine("Well done! You have found the flask containing the Styxian potion.")
  191.             Console.WriteLine("You have won the game of MONSTER!")
  192.             Console.WriteLine()
  193.         End Sub
  194.         Public Sub DisplayTrapMessage()
  195.             Console.WriteLine("On no! You have set off a trap. Watch out, the monster is now awake!")
  196.             Console.WriteLine()
  197.         End Sub
  198.         Public Sub DisplayLostGameMessage()
  199.             Console.WriteLine("ARGHHHHHH! The monster has eaten you. GAMEOVER.")
  200.             Console.WriteLine("Maybe you will have better luck next time you play MONSTER! ")
  201.             Console.WriteLine()
  202.         End Sub
  203.         Public Function CheckValidMove(ByVal Direction As Char) As Boolean
  204.             Dim ValidMove As Boolean
  205.             ValidMove = True
  206.             If Not (Direction = "N" Or Direction = "S" Or Direction = "W" Or
  207.                Direction = "E" Or Direction = "M" Or Direction = "F") Then
  208.                 ValidMove = False
  209.             End If
  210.             Return ValidMove
  211.         End Function
  212.         Public Function SetPositionOfItem(ByVal Item As Char) As CellReference
  213.             Dim Position As CellReference
  214.             Do
  215.                 Position = GetNewRandomPosition()
  216.             Loop Until Cavern.IsCellEmpty(Position)
  217.             Cavern.PlaceItem(Position, Item)
  218.             Return Position
  219.         End Function
  220.         Public Sub SetUpGame()
  221.             Dim Position As CellReference
  222.             Cavern.Reset()
  223.             If Not TrainingGame And Not isLoad Then
  224.                 Position.NoOfCellsEast = 0
  225.                 Position.NoOfCellsSouth = 0
  226.                 Player.SetPosition(Position)
  227.                 Cavern.PlaceItem(Position, "*")
  228.                 Trap1.SetPosition(SetPositionOfItem("T"))
  229.                 Trap2.SetPosition(SetPositionOfItem("T"))
  230.                 Cavern.PlaceItem(Position, "*")
  231.                 Monster.SetPosition(SetPositionOfItem("M"))
  232.                 Flask.SetPosition(SetPositionOfItem("F"))
  233.             ElseIf TrainingGame And Not isLoad Then
  234.                 Position.NoOfCellsEast = 4
  235.                 Position.NoOfCellsSouth = 2
  236.                 Player.SetPosition(Position)
  237.                 Cavern.PlaceItem(Position, "*")
  238.                 Position.NoOfCellsEast = 6
  239.                 Position.NoOfCellsSouth = 2
  240.                 Trap1.SetPosition(Position)
  241.                 Cavern.PlaceItem(Position, "T")
  242.                 Position.NoOfCellsEast = 4
  243.                 Position.NoOfCellsSouth = 3
  244.                 Trap2.SetPosition(Position)
  245.                 Cavern.PlaceItem(Position, "T")
  246.                 Position.NoOfCellsEast = 4
  247.                 Position.NoOfCellsSouth = 0
  248.                 Monster.SetPosition(Position)
  249.                 Cavern.PlaceItem(Position, "M")
  250.                 Position.NoOfCellsEast = 3
  251.                 Position.NoOfCellsSouth = 1
  252.                 Flask.SetPosition(Position)
  253.                 Cavern.PlaceItem(Position, "F")
  254.             ElseIf Not TrainingGame And isLoad Then
  255.                 load()
  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.         Sub SetAwake(awake As Boolean)
  340.             Me.Awake = awake
  341.         End Sub
  342.         Public Sub New()
  343.             Awake = False
  344.         End Sub
  345.     End Class
  346.     Class Boss
  347.         Inherits Enemy
  348.         Public Sub New()
  349.  
  350.         End Sub
  351.         Public Overrides Sub ChangeSleepStatus()
  352.             MyBase.ChangeSleepStatus()
  353.         End Sub
  354.         Public Overrides Sub MakeMove(PlayerPosition As CellReference)
  355.             'MyBase.MakeMove(PlayerPosition)
  356.             NoOfCellsEast = NoOfCellsEast - 1
  357.         End Sub
  358.     End Class
  359.     Class Character
  360.         Inherits Item
  361.         Public Sub MakeMove(ByVal Direction As Char)
  362.             Select Case Direction
  363.                 Case "N"
  364.                     NoOfCellsSouth = NoOfCellsSouth - 1
  365.                 Case "S"
  366.                     NoOfCellsSouth = NoOfCellsSouth + 1
  367.                 Case "W"
  368.                     NoOfCellsEast = NoOfCellsEast - 1
  369.                 Case "E"
  370.                     NoOfCellsEast = NoOfCellsEast + 1
  371.  
  372.             End Select
  373.         End Sub
  374.     End Class
  375.     Class Trap
  376.         Inherits Item
  377.         Private Triggered As Boolean
  378.         Public Function GetTriggered() As Boolean
  379.             Return Triggered
  380.         End Function
  381.         Public Sub New()
  382.             Triggered = False
  383.         End Sub
  384.         Public Sub ToggleTrap()
  385.             Triggered = Not Triggered
  386.         End Sub
  387.     End Class
  388.     Class Item
  389.         Protected NoOfCellsEast As Integer
  390.         Protected NoOfCellsSouth As Integer
  391.         Public Function GetPosition() As CellReference
  392.             Dim Position As CellReference
  393.             Position.NoOfCellsEast = NoOfCellsEast
  394.             Position.NoOfCellsSouth = NoOfCellsSouth
  395.             Return Position
  396.         End Function
  397.         Public Sub SetPosition(ByVal Position As CellReference)
  398.             NoOfCellsEast = Position.NoOfCellsEast
  399.             NoOfCellsSouth = Position.NoOfCellsSouth
  400.         End Sub
  401.         Public Function CheckIfSameCell(ByVal Position As CellReference) As Boolean
  402.             If NoOfCellsEast = Position.NoOfCellsEast And NoOfCellsSouth =
  403.                Position.NoOfCellsSouth Then
  404.                 Return True
  405.             Else
  406.                 Return False
  407.             End If
  408.         End Function
  409.     End Class
  410.  
  411. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement