Niminim
Niminim

Reputation: 678

Java object-oriented design for RPG

I started creating a roleplaying as my first java project. I won't show you the code I have, but just an extremely simple version of it so it would be easier to explain and show what exactly it should be done. Here we go - we have an input from the user that contains a string of his chosen character class (Fighter or Rogue in this case), and we want to create a new character, our hero. The character has 3 fields: character class (according to the input) and attack and defense. Now I want to store initial stats of attack and defense for Fighter and Rouge, so the code can call Character(chosenCharacterClass) and create the character with the right stats. How it should be done ? The code is very simple so it would be easy to edit. Just keep in mind that I have much more than 2 stats, so the solution should be good for a long stats-list (most of it would be int but it's plausible that I'll have some booleans), so avoid using enums (and avoid inheritance, I don't think that making Character an abstract class is the best solution here). Thanks!

public class Game {
public static void main(String[] args) throws IOException {

    Character hero = CharacterCreator.createCharacter();

    try {
        hero.displayCharacter();
    }catch (Exception e){
        System.out.println("Wrong input");
    }


}

}



public static Character createCharacter() {
    System.out.println("Choose a character: ");
    System.out.println("1. Fighter");
    System.out.println("2. Rogue");


    Scanner sc = new Scanner(System.in);
    int scan = sc.nextInt();
    String chosenClass = getCharacterClass(scan);

    System.out.println("Choose Name:");
    Scanner nameIn = new Scanner(System.in);
    String name = nameIn.next();

    Character hero = null;

    switch (chosenClass){
        case "Fighter":

            break;
        case "Rogue":

            break;
        case "def":
            System.out.println("Wrong input");
    }

    return hero;
}

    public static String getCharacterClass(int scan){

    String classIn;

    switch (scan) {
        case 1:
            classIn = "Fighter";
            break;
        case 2:
            classIn = "Rogue";
            break;
        default:
            System.out.println("Enter again");
            classIn = "def";
    }

    return classIn;
}
}


public class Character {

    private String characterClass;
    private int attack;
    private int defense;


    protected Character(String characterClass){
    setCharacterClass(characterClass); 
    setAttack();
    setDefense();

    }


    public String getCharacterClass() { return characterClass; }
    public int getAttack() { return attack; }
    public int getDefense() { return  defense; }


    protected void setCharacterClass(String characterClass) { this.characterClass = characterClass; }
    protected void setAttack(int atck){ attack = atck}
    protected void setDefense(int dfns){ defence = dfns; }

}

Upvotes: 2

Views: 2676

Answers (3)

user811602
user811602

Reputation: 1354

I haven't read full question, but what i understand is that You have Character (Warrior, Fighter) and each has property like (attack, defense).

So "Character", in itself is nothing. It is abstract.

public abstract class Character {
    private int attack;
    private int defense;

    public Character(int attack, int defense) {
        this.attack = attack;
        this.defense = defense;
    }

    public int getDefense() {
        return defense;
    }

    public void changeDefence(int alterDefence) {
        defense += alterDefence;
        if (defense < 0) {
            defense = 0;
        }
    }
    // similarly for attack 
}

Here is your Fighter

public class Fighter extends Character {
    public Fighter(int attack, int defense) {
        super(attack, defense);
    }
}

Similarly your Rogue. Now think little more. What "Character" is doing?? Fight, Walk ... and so many. These are inteface So now have interface IFight

public interface Fight<E extends Character> {
    public int fightStat (E char1);
}

Now "Character" is going to implement it.
So "Character" definition is now

public abstract class Character<E extends Character> implements Fight<E>{
....

And "Fighter" now has to implement method in it.

Upvotes: 0

Krystian Laskowski
Krystian Laskowski

Reputation: 333

Object-oriented way would be to make characterClass a class, and make use of polymorphism e.g. (following is a pseudocode java, it may not compile)

interface CharacterClass {
    String getName();
    CharacterStats getBaseStats();
}

public class Warrior implements CharacterClass{
    private CharacterStats = new CharacterStats(){{  //anonymous class with static initializer
        attack = 66;
        defense = 54;
        ...
    }}
    public String getName(){
        return this.class.getSimpleName();
    }

}

public class CharacterStats {
    public int attack;
    public int defence;
    ...
}

public class Character {

    private String characterClass;
    public CharacterStats stats;


...

    public setCharacterBaseStats(CharacterStats baseStats){
        this.stats = baseStats;
    }
}

Character npc1 = new Character();
...
npc1.stats.attack += 1;

Upvotes: 1

Peter Paul Kiefer
Peter Paul Kiefer

Reputation: 2124

I suggest to use a properties file like:

characters=Rogue,Fighter
character.Rogue.attack=15
character.Rogue.defense=7
character.Rogue.use_magic=false
character.Fighter.attack=12
character.Fighter.defense=13
character.Fighter.use_magic=false

You can read the properties file with the Properties class (Java Standard) or a resp. library like e.g. apache commons-configuration ( https://commons.apache.org/proper/commons-configuration/ ) within your Character-constructor. Take the matching value and set it in the contructor.

Upvotes: 0

Related Questions