Two questions about style and some simple math

Mensanator mensanator at aol.com
Mon Jan 19 21:11:24 EST 2009


On Jan 19, 7:44 pm, "Rhodri James" <rho... at wildebst.demon.co.uk>
wrote:
> On Tue, 20 Jan 2009 01:15:47 -0000, Spoofy <spoo... at gmx.net> wrote:
> > Hello everybody!
>
> > Though I'm a hobby programmer for years now (mainly small hackery  
> > things) I still have big problems getting "real" things to work.
>
> > I'm currently trying to write a simple RPG and have problems with the  
> > following:
>
> > 1.
>
> > Characters have a "courage" attribute that basically determins who has  
> > the first attack in a fight. After some trying, I came up with this  
> > (sorry, not really working code, but what I made from interactive  
> > experimentation):
>
> > def first_attack(player1,  player2):
> >      diff = player1.attributes.courage - player2.attributes.courage
> >      players = (player,  player2)
> >      return players[diff + random.randint(-diff,  diff) < 0]
>
> > To make it more realistic, I randomized it a little bit and this seems  
> > to work for low courage values. But when the courage values are high  
> > (100 and such) it fails (the chance to have the first attack drops the  
> > higher the values are). My math is really bad and I have problems to  
> > understand what's happenning here. I suspect the greater range for  
> > randint() is the problem, but I don't really get why.
>
> > Any tips would be greatly appreciated.
>
> This is always going to select player1 (assuming you fix the typo of
> "player" for "player1"!).  The most negative number that the call to
> randint can produce is "-diff", so "diff + randint()" is at least
> "diff + -diff", which is zero and hence never less than zero.
>
> Surely in any case you don't want an expression based on the difference,
> since that would give you the same chance of having the first attack no
> matter what the levels of courage actually were, which can't be right.

Why? Isn't it possible some attributes are relative rather than
absolute?

>
>
>
>
>
> > 2.
>
> > For maintaining the character attributes I creates a seperate class. I  
> > wonder weather this is an "overuse" of OO (instead of just making the  
> > attributes plain variables of the Char class) and if the way I wrote  
> > this is OK (somehow this looks cool to me but maybe too "showy"?)
>
> > class Attributes(object):
> >      ATTRIBUTES = {"attack": 0, "defence": 0, "ability": 0, "courage":  
> > 0, "condition": 0}
> >      def __init__(self, **kwargs):
> >          self.__dict__.update(self.ATTRIBUTES)
> >          for arg in kwargs:
> >              if arg not in self.ATTRIBUTES:
> >                  raise ValueError("Unkown character attribute '%s'" %  
> > arg)
> >              self.__dict__[arg] = kwargs[arg]
>
> It's not necessarily a bad idea to have your character attributes in a
> separate class, but do you really need to prevent use of other class
> attribute names (sorry, the terminology crossover is inherently
> confusing) so much?  Unless you think there's a serious danger of
> trying to add new character attributes on the fly, I think it's
> overkill.
>
> --
> Rhodri James *-* Wildebeeste Herder to the Masses- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -




More information about the Python-list mailing list