Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Trivia Game in Console Application

I made something very similar to this in Batch back in 2012. I've been working with console applications recently and I decided to see if I could remake it in VB. So here it is, I only did level one. The original I made in batch had 10 levels.

*Note: It's missing some try statements, meaning if an invalid charter is entered the program might crash.

Anyways, someone might find some use out of this. Have fun.

Code
Module Module1

    Sub Main()
        Game()
    End Sub
    Sub Game()
        'Sets Title
        Console.Title = "Trivia Master"


        'Variables Start
        Dim number As Integer = Nothing
        'Variables End

        'Clears any text in the console
        Console.Clear()

        'Main Menu
        Console.WriteLine("Welcome to the home page!")
        Console.WriteLine()
        Console.WriteLine("1) Start Trivia Game")
        Console.WriteLine("2) Info")
        Console.WriteLine("3) Level List")
        Console.WriteLine("4) Exit")
        Try
            number = Console.ReadLine
        Catch ex As Exception
            Main()
        End Try
        'Calls selected stub
        If number = "1" Then
            Trivia()
        ElseIf number = "2" Then
            Info()
        ElseIf number = "3" Then
            LevelList()
        ElseIf number = "4" Then
            End
        Else
            Console.Clear()
            Main()
        End If
        Console.ReadLine()
    End Sub
    'START OF TRIVIA
    Sub Trivia()

        Console.Clear()
        Console.WriteLine("Hello! I'm Trivia Bot, I'll be your Game host today!")
        Console.WriteLine("Press enter to begin.")
        Try
            Console.ReadLine()
        Catch ex As Exception
            LevelOne()
        End Try
        LevelOne()
    End Sub


    Sub LevelOne()
        Dim answer As Char = Nothing

        Console.Clear()
        Console.WriteLine("Welcome to level 1! Your first question is as follows")
        Console.WriteLine("What is the fastest moving thing in the world?")
        Console.WriteLine()

        Console.WriteLine("A) Air Plane")
        Console.WriteLine("B) Light")
        Console.WriteLine("C) A Snail")
        Console.WriteLine("D) A car")
        answer = Console.ReadLine()

        If (answer = "a") Or (answer = "A") Then
            Console.Clear()
            Console.WriteLine("Wrong!")
            Console.WriteLine("Press enter to try again...")
            Console.ReadLine()
            Console.Clear()
            LevelOne()
        ElseIf (answer = "b") Or (answer = "B") Then
            Console.Clear()
            Console.WriteLine("Correct! The fastest moving thing in the world is Light.")
            Console.WriteLine("Press enter to continue to the next level")
            Console.ReadLine()
            Leveltwo()
        ElseIf (answer = "c") Or (answer = "C") Then
            Console.Clear()
            Console.WriteLine("Wrong!")
            Console.WriteLine("Press enter to try again...")
            Console.ReadLine()
            Console.Clear()
            LevelOne()
        ElseIf (answer = "d") Or (answer = "D") Then
            Console.Clear()
            Console.WriteLine("Wrong!")
            Console.WriteLine("Press enter to try again...")
                Console.ReadLine()
                Console.Clear()
                LevelOne()
        End If
    End Sub

    Sub Leveltwo()
        Console.Clear()
        Console.WriteLine("Welcome to level two. This is not coded yet.")
        Console.WriteLine("")
        Console.Write("Press enter to return to the main menu")
        Try

            Console.ReadLine()
        Catch ex As Exception
            Game()
        End Try
        Game()
    End Sub
    'END OF TRIVIA


    Sub Info()
        Console.Clear()
        Console.WriteLine("A trivia game base in VB Console by Amuse")
        Console.WriteLine()
        Console.WriteLine("Press enter to return to the main menu")
        Console.ReadLine()
        Main()
    End Sub

    Sub LevelList()
        Dim answer As Integer = Nothing
        Console.Clear()
        Console.WriteLine("This is a list of all levels")
        Console.WriteLine("Type a levels number and press enter to jump to the selected level")
        Console.WriteLine()
        Console.WriteLine("1) Level 1")
            answer = Console.ReadLine
            If answer = "1" Then
                LevelOne()
            Else
                Console.Clear()
                Console.WriteLine("That is an invalid level")
                Console.WriteLine()
                Console.WriteLine("Press enter to continue")
                Console.ReadLine()
                LevelList()
            End If
    End Sub
End Module
Not too bad & I'm sure this will help some new coders out. Like you clearly stated; with some Try/Catch codes and a few touch-ups; this will def. be a good share to help others ;)
(Mar 22, 2015 07:36 PM)nuk Wrote:  Not too bad & I'm sure this will help some new coders out. Like you clearly stated; with some Try/Catch codes and a few touch-ups; this will def. be a good share to help others ;)

Learned today about Select Case apposed to if statements. Much more efficient for what I want to do. c:

