Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Have mercy

Name: Anonymous 2009-11-19 6:56

Gentlemen, I'm playing around with C# trying to make a simple little game with a highscore board but sorting the highhscores is troubling me.

Highscores are read from a highscore file and put in an ArrayList with 3 fields, int score, string playerName, int moves. The board will only have a 'top ten' so including the last game played there is a potential of 11 highscores. The ArrayLists need to be sorted by the first element, score, and then trimmed to 10 entries before being written back to the highscore file and displayed.

Now, sorting a single ArrayList is fine, but I have 11 that need to be checked against each other and then sorted. I even tried an ArrayList of ArrayLists, what the shit.

Name: Anonymous 2009-11-19 7:16

THERE IS NO EXCEPTIONS GOT IT?

Name: Anonymous 2009-11-19 8:28

Well, if anyone is interested the answer was to use objects in an ArrayList and make a new class using IComparable, then just array.Sort()

feels good man

Name: Anonymous 2009-11-19 8:35

>>3
ENTERPRISE SOLUTIONS

Name: Anonymous 2009-11-19 8:40

>>4
thanx, u r very kind

Name: Anonymous 2009-11-19 8:54

>>3

Wrote the code for you.

class Schore : IComparable
{
    string name;
    int schore;
    int movesh;

    Schore(string name, int schore, int movesh)
    {
        this.name = name;
        this.schore = schore;
        this.movesh = movesh;
    }

    public int CompareTo(object obj)
    {
        if (obj is Schore)
        {
            Schore tempSchore = (Schore) obj;
            return this.schore.CompareTo(tempSchore.schore);
        }
        else
        {
            throw new ArgumentException();
        }
    }

    //put a tostring method in here...
}

Hope this works. I don't have a C# compiler.

Name: Anonymous 2009-11-19 9:07

>>6
Now with Best Practices!
        struct SchoreEntry : IComparable<SchoreEntry>
        {
            public readonly string Name;
            public readonly int Schore;
            public readonly int Movesh;
            public SchoreEntry(string name, int schore, int movesh)
            {
                this.Name = name;
                this.Schore = schore;
                this.Movesh = movesh;
            }
            public int CompareTo(SchoreEntry other)
            {
                return this.Schore.CompareTo(other.Schore);
            }
        }

        static void Main()
        {
            var sc1 = new SchoreEntry("MDickie", 9001, 0);
            var sc2 = new SchoreEntry("Anon", 1, 100);
            Console.WriteLine(sc1.CompareTo(sc2));
        }

Name: Anonymous 2009-11-19 9:13

>>7
Fuck. I forgot that struct's could have interfaces in C#... Thanks.

Name: Anonymous 2009-11-19 9:26

here's what i had, god help me if the code tag isn't correct.



    class scoresCompare : IComparable
    {
        public int score;
        public string name;
        public string date;
        public string time;
        public int moves;

        public int CompareTo(object obj)
        {
            scoresCompare Compare = (scoresCompare)obj;
            int result = this.score.CompareTo(Compare.score);
            if (result == 0)
            {
                result = this.score.CompareTo(Compare.score);
            }
            return result;
            }
        }

Name: Anonymous 2009-11-19 9:47

>>9
Use, the DateTime class for your date & time variable. Also, why are all of your variables public?

Name: Anonymous 2009-11-19 10:06

>>10
My lecturer said he "doesn't care" about protection levels and hasn't taught us anything about it so I'm completely ignorant.

I kept date and time as strings because i'm not using them as anything but strings.

lolwut

Name: Anonymous 2009-11-19 11:16

>>10
members want to be free

Name: Anonymous 2009-11-19 11:22

>>12
Java scopes, increasing visibility: local, private, package, protected, public, published (JavaBean only).

I've also heard of some BS scope called "default" which can not be formally declared and is the same as "friendly" or "package."  I don't believe you ever have a reason to declare that integer counter you initialized for your loop "local" either.

Name: Anonymous 2009-11-19 11:32

>>14
package, protected
your mistake.
Unless Java is completely braindead, these two are incomparable.

Name: sage 2009-11-19 12:00

>>12
Yes, indeed, my good man, Java was designed by committees at Sun; they over engineer everything there. Turns out to be really nice for the hardware side of things, everything else...not so much.

Name: Anonymous 2011-02-04 14:43

Name: Anonymous 2011-02-04 14:44

Name: Anonymous 2011-02-04 16:24

Name: Anonymous 2011-02-18 13:08

check 'em
Don't change these.
Name: Email:
Entire Thread Thread List