0

I started creating a basic roleplaying game and I have a problem with the design. I have Character class that includes all the stats and info ( like name, character class, level, hit points, experience points etc.). I have an abstract class named CharacterClass that has some subclasses (like Fighter, Mage etc.). The abstract class CharacterClass has most info that Character has, but not all of it (it doesn't have experience points for instance). The problem is that I have a code duplication ( I create a character by creating a character class (like Fighter, and then assign the data to Character's construction, with some additions), both have strength, hit points, and getHitPoints, and setHitPoints for instance. How can I avoid this ?

Character          CharacterClass (abstract)

                   Fighter  Mage  Rogue Cleric (all extend CharacterClass)                      

If it helps to understand, then when I create a character I have this code:

switch (choice){
        case "Fighter":
            chosenClass = new Fighter();
            break;
        case "Rogue":
            chosenClass = new Rogue();
            break;
        case "Mage":
            chosenClass = new Mage();
            break;
        case "Cleric":
            chosenClass = new Cleric();
            break;
    }

    try {
        hero = new Character(name, chosenClass);
Niminim
  • 668
  • 1
  • 7
  • 16

0 Answers0