Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- ' maximum value
- Dim max = 10
- Dim random = New Random
- ' randomly generated number between 1 and maximum value
- Dim randVal As Integer = random.Next(1, max)
- ' amount of guesses
- Dim guesses As Integer = 3
- ' when the guess button is clicked
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- ' if the inputted value is numeric
- If IsNumeric(guessInp.Text) = True Then
- Dim input As Integer = guessInp.Text
- ' checks whether the input is out of range
- If input > max Or input < 1 Then
- Console.WriteLine("out of range")
- Label1.Text = input & " is not valid!"
- Else
- ' if the input matches the random number and there are still guesses left
- If input = randVal And guesses > 1 Then
- Console.WriteLine("CORRECT")
- Label1.Text = "Correct"
- Label3.Text = randVal
- ' disables the guess button so you can no longer guess
- Button1.Enabled = False
- ' if the input is incorrect but there are still guesses left
- ElseIf guesses > 1 Then
- ' decrement by 1
- guesses -= 1
- If guesses = 1 Then
- Console.WriteLine("INCORRECT, you have " & guesses & " guess left")
- Label1.Text = "Incorrect you have " & guesses & " guess left!"
- Else
- Console.WriteLine("INCORRECT, you have " & guesses & " guesses left")
- Label1.Text = "Incorrect you have " & guesses & " guesses left!"
- End If
- ' when there are no more guesses left
- Else
- Console.WriteLine("No more guesses left")
- ' disables the guess button so you can have no more guesses
- Button1.Enabled = False
- Label1.Text = "Incorrect, no more guesses left! Answer: " & randVal
- Label3.Text = randVal
- End If
- End If
- ' when the input is not numeric
- Else
- Label1.Text = guessInp.Text & " is not valid!"
- Console.WriteLine("INVALID, type string")
- End If
- End Sub
- ' when the reset button is clicked
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- ' generates a new random number
- randVal = random.Next(1, max)
- ' enables button
- Button1.Enabled = True
- ' resets guesses
- guesses = 3
- ' resets text
- Label1.Text = "Enter a number between 1 and " & max
- Label3.Text = "NUMBER"
- End Sub
- End Class
Add Comment
Please, Sign In to add comment