0

I am making a roguelike based on Trystan's Tutorial and running into issues with implementing a class system. I'm pretty sure the solution is simple, but bear with me.

class Creature {
    int HP;
    CharacterClass playerClass = new Wizard();
    HP = playerClass.hitDie;
    ArrayList<Ability> creatureAbilityList = new ArrayList<>();
    creatureAbilityList.add(classAbilityList.get(1));
}

class CharacterClass {
    int hitDie;
    ArrayList<Ability> classAbilityList = new ArrayList<>();
}

class Wizard extends CharacterClass {
    Wizard() {
        hitDie = 6;
        classAbilityList.add(new Ability(magicMissile));
    }
}

I'm getting a "Syntax error on token ";", , expected", on the semicolon following "new Wizard()". I'm fairly sure that this isn't the issue however, but instead the way that my classes and inheritance is set up. How should I set up the code instead? Any help would be appreciated.

Oneiros
  • 4,328
  • 6
  • 40
  • 69
Ben
  • 3
  • 1
  • Have you tried Wizard playerClass = new Wizard(); ? Also Creature could extend Wizard aswell! – Dinh Oct 25 '17 at 06:59
  • 1
    Ahh the wonders of learning to program! :D you should study basic syntax and learn to indent properly. In the creature class e.g. you both initialize and assign to HP, separate assigning is not allowed straight under the class, it needs to be in a method or constructor or in the initiation statement. – Adam Oct 25 '17 at 07:03
  • @Dinh But then if I want a Barbarian class, etc, that doesnt allow for it. – Ben Oct 25 '17 at 07:14

2 Answers2

3

The problem is the row below. It should be

    int HP=playerClass.hitDie;

(and remove the line int HP;)

Stefan
  • 2,395
  • 4
  • 15
  • 32
  • Don't forget about `creatureAbilityList.add(classAbilityList.get(1));`, which cannot be used in the field. – Vince Oct 26 '17 at 05:46
0

you should make hitDie private, and make it accessible with getter and setter to enable the polimorphism (and so the getHitDie() invoked will be the one of the class Wizard instead of the one of the class CharacterClass)

marco
  • 671
  • 6
  • 22