[Tutor] a ScoreList class (was: Multiple identical keys in a dictionary)

Glen Wheeler wheelege@tsn.cc
Mon, 28 May 2001 22:27:27 +1000


> <snip!>
>
> > >     def __cmp__(self, other):
> > >         """Allows for comparison with other Score-s and numerics."""
> <...>
> >   Now that's something I didn't think about...although I didn't have to
> > because I ended up using a dictionary :)
> >   Very cool.
>
> At one point I figured it would be nice to have a sort() come out in
> descending order, like you expect from a displayed list of scores.
> Of course, "if score1 < score2:", came out backwards.  :) [cont...]
>

so it does...

> <snip>
> Ya, I was thinking big (all the scores in the list, except for the
> wimps who didn't even try), and considering that a limit on the number
> of entries may be needed (or maybe users named bob21 couldn't post
> scores when the moon was full, whatever [played nethack?]).
>

  Ha ha, yeah I've played nethack - although I like (Z)Angband just that tad
bit more :)  Right now I've actually included the wimps that didn't even get
a single point in a special 'Hall of Shame' listing :)

> <...>
> [...cont]
> > >     def top(self, number):
> > >         self.values.sort()
> > >         return ScoreList(self.values[-number:], self.cutoff)
>
> While thinking about getting "print aScoreList" to spit out in
> descending order, I discovered that this...
>
> def top(self, number):
>     self.values.sort()
>     self.values.reverse()
>     return ScoreList(self.values[:number], self.cutoff)
>
> ...doesn't do the same as reversing the sense of Score.__cmp__ :/
> In fact, reverse() doesn't appear to do anything.
>

  Are you sure..?  self.values...that is a list isn't it?  *checks code*
Well, it is a list.  I added the reverse() line and also a 'print 'values
are', self.values' line and it does in fact reverse the list.  Perhaps
something to do with the 'return ...' statement?
  Well, this does the right thing...

###
    def top(self, number):
        self.values.sort()
        self.values.reverse()
        return ScoreList(self.values[-number:], self.cutoff)
###

  Aha!  Seems somewhere along the line the little negative sign
dissappeared...*has a fiddle*  Well, from what I can tell, your __cmp__
function actually does sort them in the right (descending) order - so when
you reverse your list your actually making it back ascending, then printing
it.  If you have the negative sign there then is will work, but I think we
will both agree that...

###
    def top(self, number):
        self.values.sort()
        return ScoreList(self.values[number:], self.cutoff)
###

  Is better :)

> Since it was almost as late then as it is now, I didn't investigate
> any further... maybe tomorrow....
>
> Any ideas?
>

  Yup, detailed above :)

>
> <...>
> >   Perhaps if something mind-boggling evil comes of the ye olde
'dictionary'
> > approach and a rewrite is neccessary I will go for the class
implementation.
> > In which case I will be happy for this discussion :)
>
> I was thinking a dict may be better than a list (for the Score
> objects) if a ScoreList had a lot of Scores... not sure what the best
> key would be, probably depend on the game and players (is it just a
> list of scores, or will players want to analyse it for weaknesses in
> their opponents, kinda thing).
>

  Heh, not sure how they could analyze anything for weaknesses - since it is
an Arkanoid variant (hey, I was just told to make a small fun distracting
(but not too much so) game...in the vein of old arcade style, I wasn't told
why - even stranger that this is for company wide use too :).  Basically,
all I am thinking of including is player name, score, difficulty and level.
It will probably be netwoked...hmmm how cool would it be to have a single
high score table for everybody...aaa hmmm....must implement...hmmm must
think of code to implement...perhaps a token style file access...aaa
thinking out loud.  I've rambled for way too long :)

  Thanks,
  Glen.

>
> - Bruce
>
>