0
\$\begingroup\$

For my school project I'm making a little NFC based game on Android. The concept is simple, on each phone you create a character and you can fight with other people through NFC.

The character will have have a set of attributes, like strength, agility, etc which are represented as short (only 2 bytes => for NFC communication). A character must have a race (human, dwarf, ...) and a class (warrior, wizard, ...). Each race has attribute advantages (ex : + 2 strength for dwarf) and a passive bonus ex : +5% gold after a fight. Each class can wear different weapon and will have a different attack() methods.

My problem is that I don't know how I should create my character class and allow it to have a race and a class.

Does anyone have an idea of how I can accomplish this ?

\$\endgroup\$
  • 1
    \$\begingroup\$ Do you know anything about interfaces and class inheritance? \$\endgroup\$ – Vaillancourt Apr 18 '16 at 14:28
  • 2
    \$\begingroup\$ Why would you need to employ inheritance at all here? You could just use an Enum to store the class and another to store the race. \$\endgroup\$ – JDSweetBeat Apr 18 '16 at 14:45
  • \$\begingroup\$ @DJMethaneMan You need inheritance for the Race and the Class classes. You don't want all that data stuck in the same Character class, and you'll need a way to differentiate the attack methods. \$\endgroup\$ – Vaillancourt Apr 18 '16 at 14:55
  • \$\begingroup\$ I do know interfaces and class inheritance. Currently my Race classes inherit my Character class. I don't know how to link my Class classes with it. \$\endgroup\$ – Cyrius Apr 18 '16 at 15:40
  • \$\begingroup\$ Are you just wanting people to throw code at you? Because that's not how this website works. \$\endgroup\$ – Krythic Mar 3 '17 at 3:17
2
\$\begingroup\$

As hinted at in the comments, you'll want to use a combination of Inheritance and Composition. (Inheritance being is a and Composition being has a).

In your case, each Character isn't a race or a class, but instead has a race and has a class.

Each race would then inherit from a base Race class, and each Class would inherit from a base Class class.

An example Character class setup:

public interface CharacterClass {
    int doSomething(Character me);
    int applyBonusOrSomesuch(Character me);
}

public interface Race {
    int doSomethingRacial(Character me);
    int applyRacialBonuses(Character me);
}

// Example Race
public class Elf implements Race {
   // Implement the Race methods
}

// Example "Class"
public class Cleric implements CharacterClass {
    // Implement the CharacterClass methods
}

// The Compositional Character class
public class Character {
    Race _race;
    CharacterClass _class;

    ...

    public void ApplyBonuses(){
        _race.applyRacialBounuses(this);
        _class.applyBonusOrSomesuch(this);
    }
    ...
}

You then set the Race and CharacterClass on creation, or whenever is suitable. The Character class would then call the Race and CharacterClass methods as appropriate, and the methods for the classes passed in (e.g. Elf for Race and Cleric for CharacterClass) would then be called.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @XGundam05 Good answer, though I would have done something fancy with enums. \$\endgroup\$ – JDSweetBeat Apr 18 '16 at 17:28
  • \$\begingroup\$ @DJMethaneMan then post an answer :) \$\endgroup\$ – Vaillancourt Apr 18 '16 at 17:40
0
\$\begingroup\$

I would do this rather than have a dozen classes and races;

public class GameCharacter
{
    public enum CharacterClass
    {
        WARRIOR
        {
            @Override
            public void attack(GameCharacter from, GameCharacter target)
            {
                //Do something here
            }
        },

        MAGE
        {
            @Override
            public void attack(GameCharacter from, GameCharacter target)
            {
                //Do something here
            }
        };

        public void attack(GameCharacter from, GameCharacter target)
        {
            //Default attack implementation
        }
    }

    public enum CharacterRace
    {
        HUMAN, ELF, DWARF, ORC;
    }

    private CharacterClass cClass;
    private CharacterRace race;

    public GameCharacter(CharacterClass cClass, CharacterRace race)
    {
        this.cClass = cClass;
        this.race = race;
    }

    public void attack(GameCharacter target)
    {
        cClass.attack(this, target);
    }
   //Some more methods
}

This allows you as much flexibility as the other method, and IMHO is more extendable (ie just add a new member to either race or class to extend them).

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.