John Salerno wrote:
> My code is below. For now I'm focusing on the lines where health (and 
> armor) are increased in each character class. Let's say I decided to 
> change the amount of increase in the future. As it is now, I'd have to 
> go to each character class and change the number so that each is still 
> in a good relation to the other (right now: 3, 2, 1; later: perhaps 4, 
> 3, 2, 1, if I added a new class -- i.e., change Fighter from 3 to 4, 
> Thief from 2 to 3, in other words increase them all by 1). So instead of 
> changing each one, is there a simple, clean way of just changing a 
> single number so that this change is reflected in all classes? Hope that 
> makes sense.
> 
> 
> 
> class Character(object):
>     def __init__(self, name, strength, dexterity, intelligence):
>         self.name = name
>         self.health = 10
>         self.armor = self.attack = self.defense = self.magic_attack = \
>             self.magic_defense = 0
>         self.strength = strength
>         self.dexterity = dexterity
>         self.intelligence = intelligence
>         self.adjust_attributes()
> 
>     def adjust_attributes(self):
>         pass
> 
> 
> class Fighter(Character):
>     def adjust_attributes(self):
>         self.health += 3
>         self.armor += 3
>         self.attack += 2
>         self.defense += 2
>         self.strength += 1
> 
> 
> class Thief(Character):
>     def adjust_attributes(self):
>         self.health += 2
>         self.armor += 2
>         self.attack += 1
>         self.defense += 1
>         self.magic_defense += 1
>         self.dexterity += 1
> 
> 
> class Mage(Character):
>     def adjust_attributes(self):
>         self.health += 1
>         self.armor += 1
>         self.magic_attack += 2
>         self.magic_defense += 2
>         self.intelligence += 1

The place to do this seems to be in the Character class.

class Character(object):
     _health_base_inc = 1
     _armor_base_inc = 1
     # etc
     def __init__(self, name, strength, dexterity, intelligence):
         self.name = name
         self.health = 10
         self.armor = self.attack = self.defense = self.magic_attack = \
             self.magic_defense = 0
         self.strength = strength
         self.dexterity = dexterity
         self.intelligence = intelligence
         self.adjust_attributes()

     def adjust_attributes(self):
         pass

class Mage(Character):
     # for symmetry with Character
     _health_char_inc = 1
     _armor_char_inc = 1
     # etc
     def adjust_attributes(self):
       self.health += self._health_char_inc + self_health_base_inc
       self.armor += self._armor_char_inc + self._armor_base_inc
       # etc

James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to