[RESOLVED] Looking for a way to read specific lines in my program.-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] Looking for a way to read specific lines in my program.

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2017
    Posts
    6

    Resolved [RESOLVED] Looking for a way to read specific lines in my program.

    I am not really a knowledgeable or experienced programmer, but I know some of the basics. I am working on a game, but the way I am writing it requires the computer to read lines that it has already skipped over. Here is a snippet of the code:
    Code:
     If HelpScreen3 = 1 Then
                Console.Clear()
                Console.WriteLine("In the game you will always have two, three, or four choices to make.")
                Console.WriteLine("Each choice can affect things that happen later in the game, so choose wisely.")
                Console.WriteLine("Your goal is to escape, and stay alive.")
                Console.WriteLine("")
                Console.WriteLine("Here are the commands you can type:")
                Console.WriteLine("")
                Console.WriteLine("")
                Console.WriteLine("'help' brings up this screen. (The help screen)")
                Console.WriteLine("'inventory' shows what you are carrying in the game.")
                Console.WriteLine("'map' shows a map of what you have explored.")
                Console.WriteLine("'exit' will close the game. Be warned, there is currently no saving.")
                Console.WriteLine("")
                Console.WriteLine("")
                Console.WriteLine("Type 'return' to continue the came.")
                Console.WriteLine("")
                Do Until Stage1 = 1 Or Stage2 = 1 Or Stage3 = 1
                    Dim enteredText As String = Console.ReadLine()
                    If enteredText = "return" Then
                        If StageNumber = 1 Then
                            Stage1 = 1
                            HelpScreen3 = 0
                        End If
                        If StageNumber = 2 Then
                            Stage2 = 1
                            HelpScreen3 = 0
                        End If
                        If StageNumber = 3 Then
                            Stage3 = 1
                            HelpScreen3 = 0
                        End If
                        If StageNumber = 4 Then
                            Stage4 = 1
                            HelpScreen3 = 0
                        End If
                        If StageNumber = 5 Then
                            Stage5 = 1
                            HelpScreen3 = 0
                        End If
                        If StageNumber = 6 Then
                            Stage6 = 1
                            HelpScreen3 = 0
                        End If
                    Else
                        Console.WriteLine("")
                        Console.WriteLine("This command does not exist!")
                        Console.WriteLine("")
                    End If
                Loop
            End If
    
    
    
    
    
            If Stage1 = 1 Then
                StageNumber = 1
                Console.Clear()
                Console.WriteLine("You awake inside of a cold and very dark room.")
                Console.WriteLine("As you come to your senses, the room appears lighter.")
                Console.WriteLine("The moonlight shines bright through a broken window to your right.")
                Console.WriteLine("It appears as though you are in your bedroom, but something happened there.")
                Console.WriteLine("Someone looks to have been in your house.")
                Console.WriteLine("You see a closed door in front of you.")
                Console.WriteLine("There is a drawer to your left.")
                Console.WriteLine("A wall stands behind you.")
                Console.WriteLine("")
                Console.WriteLine("")
                Console.WriteLine("'1' to climb out the window.")
                Console.WriteLine("'2' to open the door.")
                Console.WriteLine("'3' to check the drawer.")
                Console.WriteLine("'4' to examine the wall.")
                Console.WriteLine("")
                Do Until Stage2 = 1 Or Stage3 = 1 Or Stage1Drawer = 1 Or Stage1Wall = 1 Or HelpScreen3 = 1 Or Inventory = 1 Or Map = 1
                    Dim enteredText As String = Console.ReadLine()
                    If enteredText = "1" Then
                        Stage2 = 1
                        Stage1 = 0
                    ElseIf enteredText = "2" Then
                        If Stage1DoorKey = 1 Then
                            Stage3 = 1
                            Stage1 = 0
                        ElseIf Stage1DoorKey = 0 Then
                            Stage1Door = 1
                            Stage1 = 0
                        End If
                    ElseIf enteredText = "3" Then
                        Stage1Drawer = 1
                        Stage1 = 0
                    ElseIf enteredText = "4" Then
                        Stage1Wall = 1
                        Stage1 = 0
                    ElseIf enteredText = "help" Then
                        HelpScreen3 = 1
                        Stage1 = 0
                    ElseIf enteredText = "inventory" Then
                        Inventory = 1
                        Stage1 = 0
                    ElseIf enteredText = "map" Then
                        Map = 1
                        Stage1 = 0
                    ElseIf enteredText = "exit" Then
                        Environment.Exit(0)
                    Else
                        Console.WriteLine("")
                        Console.WriteLine("This command does not exist!")
                        Console.WriteLine("")
                    End If
                Loop
            End If

    Now as you can see, there are 2 main chunks of code in this part. In the game, the computer skips over the first chunk and reads the second chunk. That is what is supposed to happen. What I want to happen is when you type 'help' it will read the first chunk, but this only works if the first chunk is under the second chunk. Makes sense since the computer reads downward, but it is required for the first chunk to stay where it is. I cant flip flop the chunks or else the program wont work for the way I a writing it.

    So, is there anything I can put inside of HERE, that will allow me to read the start of chunk number 1. Remember that chunk 1 is not the start of my program.:
    Code:
     If enteredText = "1" Then
                        Stage2 = 1
                        Stage1 = 0
                    ElseIf enteredText = "2" Then
                        If Stage1DoorKey = 1 Then
                            Stage3 = 1
                            Stage1 = 0
                        ElseIf Stage1DoorKey = 0 Then
                            Stage1Door = 1
                            Stage1 = 0
                        End If
                    ElseIf enteredText = "3" Then
                        Stage1Drawer = 1
                        Stage1 = 0
                    ElseIf enteredText = "4" Then
                        Stage1Wall = 1
                        Stage1 = 0
                    ElseIf enteredText = "help" Then
                        HelpScreen3 = 1
                        Stage1 = 0
    
                           (HERE)
    
                    ElseIf enteredText = "inventory" Then
                        Inventory = 1
                        Stage1 = 0
                    ElseIf enteredText = "map" Then
                        Map = 1
                        Stage1 = 0
                    ElseIf enteredText = "exit" Then
                        Environment.Exit(0)

    Thank you for any help!
    Last edited by Shaggy Hiker; May 16th, 2017 at 05:01 PM. Reason: Added CODE tags.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    31,118

    Re: Looking for a way to read specific lines in my program.

    I edited the post to add [CODE][/CODE] tags, which improves the readability of the code. You can do this yourself by pressing the # button and pasting the code between the resulting tags, or paste in the code, highlight it, then press the # button.

    One other quick point unrelated to your question: Use OrElse and AndAlso in place of Or and And. The reason is subtle, and will make no noticeable difference in this case, but it's a good habit to get into. With Or, all parts of the condition are evaluated, even if the truth of the whole statement is already known. For example, you have three conditions ORed together. If the first one is True, then the other two are irrelevant, but they will still be evaluated. With OrElse, evaluation stops as soon as the truth of the whole statement is known, so it's more efficient.

    I wouldn't say that the computer reads the code line by line. The compiler turns it into something else, and that gets executed, but then it really does get executed line by line, so the program executes line by line after a fashion. You already have a couple loops in there, in which case, when execution hits the Loop statement, it returns to the first statement in the loop, so you are going back to where you were before, which is kind of what you want to be doing. Building it up using loops, though, is going to melt your mind in short order. Get enough of those loops, and you'll be loopier than your code.

    What you really need is to break things out into functions. The way I would write this would be to have some kind of structure that looked kind of like this:
    Code:
    'Outside any method:
    Public theState As Integer = 1
    
    
    'Inside the main method.
    
    Do While theState >0
     Select Case theState
        Case 1
         Introduction()
        Case 2
          ShowHelp()
        Case 3
         SomethingElse()
        Case 4
          YetAnotherThing()
        Case 5
         Etc()
     End Select
    Loop
    Each method would just have to set theState to whatever should be shown next, and exit. Alternatively, each method could return the next value for theState, and theState could be a local variable, but it was easier to write it like this. So, if the user entered the Help command, you could just set theState to 2 and return. Getting back to where you left off after viewing the help would be a bit trickier, as you'd have to remember which place you came from, but that would just require one (or two) more variables.

    The nice thing about dividing it all up into methods is that you can keep each piece isolated from the rest, such that you can focus on just one piece at a time.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2017
    Posts
    6

    Re: Looking for a way to read specific lines in my program.

    Ok I will try out your method when I have some more time. But just so I know, is there still any sort of way I can get the program to read a specific line?

    Example:
    Code:
    Line 121 -    Dim example As Integer
    Line 123 -    example = 1 + 2
    Line 124 -    If example = 3 Then
    Line 125 -    Go Back To Line 123
    Line 126 -    End If
    Obviously there is no such thing as "Go Back To Line", but its just an example to show you what I could still use. This program would set "example" to 3, and if "example" is 3, it will go back to line 123 and set "example" to 3 again. I still want to see if this will work for my game.

    Since I am a beginner to programming I am still experimenting with things, so this would be awesome if you could help me with this!

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,435

    Re: Looking for a way to read specific lines in my program.

    To do that, the cleanest way is to use a loop, eg:
    Code:
    Dim example As Integer
    
    Do 
      example = 1 + 2
    Loop While example = 3
    There are other ways, but this is almost always the best... partly because it makes it clear where you are jumping back to, and also when you are going to do it.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2017
    Posts
    6

    Re: Looking for a way to read specific lines in my program.

    I have already tried a loop. It works, until I add more "chunks" of code. The way I want to write the program, i dont think loops will work. Let me show you a little better what I am doing:

    Code:
    Line 119 -    Dim Stage1 As Integer = 1
    Line 120 -    Dim Stage2 As Integer = 0
    Line 121 -    Dim help As Integer = 0
    Line 123 -    Dim inventory As Integer = 0
    Line 124 -    
    Line 125 -    If help = 1 Then
    Line 126 -    ~stuff happens~
    Line 127 -    End If
    Line 128 -    
    Line 129 -    If inventory = 1 Then
    Line 130 -    ~stuff happens~
    Line 131 -    End If
    Line 132 -    
    Line 133 -    If Stage1 = 1 Then
    Line 134 -    If user types "2" Then
    Line 135 -    Stage2 = 1                                 <---- Typing "2" will bring you to stage 2 below.
    Line 136 -    ElseIf user types "help" Then
    Line 137 -    help = 1                                    <---- Typing "help" will bring you to the help screen above. Something right here that would bring the program to line 125 would be good.
    Line 138 -    ElseIf user types "inventory" Then
    Line 139 -    inventory = 1                             <---- Typing "inventory" will bring you to the inventory screen above. Something right here that would bring the program to line 129 would be good.
    Line 140 -    End If
    Line 141 -    End If 
    Line 142 -    
    Line 143 -    If Stage2 = 1 Then
    Line 144 -    If user types "1" Then
    Line 145 -    Stage1 = 1                                <---- Typing "1" will bring you to stage 1 above. Something right here that would bring the program to line 133 would be good.
    Line 146 -    ElseIf user types "help" Then
    Line 147 -    help = 1                                   <---- Typing "help" will bring you to the help screen above. Something right here that would bring the program to line 125 would be good.
    Line 148 -    ElseIf user types "inventory" Then
    Line 149 -    inventory = 1                            <---- Typing "inventory" will bring you to the inventory screen above. Something right here that would bring the program to line 129 would be good.
    Line 150 -    End If
    Line 151 -    End If

    I hope that clears it up a little better. Thanks for the help so far!

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,435

    Re: Looking for a way to read specific lines in my program.

    You only need to use one loop for all of them, you just need to make it big enough to include all of that code.

    For that example, you would add these two lines:
    Code:
    '(just before line 119)
    Do
    Code:
    '(just after line 151)
    Loop While (help = 1) OrElse (inventory = 1) OrElse (Stage1 = 1)
    This is basically the same idea as Shaggy Hiker's example above (if you ignore functions/subs), but this way is a little bit more messy because you are using lots of variables to determine what the current action/stage should be.

    It would probably be easier to have one String variable to contain the current 'action' (eg: "inventory" or "map" or "stage"), and for the stage either an Integer (eg: 1 or 3) or a String (eg: "Stage1" or "Stage1Wall").


    Note that using functions/subs instead is possibly another good way to deal with the situation, but in a way that changes the behaviour a bit - perhaps in a way you want.

    If you create functions/subs for the 'standard' actions (such as Help), you could call it from anywhere in your code, and it would run immediately, then return to exactly the same point as you called it.

    For example, a sub called ShowHelp would be like this:
    Code:
    '(add this outside of your current subs/functions)
    Private Sub ShowHelp
        'put the "~stuff happens~" code here from Line 126 of your last example.
    End Sub
    ...and you would call it like this:
    Code:
                    ElseIf enteredText = "help" Then
                        ShowHelp   'this will show the Help immediately
                        Stage1 = 0   '...and when it finishes, this line will run
                    ElseIf enteredText = "inventory" Then
    Note that any variables you use in the code for Help will need to have their Dim lines moved to outside of any sub/function.

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2017
    Posts
    6

    Re: Looking for a way to read specific lines in my program.

    Ooooohhhhh ok I see now. Thank you!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.