Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Module Module1
- Private Const scoresFilePath As String = "scores.txt"
- Sub Main()
- AskAgainMode:
- Console.Write(vbCrLf & "_-_ LESS OR MORE ? _-_ (VB.NET version)" & vbCrLf & vbCrLf & "1. Play" & vbCrLf & "2. Look at scores" & vbCrLf & "3. Quit game" & vbCrLf & "Type the number corresponding to the action of your choice : ")
- Select Case Console.ReadLine()
- Case 1
- PlayGame()
- Case 2
- LookAtScores()
- Case 3
- Environment.Exit(0)
- Case Else
- GoTo AskAgainMode
- End Select
- End Sub
- Sub PlayGame()
- AskAgainDifficulty:
- Console.Write(vbCrLf & "_-_ GAME _-_" & vbCrLf & vbCrLf & "1. Easy (*)" & vbCrLf & "2. Normal (**)" & vbCrLf & "3. Hard (***)" & vbCrLf & "Type the number corresponding to the difficulty of your choice : ")
- Dim interval As Integer
- Dim choiceDifficulty As Integer
- Try
- choiceDifficulty = Console.ReadLine()
- Catch
- GoTo AskAgainDifficulty
- End Try
- Select Case choiceDifficulty
- Case 1 'Easy
- interval = 100
- Case 2 'Normal
- interval = 1000
- Case 3 'Hard
- interval = 10000
- Case Else
- GoTo AskAgainDifficulty
- End Select
- Dim randomValue As Integer = New Random().Next(interval)
- Console.WriteLine(randomValue)
- Dim counter As Integer = 0
- Console.WriteLine(vbCrLf & "A number in the interval [0 ; " & interval & "] has just been generated. Find it!")
- AskAgainFindNumber:
- counter += 1
- Console.Write(vbCrLf & "Type a number in the interval [0 ; " & interval & "] : ")
- Dim inputValue As Integer
- Try
- inputValue = Console.ReadLine()
- Catch
- GoTo AskAgainFindNumber
- End Try
- If inputValue < 0 Or inputValue > interval Then
- GoTo AskAgainFindNumber
- End If
- If randomValue < inputValue Then
- Console.WriteLine("The number to find is lower than " & inputValue & ".")
- ElseIf randomValue > inputValue Then
- Console.WriteLine("The number to find is bigger than " & inputValue & ".")
- Else
- Console.WriteLine("You found the secret number (" & randomValue & ") in " & counter & " attempts !")
- Dim oldScore As Integer = GetScore(choiceDifficulty)
- If counter < oldScore Or oldScore = 0 Then
- WriteScore(choiceDifficulty - 1, counter)
- End If
- AskAgainReplay:
- Console.WriteLine(vbCrLf & "Do you want to replay ? Answer by typing " & Chr(34) & "y" & Chr(34) & " for yes or " & Chr(34) & "n" & Chr(34) & " for no : ")
- Select Case Console.ReadLine()
- Case "y" 'Yes
- GoTo AskAgainDifficulty
- Case "n" 'No
- Main()
- Case Else
- GoTo AskAgainReplay
- End Select
- End If
- GoTo AskAgainFindNumber
- End Sub
- Sub LookAtScores()
- Console.WriteLine(vbCrLf & "_-_ SCORES _-_" & vbCrLf & vbCrLf & "Easy mode : " & GetScore(0) & " attempts." & vbCrLf & "Normal mode : " & GetScore(1) & " attempts." & vbCrLf & "Hard mode : " & GetScore(2) & " attempts." & vbCrLf & "Press " & Chr(34) & "ENTER" & Chr(34) & " to quit..." & vbCrLf)
- Console.ReadKey()
- Main()
- End Sub
- Function GetScore(ByVal difficulty As Integer) As Integer
- Dim scoresFileContent As String = File.ReadAllText(scoresFilePath)
- Dim scores() As Integer = Array.ConvertAll(scoresFileContent.Split(";")(0).Split(","), New Converter(Of String, Integer)(AddressOf Int32.Parse))
- Return scores(difficulty)
- End Function
- Sub WriteScore(ByVal difficulty As Integer, ByVal newScore As Integer)
- Dim newLine As String
- For index = 0 To 2
- If difficulty = index Then
- newLine += newScore.ToString
- Else
- newLine += GetScore(index).ToString
- End If
- If index <> 2 Then
- newLine += ","
- Else
- newLine += ";"
- End If
- Next
- File.WriteAllText(scoresFilePath, newLine)
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement