4 Replies - 1775 Views - Last Post: 08 March 2011 - 07:57 PM Rate Topic: -----

#1 pretychil420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-March 11

High-Score Table : Sorting values from 2 external text files - need he

Posted 07 March 2011 - 04:30 PM

Hi, i am developing a game in visual basic for a very important piece of coursework. I've overcame every other hurdle thrown at me except this. This is a last resort - having spent at least 8 hours today on the internet doing research, while i've made good steps in the right direction, i'm just not getting it at all.

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




Is This A Good Question/Topic? 0
  • +

Replies To: High-Score Table : Sorting values from 2 external text files - need he

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,464
  • Joined: 29-May 08

Re: High-Score Table : Sorting values from 2 external text files - need he

Posted 07 March 2011 - 04:33 PM

What are your theories?
Was This Post Helpful? 0
  • +
  • -

#3 pretychil420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-March 11

Re: High-Score Table : Sorting values from 2 external text files - need he

Posted 07 March 2011 - 04:43 PM

View PostAdamSpeight2008, on 07 March 2011 - 04:33 PM, said:

What are your theories?

It's hard to explain clearly. Basically i would sort the score array (after i managed to convert all the score values from strings to integers... fml) into descending order, when the order of the values in the arrays changed - they would also change in the name array by matching up the array value?

example

BEFORE:
score(0) = 1 & name(0) = a
score(1) = 2 & name(1) = b

DURING
*sort scores*
score(1) = 2
score(0) = 1
since the order of array value 1 has now swapped with array value 0, this should be done with the name array aswell

AFTER :

score(1) = 2 & name(1) = b
score(0) = 1 & name(0) = a

This would probably require the use of arrays to store values temporarily or maybe even an entirely new array for cross-referencing. I have literally no clue, i just need all the advice i can get on high-score tables and how to sort them. Even if it requires totally changing how i save/store the values. There must be a simple way to implement the storing of strings and integers and then order them accordingly? Because of my lack of knowledge there hasn't been much to go on.

This post has been edited by pretychil420: 07 March 2011 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,464
  • Joined: 29-May 08

Re: High-Score Table : Sorting values from 2 external text files - need he

Posted 07 March 2011 - 04:57 PM

When sorting what are called parallel arrays, when you sort you also have to move the associated entries in each of the arrays also.

This where the concept of encapsulation comes in. You need to define an object to encapsulate all to properties you want to capture inside that object.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,464
  • Joined: 29-May 08

Re: High-Score Table : Sorting values from 2 external text files - need he

Posted 08 March 2011 - 07:57 PM

Have a look at this tutorial
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1