Using XML for High Score Table

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1849 Views - Last Post: 18 January 2013 - 10:28 AM Rate Topic: -----

#1 Mayzie12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 16-January 13

Using XML for High Score Table

Posted 16 January 2013 - 12:52 PM

Hello,

Needing a hand here making a high score table for a pong game i have created. Read a few different ways about it but it seems XML is the best way rather than just using a text file. Only problem is i don't know how to use XML. I have read a bit on Serialization and De-serialization but i'm not sure how to use them and the right code to do so.

At the moment when the game ends, it asks for the players name and has the players score in a variable.

How do i save their name and score and then load it up on a different form in a highest to lowest order?

Thanks in Advance

Craig

Is This A Good Question/Topic? 0
  • +

Replies To: Using XML for High Score Table

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9381
  • View blog
  • Posts: 35,227
  • Joined: 12-June 08

Re: Using XML for High Score Table

Posted 16 January 2013 - 01:10 PM

It may be more than you need, but the data table has a 'writexml' and 'readxml' method.

http://msdn.microsof...y/x3zy2whb.aspx
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,416
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 16 January 2013 - 02:00 PM

If it is just name/scores that you want to keep then you could consider storing them in the Project's Settings.

Choose Project.. Properties, and then the Settings tab. Add a name, HighScores, then choose what kind of object you want it to be. It could be a string, StringCollection, or click Browse for other options. It could still be XML and you could serialize, and de-serialize, it as a string.

Set the Scope as Application rather than per User. You access the setting in your program as:

YourProjectName.My.Settings.HighScores

If the information you need to store is more complex then, yes, a text file, XML or a database may be considered.

This post has been edited by andrewsw: 16 January 2013 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#4 Mayzie12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 16-January 13

Re: Using XML for High Score Table

Posted 16 January 2013 - 02:18 PM

Thanks for the reply's so far.

All that is needed is to save the players name, with their score.
Arrange the scores in a highest - lowest order and display them on a form with the corresponding name.
Be able to save the scores permanently.


@ modi123_1
Haven't heard about Data tables, ill have a look into them.

@ andrewsw

I dont need to use XML. I am open to anyway to do it :)
Would that way allow me to arrange the data on a form in the way needed?

Thanks

Craig
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,416
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 16 January 2013 - 02:29 PM

Quote

Would that way allow me to arrange the data on a form in the way needed?


Yes, although I haven't quite worked out the specifics.

If I assume that you might use AAA, AAB as user initials (or names) then these could be keys for a dictionary. This assumes, though, that you would only keep one (high) score per person. Then the scores could be the dictionary values, allowing duplicate scores.

There are many collections that you might consider. A SortedDictionary sounds okay BUT it means sorted by the keys (the initials).. so, not really. Basically, you need to discover which collection is easiest to sort by its values.

But I'm only speculating as I haven't looked into this in a while.
Was This Post Helpful? 0
  • +
  • -

#6 Mayzie12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 16-January 13

Re: Using XML for High Score Table

Posted 16 January 2013 - 03:30 PM

The names used will be peoples names, like craig or andrew and so on.

If one person possess all of the top 10 spaces on the high score table thats fine by me. I would like it to be sorted by score and not just one per person.

I'm guessing then the collection of scores is the best way to sort it? As it it will just be a list of integers.

So i need to add a person and their score to their own corresponding collection.
But if i sort the score will the other details follow and stay linked?

Thanks
Craig
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,416
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 16 January 2013 - 03:46 PM

But if i sort the score will the other details follow and stay linked?

That's the idea :)/>. Just need the right Collection type, and probably use OrderBy to sort by the second column. The link uses an Array of objects as an example, but I'm not yet sure if such can be stored in Settings.

This post has been edited by andrewsw: 16 January 2013 - 03:47 PM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,416
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 16 January 2013 - 04:36 PM

I've been exploring as follows:

Project, ..Properties, Settings
Name: HighScores, System.Collections.Specialized.StringCollection,
Scope: Application.
Add at least one example to initiate: "1000;Andy" (without the quotes).
[You might notice that this is actually stored as XML.]

We need to switch from Debug to Release mode to test this - this option is on the main toolbar. Sample code:

        My.Settings.HighScores.Add("3456;Andy")
        My.Settings.HighScores.Add("3000;Michael")
        My.Settings.HighScores.Add("1234;Pete")
        My.Settings.HighScores.Add("3456;Pete")

        For Each score As String In My.Settings.HighScores
            MsgBox(score.ToString())
        Next

Putting the number first will make it easier to sort, assuming the numbers always use the same number of digits(?). But we will need to Split() the strings anyway, and so we could sort them numerically after having split them, and copied them (the value-pairs) to some other collection.

That's my thinking so far :dozingoff: but someone may have a better suggestion.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,416
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 16 January 2013 - 05:09 PM

Sussed:

Imports System.Collections.Specialized

Structure Scores
    Public Name As String
    Public Score As Integer
End Structure

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim theScores As New List(Of Scores)

        My.Settings.HighScores.Add("3456;Andy")
        My.Settings.HighScores.Add("3000;Michael")
        My.Settings.HighScores.Add("1234;Pete")
        My.Settings.HighScores.Add("3456;Pete")

        For Each score As String In My.Settings.HighScores
            Dim values() As String = score.Split(";")
            'MsgBox(score.ToString())
            theScores.Add(New Scores With {.Score = Int32.Parse(values(0)), .Name = values(1)})
        Next

        For Each score As Scores In theScores.OrderBy(Function(aScore) aScore.Score)
            MsgBox(score.Score & score.Name)
            'add these to a form control
        Next
    End Sub


Not forgetting My.Settings.Save()

Added:

Quote

