Program : my game is a frogger port, you make the frog cross the road until you run out of lives. When you die, you are asked to enter your name and the name is appended to the end of file called names.txt, the final score is appended to the end of a file called scores.txt. Using the System.IO functions, i read the contents of each text file, and load the contents into two separate strings (one for names, one for scores. These two long strings are then broken down using Split() command, transferring the pieces into an array. (again 2 arrays - one for names, one for scores). The contents of both arrays are then loaded into 2 listboxes, 1 for scores, 1 for names.
Problem: i can sucessfully load the contents of both files, and display them side by side - but i want to re-arrange the scores so that they are in descending order. While doing this, i need to make sure that the player name matches up with the score - so the order of the names being displayed would have to be changed aswell. After spending many, many hours trying to do it myself i have had to give up - i'm clueless. My first issue is converting the score array into integers so that the array could be sorted, the second problem is then sorting the names so that the scores and names match up.
While i have theories on how to solve this issue, i don't have the knowledge or expertise to implement it. I suspect that there will have been an easier way to create a highscore table aswell (perhaps through a record, xml file etc) but again i didn't have the knowledge. Having started coding from scratch - with no prior knowledge or help from my teachers, this has been an uphill struggle. The lack of support is killing me - the amount of fundamental gaps in my knowledge has led to days worth of wasted coding time.
ANY help you can give me will be much appreciated.
The code below is from the game screen form :
Private Sub gameover() Dim playername As String 'pauses all traffic Timer1.Enabled = False Timer2.Enabled = False Timer3.Enabled = False Timer4.Enabled = False Timer5.Enabled = False 'gets name and informs user of score + level reached playername = InputBox("Enter your name") MsgBox(playername & " - you have no more lives remaining, you managed to reach level : " & level & " and scored : " & score) Dim filepath_name As String Dim filepath_score As String filepath_score = "C:\temp\scores.txt" filepath_name = "C:\temp\names.txt" Dim scoreWriter As New StreamWriter(filepath_score, True) scoreWriter.WriteLine("," & score) scoreWriter.Close() Dim NameWriter As New StreamWriter(filepath_name, True) NameWriter.WriteLine("," & playername) NameWriter.Close() Me.Close() 'closes current page HighScore.Show() 'displays highscore page End Sub
The code below is from the form which displays the highscore table :
Private Sub getscores() Dim filepath_names As String Dim textcontents As String Dim split_contents() As String Dim filepath_scores As String Dim scorecontents As String Dim score_split() As String filepath_names = "C:\temp\names.txt" filepath_scores = "C:\temp\scores.txt" Dim scoreReader As New StreamReader(filepath_scores) Dim objReader As New StreamReader(filepath_names) textcontents = objReader.ReadToEnd split_contents = textcontents.Split(",") scorecontents = scoreReader.ReadToEnd score_split = scorecontents.Split(",") Call sortscores(score_split, split_contents) End Sub Private Sub sortscores(ByVal split_contents() As String, ByVal score_split() As String) ' currently empty because i have NO clue how to sort the scores Call displayscores(score_split, split_contents) End Sub Private Sub displayscores(ByVal split_contents() As String, ByVal score_split() As String) ListBox1.Items.Clear() ListBox2.Items.Clear() For i = 0 To UBound(score_split) ListBox2.Items.Add(score_split(i)) Next For i = 0 To UBound(split_contents) ListBox1.Items.Add(split_contents(i)) Next End Sub