Module Bagels Dim Running As Boolean Dim GameOver As Boolean Dim Rnd As Random Dim Digits(3) As Integer Function AskQuestion(prompt As String) Dim gotAns As Boolean = False Dim ans As String While Not gotAns Console.WriteLine($"{prompt} ?") ans = Console.ReadLine If String.IsNullOrWhiteSpace(ans) Then Console.WriteLine("PLEASE ANSWER THE QUESTION.") Else gotAns = True End If End While Return ans.ToUpper() End Function Function GetIntInput(prompt As String) Dim gotVal As Boolean = False Dim value As Integer While Not gotVal Dim ans As String Console.WriteLine($"{prompt} :") ans = Console.ReadLine If Not Integer.TryParse(ans, value) Then Console.WriteLine("YOU DID NOT ENTER AN INTEGER.") Else gotVal = True End If End While Return value End Function Function AskToPlayAgain() Dim gotAns As Boolean = False Dim answer As Boolean While Not gotAns Dim ans As String ans = AskQuestion("DO YOU WANT TO PLAY AGAIN") If ans = "YES" Or ans = "Y" Then gotAns = True answer = True ElseIf ans = "NO" Or ans = "N" Then gotAns = True answer = False Else Console.WriteLine("PLEASE ANSWER ""YES"" OR ""NO""") End If End While Return answer End Function Function AskToSeeTheRules() Dim gotAns As Boolean = False Dim answer As Boolean While Not gotAns Dim ans As String ans = AskQuestion("DO YOU WANT TO SEE THE RULES") If ans = "YES" Or ans = "Y" Then gotAns = True answer = True ElseIf ans = "NO" Or ans = "N" Then gotAns = True answer = False Else Console.WriteLine("PLEASE ANSWER ""YES"" OR ""NO""") End If End While Return answer End Function Function GenerateRandomNumber(minimum As Integer, maximum As Integer, show As Boolean) Dim value As Integer value = Rnd.Next(minimum, maximum) If show Then Console.WriteLine($"{value}") End If Return value End Function Sub DisplayRules() Console.WriteLine($"I WILL THINK OF A THREE-DIGIT NUMBER. YOU WILL TRY TO") Console.WriteLine($"GUESS MY NUMBER AND I WILL GIVE YOU CLUES AS FOLLOWS:") Console.WriteLine($" PICO - ONE DIGIT CORRECT BUT IN THE WRONG POSITION") Console.WriteLine($" FERMI - ONE DIGIT CORRECT AND IN THE RIGHT POSITION") Console.WriteLine($" BAGELS - NO DIGITS ARE CORRECT") Console.WriteLine($"") Console.WriteLine($"YOU GET TWENTY TRIES BEFORE YOU LOSE.") Console.WriteLine($"") End Sub Function AskForGuess(attempt As Integer) Dim guess As Integer Dim gotGuess As Boolean = False While Not gotGuess guess = GetIntInput($"GUESS #{attempt}") If guess < 0 Then Console.WriteLine("PLEASE GUESS A NON-NEGATIVE NUMBER.") ElseIf guess < 100 Or guess > 999 Then Console.WriteLine("PLEASE GUESS A NUMBER BETWEEN 100 AND 999.") Else gotGuess = True End If End While Return guess End Function Sub DisplayGameOver() Dim number As Integer number = MakeIntFromArray(Digits) Console.WriteLine("OK, THAT'S TWENTY GUESSES.") Console.WriteLine($"MY NUMBER WAS {number}.") End Sub Function MakeIntFromArray(arrayIn As Array) Dim retVal As Integer retVal = (arrayIn(0) * 100) + (arrayIn(1) * 10) + arrayIn(2) Return retVal End Function Function MakeArrayFromInt(valIn As Integer) Dim retArray(3) As Integer retArray(0) = Math.Floor(valIn / 100) retArray(1) = Math.Floor((valIn - (retArray(0) * 100)) / 10) retArray(2) = valIn - (retArray(0) * 100) - (retArray(1) * 10) Return retArray End Function Function CreateTheNumber() Dim digits(3) As Integer digits(0) = GenerateRandomNumber(1, 9, False) Dim gotDigit As Boolean = False While Not gotDigit digits(1) = GenerateRandomNumber(0, 9, False) If digits(0) <> digits(1) Then gotDigit = True End If End While gotDigit = False While Not gotDigit digits(2) = GenerateRandomNumber(0, 9, False) If digits(2) <> digits(0) And digits(2) <> digits(1) Then gotDigit = True End If End While Return digits End Function Function CheckGuess(guess As Integer) Dim temp(3) As Integer Dim vals(3) As Integer temp = MakeArrayFromInt(guess) If temp(0) = Digits(0) Then vals(0) = 1 End If If temp(1) = Digits(1) Then vals(1) = 1 End If If temp(2) = Digits(2) Then vals(2) = 1 End If If MakeIntFromArray(vals) = 111 Then Return True End If If vals(0) = 0 Then If temp(0) = Digits(1) Or temp(0) = Digits(2) Then vals(0) = 2 End If End If If vals(1) = 0 Then If temp(1) = Digits(0) Or temp(1) = Digits(2) Then vals(1) = 2 End If End If If vals(2) = 0 Then If temp(2) = Digits(0) Or temp(2) = Digits(1) Then vals(2) = 2 End If End If If MakeIntFromArray(vals) = 0 Then Console.WriteLine("BAGELS") Else Dim display As String = "" If vals(0) = 1 Then display = display + "FERMI " End If If vals(0) = 2 Then display = display + "PICO " End If If vals(1) = 1 Then display = display + "FERMI " End If If vals(1) = 2 Then display = display + "PICO " End If If vals(2) = 1 Then display = display + "FERMI" End If If vals(2) = 2 Then display = display + "PICO" End If Console.WriteLine(display) End If Return False End Function Sub Main() Dim Guess As Integer Dim Attempt As Integer Dim Score As Integer Rnd = New Random(Environment.TickCount) Console.WriteLine("*** BAGELS : VB .NET ***") If AskToSeeTheRules() Then DisplayRules() End If Running = True Score = 0 While Running Attempt = 1 Digits = CreateTheNumber() Console.WriteLine("OK, I HAVE A NUMBER IN MIND.") GameOver = False While Not GameOver Guess = AskForGuess(Attempt) If CheckGuess(Guess) = True Then Console.WriteLine("YOU GOT IT!!!") Score = Score + 1 GameOver = True Else Attempt = Attempt + 1 If Attempt > 20 Then DisplayGameOver() GameOver = True End If End If End While Console.WriteLine("*** GAME OVER ***") Running = AskToPlayAgain() End While Console.WriteLine($"A {Score} POINT BAGELS BUFF!") Console.WriteLine("HOPE YOU HAD FUN. BYE!") Console.ReadLine() End Sub End Module