Scope indicates if the property is read-only. If the value is Application, the property is read-only; if the value is User, the property is read-write.

This seems counter-intuitive. So we need to set the value as User to Save the Settings. But it isn't saving for me; maybe it only does so on release/completion of the application..

This post has been edited by andrewsw: 16 January 2013 - 05:41 PM

Was This Post Helpful? 0
  • +
  • -

#10 Mayzie12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 16-January 13

Re: Using XML for High Score Table

Posted 17 January 2013 - 02:20 PM

View Postandrewsw, on 16 January 2013 - 05:09 PM, said:

Sussed:

Imports System.Collections.Specialized

Structure Scores
    Public Name As String
    Public Score As Integer
End Structure

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim theScores As New List(Of Scores)

        My.Settings.HighScores.Add("3456;Andy")
        My.Settings.HighScores.Add("3000;Michael")
        My.Settings.HighScores.Add("1234;Pete")
        My.Settings.HighScores.Add("3456;Pete")

        For Each score As String In My.Settings.HighScores
            Dim values() As String = score.Split(";")
            'MsgBox(score.ToString())
            theScores.Add(New Scores With {.Score = Int32.Parse(values(0)), .Name = values(1)})
        Next

        For Each score As Scores In theScores.OrderBy(Function(aScore) aScore.Score)
            MsgBox(score.Score & score.Name)
            'add these to a form control
        Next
    End Sub


Not forgetting My.Settings.Save()

Added:

Quote

Scope indicates if the property is read-only. If the value is Application, the property is read-only; if the value is User, the property is read-write.

This seems counter-intuitive. So we need to set the value as User to Save the Settings. But it isn't saving for me; maybe it only does so on release/completion of the application..


Thanks for all of the help so far.

I have been trying to implement the code above that you posted. I have two settings added one named "name" and the other is "Highscores"

The type for name is string, scope is application and the value field is blank

The type for HighScores is System.Collections.Specialized.StringCollection, Scope is application and the value is blank.


I basically put the code you wrote above in a seperate file, so i can understand it before inplementing it in my game but i'm guessing i've done something wrong as it is saying "theScores" is not declared.

What have i done wrong? :S

Thanks again

Craig
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,416
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 17 January 2013 - 02:29 PM

You need to initiate the Setting with at least one value, as I described earlier. There may be a way around this, but this is easiest.

To extend on my example, deleting an item is:

My.Settings.HighScores.Remove("1234;Pete")

[so it needs to match precisely to be deleted]

and to sort in descending order use OrderByDescending() instead of OrderBy().

Note, however, that your example is different to mine as you are using two Settings. I'm not sure what your intention is: how are you going to keep the Name and HighScore synchronised? Unless the Name is the name of the current user(?) and the score includes their name (similar to my example). Added: I don't see why you would need to store (as a Setting) the current users' name, as you could just store this in a variable.

This post has been edited by andrewsw: 17 January 2013 - 02:33 PM

Was This Post Helpful? 1
  • +
  • -

#12 Mayzie12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 16-January 13

Re: Using XML for High Score Table

Posted 17 January 2013 - 02:49 PM

Ah i understand now.

The reason i had two was i was setting the HighScores as an integer, as i thought that was the way i would sort it.

In other words i didnt read the post properly.....

Right so as i ask for the users name at the end of the game, it is variable in my program, i can just slot that in with the score and it will still be sorted with the numbers.

Thanks again

Craig
Was This Post Helpful? 0
  • +
  • -

#13 Mayzie12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 16-January 13

Re: Using XML for High Score Table

Posted 17 January 2013 - 03:07 PM

Apologies for double post, Cannot find the edit button :/

When i try to add e.g. 1234;Pete to the value, it comes up as says "Cannot be converted to an instance of type System.Collections.Specialized.StringCollection"

Cheers
Craig
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,416
  • Joined: 12-December 12

Re: Using XML for High Score Table

Posted 17 January 2013 - 03:15 PM

Quote

Right so as i ask for the users name at the end of the game, it is variable in my program, i can just slot that in with the score and it will still be sorted with the numbers.


My code-samples store as "1234;Dave" then, currently, splits these out to the number and the name, and sorts by the number.

If the numbers were always the same size/width then you wouldn't need to split them out to number/name. You could pad the number with zeroes to help:

        Dim user As String = "Michael"
        Dim uScore As Integer = 123
        Dim userScore As String = uScore.ToString().PadLeft(5, "0") & ";" & user
        My.Settings.HighScores.Add(userScore)       '00123;Michael


However, I suspect that splitting them to number/name will prove useful (necessary..) in the longer term anyway.

Quote

..still be sorted with the numbers

Note: they are not stored in sorted order.

View PostMayzie12, on 17 January 2013 - 03:07 PM, said:

Apologies for double post, Cannot find the edit button ://>

When i try to add e.g. 1234;Pete to the value, it comes up as says "Cannot be converted to an instance of type System.Collections.Specialized.StringCollection"

Cheers
Craig


How are you adding..?

My.Settings.HighScores.Add("1234;Pete")
Was This Post Helpful? 0
  • +
  • -

#15 Mayzie12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 16-January 13

Re: Using XML for High Score Table

Posted 17 January 2013 - 03:53 PM

View Postandrewsw, on 17 January 2013 - 03:15 PM, said:

How are you adding..?

My.Settings.HighScores.Add("1234;Pete")


I'm Adding it using the code above.

But as you said earlier to initiate the setting with one value. in the settings tab, if i try to put e.g. 1234;pete into it it comes up with the error i said earlier, so i cant.

However if i run the program without that anyway, it highlights

My.Settings.HighScores.Remove("1234;Pete")

and says

Object reference not set to an instance of an object.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3