forked from DarthJDG/NumGuess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
numguess.vb
72 lines (59 loc) · 1.86 KB
/
numguess.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Module numguess
Sub Main()
Dim name, limit, tries, max_tries, number, guess, message
Randomize()
Console.WriteLine("Welcome to NumGuess VB.NET version!" & vbNewLine)
Console.Write("Enter your name: ")
name = Console.ReadLine()
If name = "" Then name = "Player"
Console.Write(vbNewLine & "Welcome " & name & ", enter upper limit: ")
Try
limit = CInt(Console.ReadLine())
If limit < 10 Then limit = 10
Catch
limit = 10
End Try
max_tries = Math.Floor(Math.Log(limit) / Math.Log(2)) + 1
Do
tries = 0
number = Math.Floor(limit * Rnd()) + 1
Console.WriteLine(vbNewLine & "Guess my number between 1 and " & limit & "!" & vbNewLine)
Do
Console.Write("Guess: ")
Try
guess = CInt(Console.ReadLine())
If guess > limit Or guess < 1 Then
Console.WriteLine("Out of range.")
Else
tries += 1
If guess < number Then
Console.WriteLine("Too low!")
ElseIf guess > number Then
Console.WriteLine("Too high!")
End If
End If
Catch
guess = 0
Console.WriteLine("That's just plain wrong.")
End Try
Loop Until guess = number
Console.WriteLine(vbNewLine & "Well done " & name & ", you guessed my number from " & tries & " " & If(tries = 1, "try", "tries") & "!")
If tries = 1 Then
message = "You're one lucky bastard!"
ElseIf tries < max_tries Then
message = "You are the master of this game!"
ElseIf tries = max_tries Then
message = "You are a machine!"
ElseIf tries <= max_tries * 1.1 Then
message = "Very good result!"
ElseIf tries <= limit Then
message = "Try harder, you can do better!"
Else
message = "I find your lack of skill disturbing!"
End If
Console.WriteLine(message)
Console.Write("Play again [y/N]? ")
Loop Until LCase(Console.ReadLine()) <> "y"
Console.WriteLine(vbNewLine & "Okay, bye.")
End Sub
End Module