Updated the code, it should no longer crash at any point as well. c:
Code
Module Module1

    Sub Main()
      'Runs the Game Sub - I used this incase I wanted to restart the game becuse of an unexpect char.
      Game()
    End Sub
    Sub Game()
      'Sets Title
      Console.Title = "Trivia Master"

      'Variables Start
      Dim number As Integer = Nothing
      'Variables End

      'Clears any text in the console
      Console.Clear()

      'Main Menu Text
      Console.WriteLine("Welcome to the home page!")
      Console.WriteLine()
      Console.WriteLine("1) Start Trivia Game")
      Console.WriteLine("2) Info")
      Console.WriteLine("3) Level List")
      Console.WriteLine("4) Exit")
      Try
      'Changes varible number from Nothing to users imput
      number = Console.ReadLine
      Catch ex As Exception
      Console.Clear()
      Main()
      End Try
      'Checks to see if the select number is 1, 2, 3, or 4. Then does the intented code.
      Select Case number
      Case "1"
            'Starts the trivia Game Sub
            Trivia()
      Case "2"
            'Starts Info Sub
            Info()
      Case "3"
            'Starts the LevelList Sub
            LevelList()
      Case "4"
            'Quits the program
            End
      Case Else
            'Incase anything besides 1,2,3, or 4 is entered the program restarts rather then crashing.
            Console.Clear()
            Main()
      End Select
    End Sub
    'START OF TRIVIA
    Sub Trivia()
      'Clears the mainmenu
      Console.Clear()
      'Writes some text for the user to read
      Console.WriteLine("Hello! I'm Trivia Bot, I'll be your Game host today!")
      Console.WriteLine("Press enter to begin.")
      Try
      'Does nothing intill the user press enter.
      Console.ReadLine()
      Catch ex As Exception
      'Incase the user enters an invalid char it will move onto levelone anyways
      LevelOne()
      End Try
      LevelOne()
    End Sub

    Sub LevelOne()
      'Stores the users answer as a string
      Dim answer As String = Nothing
      'clears trivias intro text
      Console.Clear()
      'writes some text on screan for the user to read
      Console.WriteLine("Welcome to level 1! Your first question is as follows")
      Console.WriteLine("What is the fastest moving thing in the world?")
      'Makes a one line space
      Console.WriteLine()
      'Writes the questions of the game
      Console.WriteLine("A) Air Plane")
      Console.WriteLine("B) Light")
      Console.WriteLine("C) A Snail")
      Console.WriteLine("D) A car")
      'saves the answer the user types in as a string
      answer = Console.ReadLine()
      'sets the case to lower if the user types in caps
      Select Case answer.ToLower
      'Takes the correct action depending on if the user answered a,b,c, or d.
      Case "a"
            Console.Clear()
            Console.WriteLine("Wrong!")
            Console.WriteLine("Press enter to try again...")
            Console.ReadLine()
            Console.Clear()
            LevelOne()
      Case "b"
            Console.Clear()
            Console.WriteLine("Correct! The fastest moving thing in the world is Light.")
            Console.WriteLine("Press enter to continue to the next level")
            Console.ReadLine()
            Leveltwo()
      Case "c"
            Console.Clear()
            Console.WriteLine("Wrong!")
            Console.WriteLine("Press enter to try again...")
            Console.ReadLine()
            Console.Clear()
            LevelOne()
      Case "d"
            Console.Clear()
            Console.WriteLine("Wrong!")
            Console.WriteLine("Press enter to try again...")
            Console.ReadLine()
            Console.Clear()
            LevelOne()
            'If the user did not answer a,b,c, or d the program will restart levelone so it does not crash.
      Case Else
            LevelOne()
      End Select
    End Sub

    Sub Leveltwo()
      'Same as level, just diffrent questions, and it would call sub LevelThree
      Console.Clear()
      Console.WriteLine("Welcome to level two. This is not coded yet.")
      Console.WriteLine("")
      Console.Write("Press enter to return to the main menu")
      Try

      Console.ReadLine()
      Catch ex As Exception
      Game()
      End Try
      Game()
    End Sub
    'END OF TRIVIA

    Sub Info()
      'Clears any text from Main Menu
      Console.Clear()
      'Writes some text for the user to read
      Console.WriteLine("A trivia game base in VB Console by Amuse")
      Console.WriteLine()
      Console.WriteLine("Press enter to return to the main menu")
      'Waits for the user to press enter before going back to Main() which leads to Game()
      Console.ReadLine()
      Main()
    End Sub

    Sub LevelList()
      'Same idea as LevelOne, but it leads to the users desired level
      Dim answer As Integer = Nothing
      Console.Clear()
      Console.WriteLine("This is a list of all levels")
      Console.WriteLine("Type a levels number and press enter to jump to the selected level")
      Console.WriteLine()
      Console.WriteLine("1) Level 1")
      Try
      answer = Console.ReadLine
      Catch ex As Exception
      Console.Clear()
      LevelList()
      End Try

      Select Case answer
      Case "1"
            LevelOne()
      Case Else
            Console.Clear()
            Console.WriteLine("That is an invalid level")
            Console.WriteLine()
            Console.WriteLine("Press enter to continue")
            Console.ReadLine()
            LevelList()
      End Select
    End Sub
End Module
Yeah it's working ;) I hear ya homie... once I learned about Case; my code looked so much cleaner LoL


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by 4lek5Jun 18, 2022 10:00 AM 8 272 Jun 18, 2022 10:00 AM
Last Post: SIM-5183822
[.NET] by absojediMay 17, 2019 12:17 PM 1 179 May 17, 2019 12:17 PM
Last Post: StarZ
[C#] by VelvetCodingApr 17, 2019 09:39 PM 1 205 Apr 17, 2019 09:39 PM
Last Post: SIM-3991524