Continuing with my VB.NET experience, I wrote a quick guess-the-number game. Any improvement suggestions?
Module GuessTheNumber
Sub Main()
Do
PlayGame()
Loop Until Not ExitGame()
Console.ReadLine()
End Sub
Private Sub PlayGame()
Dim minNumber As Integer = 1
Dim maxNumber As Integer = 100
Dim number As Integer = GetNum(minNumber, maxNumber)
For guessCount = 1 To 6
Dim guess As Integer = InputGuess(minNumber, maxNumber)
Prompt(guess, number)
If guess = number Then
Exit Sub
End If
Next
Console.WriteLine("Too bad, you ran out of guesses.")
End Sub
Private Function GetNum(ByVal Min As Integer, ByVal Max As Integer) As Integer
Static Generator As System.Random = New System.Random()
Return Generator.Next(Min, Max)
End Function
Private Function InputGuess(ByVal MinValue As Integer, ByVal MaxValue As Integer) As Integer
Dim guess As Integer = -1
Do
Console.Write("Enter a guess between " + MinValue.ToString() + " and " + MaxValue.ToString() + ": ")
If Not Integer.TryParse(Console.ReadLine(), guess) Then
guess = -1
End If
Loop Until guess >= MinValue And guess <= MaxValue
Return guess
End Function
Private Sub Prompt(ByVal guess As Integer, ByVal num As Integer)
If guess > num Then
Console.WriteLine("Your guess is too high.")
End If
If guess < num Then
Console.WriteLine("Your guess is too low.")
End If
If guess = num Then
Console.WriteLine("You won!")
End If
End Sub
Private Function ExitGame() As Boolean
Dim input As String = ""
Do While input <> "yes" And input <> "no"
Console.WriteLine("Would you like to play again ('yes' or 'no')?")
input = Console.ReadLine().ToLower()
Loop
Return input = "yes"
End Function
End Module