Advertisement
SpydotNet

More or less VB Edition by SpydotNet

May 21st, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.42 KB | None | 0 0
  1. Imports System.IO
  2.  
  3. Module Module1
  4.     Private Const scoresFilePath As String = "scores.txt"
  5.  
  6.     Sub Main()
  7. AskAgainMode:
  8.         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 : ")
  9.  
  10.         Select Case Console.ReadLine()
  11.             Case 1
  12.                 PlayGame()
  13.             Case 2
  14.                 LookAtScores()
  15.             Case 3
  16.                 Environment.Exit(0)
  17.             Case Else
  18.                 GoTo AskAgainMode
  19.         End Select
  20.     End Sub
  21.  
  22.     Sub PlayGame()
  23. AskAgainDifficulty:
  24.         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 : ")
  25.  
  26.         Dim interval As Integer
  27.         Dim choiceDifficulty As Integer
  28.  
  29.         Try
  30.             choiceDifficulty = Console.ReadLine()
  31.         Catch
  32.             GoTo AskAgainDifficulty
  33.         End Try
  34.  
  35.         Select Case choiceDifficulty
  36.             Case 1 'Easy
  37.                 interval = 100
  38.             Case 2 'Normal
  39.                 interval = 1000
  40.             Case 3 'Hard
  41.                 interval = 10000
  42.             Case Else
  43.                 GoTo AskAgainDifficulty
  44.         End Select
  45.  
  46.         Dim randomValue As Integer = New Random().Next(interval)
  47.         Console.WriteLine(randomValue)
  48.         Dim counter As Integer = 0
  49.  
  50.         Console.WriteLine(vbCrLf & "A number in the interval [0 ; " & interval & "] has just been generated. Find it!")
  51.  
  52. AskAgainFindNumber:
  53.         counter += 1
  54.  
  55.         Console.Write(vbCrLf & "Type a number in the interval [0 ; " & interval & "] : ")
  56.  
  57.         Dim inputValue As Integer
  58.  
  59.         Try
  60.             inputValue = Console.ReadLine()
  61.         Catch
  62.             GoTo AskAgainFindNumber
  63.         End Try
  64.  
  65.         If inputValue < 0 Or inputValue > interval Then
  66.             GoTo AskAgainFindNumber
  67.         End If
  68.  
  69.         If randomValue < inputValue Then
  70.             Console.WriteLine("The number to find is lower than " & inputValue & ".")
  71.         ElseIf randomValue > inputValue Then
  72.             Console.WriteLine("The number to find is bigger than " & inputValue & ".")
  73.         Else
  74.             Console.WriteLine("You found the secret number (" & randomValue & ") in " & counter & " attempts !")
  75.  
  76.             Dim oldScore As Integer = GetScore(choiceDifficulty)
  77.             If counter < oldScore Or oldScore = 0 Then
  78.                 WriteScore(choiceDifficulty - 1, counter)
  79.             End If
  80.  
  81. AskAgainReplay:
  82.             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 : ")
  83.  
  84.             Select Case Console.ReadLine()
  85.                 Case "y" 'Yes
  86.                     GoTo AskAgainDifficulty
  87.                 Case "n" 'No
  88.                     Main()
  89.                 Case Else
  90.                     GoTo AskAgainReplay
  91.             End Select
  92.         End If
  93.  
  94.         GoTo AskAgainFindNumber
  95.  
  96.     End Sub
  97.  
  98.     Sub LookAtScores()
  99.         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)
  100.         Console.ReadKey()
  101.         Main()
  102.     End Sub
  103.  
  104.     Function GetScore(ByVal difficulty As Integer) As Integer
  105.         Dim scoresFileContent As String = File.ReadAllText(scoresFilePath)
  106.  
  107.         Dim scores() As Integer = Array.ConvertAll(scoresFileContent.Split(";")(0).Split(","), New Converter(Of String, Integer)(AddressOf Int32.Parse))
  108.  
  109.         Return scores(difficulty)
  110.     End Function
  111.  
  112.     Sub WriteScore(ByVal difficulty As Integer, ByVal newScore As Integer)
  113.         Dim newLine As String
  114.  
  115.         For index = 0 To 2
  116.             If difficulty = index Then
  117.                 newLine += newScore.ToString
  118.             Else
  119.                 newLine += GetScore(index).ToString
  120.             End If
  121.  
  122.             If index <> 2 Then
  123.                 newLine += ","
  124.             Else
  125.                 newLine += ";"
  126.             End If
  127.         Next
  128.  
  129.         File.WriteAllText(scoresFilePath, newLine)
  130.     End Sub
  131.  
  132. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement