'Making a HighScore List in VB.Net
I made a jeopardy game in VB.Net
I know I am missing some codes but I don't know what should I use.
I need to save in a txt file and load it to a listbox.
here is my code:
Private Sub SummaryMenu_Load (sender As Object, e As EventArgs) Handles MyBase.Load
highscoreFile = FreeFile()
FileOpen(highscoreFile,
"jeeperdeyData.txt",
OpenMode.Input)
FileClose()
If totalScore > highscoreFile Then
highscore = totalScore
FileOpen(highscoreFile,
"jeeperdeyData.txt",
OpenMode.Output)
WriteLine(highscoreFile, username & " " & highscore)
FileClose(highscoreFile)
End If
End Sub
i have the variables in modules
'files
Public highscoreFile As Integer
Thanks
Solution 1:[1]
This replicates the work from the existing method with APIs that are actually under active use, though it seems odd to do all of this in a method named "SummaryMenu_Load()":
Private Class ScoreData
Public Property Name As String
Public Property Score as Integer
Public Shared Function FromFileLine(line As String) As ScoreData
Dim data = line.Split(vbTab)
Return New ScoreData With {Name = data(0), Score = Integer.Parse(data(1))}
End Function
Public Overrides Function ToString()
Return $"{Name}{vbTab}{Score}"
End Function
Public Shared ReadOnly Property MaxScores As Integer = 10
End Class
Private Sub SummaryMenu_Load (sender As Object, e As EventArgs) Handles MyBase.Load
var scores = File.ReadLines("jeeperdeyData.txt").
Select(AddressOf ScoreData.FromFileLine).
OrderByDescending(Function(s) s.Score).
ToList()
If scores.Count < ScoreData.MaxScores Then
scores.Add(new ScoreData With {Name = username, Score = highscore})
Else
Dim i As Integer = 0
While i < scores.Count AndAlso highscore <= scores(i).Score
i++
End While
If i < scores.Count Then 'Greater than at least one existing entry
Dim current As ScoreData = scores(i)
scores(i) = New ScoreData With {Name = username, Score = highscore}
i+=1
While i < scores.Count
Dim temp As ScoreData = scores(i)
scores(i) = current
current = temp
i+=1
End While
End If
End IF
File.WriteAllLines("jeeperdeyData.txt", scores.Select(Function(s) s.ToString()))
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
