Guest User

Untitled

a guest
Oct 16th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. Public Class Form1
  2. ' maximum value
  3. Dim max = 10
  4. Dim random = New Random
  5. ' randomly generated number between 1 and maximum value
  6. Dim randVal As Integer = random.Next(1, max)
  7. ' amount of guesses
  8. Dim guesses As Integer = 3
  9.  
  10. ' when the guess button is clicked
  11. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  12. ' if the inputted value is numeric
  13. If IsNumeric(guessInp.Text) = True Then
  14. Dim input As Integer = guessInp.Text
  15. ' checks whether the input is out of range
  16. If input > max Or input < 1 Then
  17. Console.WriteLine("out of range")
  18. Label1.Text = input & " is not valid!"
  19. Else
  20. ' if the input matches the random number and there are still guesses left
  21. If input = randVal And guesses > 1 Then
  22. Console.WriteLine("CORRECT")
  23. Label1.Text = "Correct"
  24. Label3.Text = randVal
  25. ' disables the guess button so you can no longer guess
  26. Button1.Enabled = False
  27. ' if the input is incorrect but there are still guesses left
  28. ElseIf guesses > 1 Then
  29. ' decrement by 1
  30. guesses -= 1
  31. If guesses = 1 Then
  32. Console.WriteLine("INCORRECT, you have " & guesses & " guess left")
  33. Label1.Text = "Incorrect you have " & guesses & " guess left!"
  34. Else
  35. Console.WriteLine("INCORRECT, you have " & guesses & " guesses left")
  36. Label1.Text = "Incorrect you have " & guesses & " guesses left!"
  37. End If
  38. ' when there are no more guesses left
  39. Else
  40. Console.WriteLine("No more guesses left")
  41. ' disables the guess button so you can have no more guesses
  42. Button1.Enabled = False
  43. Label1.Text = "Incorrect, no more guesses left! Answer: " & randVal
  44. Label3.Text = randVal
  45. End If
  46. End If
  47. ' when the input is not numeric
  48. Else
  49. Label1.Text = guessInp.Text & " is not valid!"
  50. Console.WriteLine("INVALID, type string")
  51. End If
  52. End Sub
  53.  
  54. ' when the reset button is clicked
  55. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  56. ' generates a new random number
  57. randVal = random.Next(1, max)
  58. ' enables button
  59. Button1.Enabled = True
  60. ' resets guesses
  61. guesses = 3
  62. ' resets text
  63. Label1.Text = "Enter a number between 1 and " & max
  64. Label3.Text = "NUMBER"
  65. End Sub
  66. End Class
Add Comment
Please, Sign In to add comment