Hey, ive been creating this number guessing game, it works perfectly but I need to add a limit to the amount of turns a user can have.

Any help would be greatly appreciated.

Module Module1

    Sub Main()

        'This program plays a simple number guessing game.
        Dim RandNum As Integer
        Dim RandomGenerator As New System.Random
        Dim UserGuess As Integer
        Dim Turns As Integer

       
        Console.Clear()
        RandNum = RandomGenerator.Next(1, 10)
        Console.WriteLine("{0}", RandNum)
        Console.SetCursorPosition(20, 12)
        Console.Write("Enter Your Guess :")
        UserGuess = Console.ReadLine()
        While UserGuess <> RandNum
            If UserGuess < RandNum Then
                Console.Clear()
                Console.SetCursorPosition(20, 14)
                Console.WriteLine("{0} Is Wrong. Too Small.", UserGuess)
            ElseIf UserGuess > RandNum Then
                Console.Clear()
                Console.SetCursorPosition(20, 14)
                Console.WriteLine("{0} Is Wrong. Too Big.", UserGuess)
            End If
            Console.SetCursorPosition(20, 12)
            Console.Write("Enter Your Guess :")
            UserGuess = Console.ReadLine()
        End While
        Console.SetCursorPosition(20, 14)
        Console.WriteLine("{0} Is Correct! Well Done", UserGuess)
        Console.ReadKey()

    End Sub

End Module

Edited 6 Years Ago by Emver: n/a

Hi Emver - something like this:

Dim UserGuess As Integer
        Dim Turns As Integer

        Const MAX_TURNS As Integer = 10
        Dim TurnsExceeded As Boolean = False


        Console.Clear()
        RandNum = RandomGenerator.Next(1, 10)
        Console.WriteLine("{0}", RandNum)
        Console.SetCursorPosition(20, 12)
        Console.Write("Enter Your Guess :")
        UserGuess = Console.ReadLine()
        While UserGuess <> RandNum
            Turns += 1
            If Turns <= MAX_TURNS Then
                If UserGuess < RandNum Then
                    Console.Clear()
                    Console.SetCursorPosition(20, 14)
                    Console.WriteLine("{0} Is Wrong. Too Small.", UserGuess)
                ElseIf UserGuess > RandNum Then
                    Console.Clear()
                    Console.SetCursorPosition(20, 14)
                    Console.WriteLine("{0} Is Wrong. Too Big.", UserGuess)
                End If
                Console.SetCursorPosition(20, 12)
                Console.Write("Enter Your Guess :")

                UserGuess = Console.ReadLine()
            Else
                TurnsExceeded = True
                Exit While
            End If

        End While
        
        Console.SetCursorPosition(20, 14)
        If TurnsExceeded Then
            Console.Write("Maximum turns exceeded")
        Else
            Console.WriteLine("{0} Is Correct! Well Done", UserGuess)
        End If

        Console.ReadKey()

    End Sub

End Module
This question has already been answered. Start a new discussion instead.