2

I am trying to make a role-playing game in java. I am new at java so i don't understand all things. I Need help to implement class choosing feature.

Here is my code:

package com.rpg.entities;

import java.util.Scanner;

public class Character extends Entity {

    private int EXP; // experience points
    private int gold; // player money
    private double critChance; // critical chance
    private int strength; // attack damage
    private int defense; // reduce taken damage
    private int dexterity; // accuracy only for bow and spells
    private int intelligence; // spell damage
    private int evasion; // dodge attacks
    // armor parts
    private String helmet;
    private String chest;
    private String boots;

    public void setEXP(int exp) {
        this.EXP = exp;
    }

    public int getEXP() {
        return EXP;
    }

    public void setGold(int gold) {
        this.gold = gold;
    }

    public int getGold() {
        return gold;
    }

    public void setCritChance(int crit) {
        this.critChance = crit;
    }

    public double getCritChance() {
        return critChance;
    }

    public void setStrength(int str) {
        this.strength = str;
    }

    public int getStrength() {
        return strength;
    }

    public void setDefense(int def) {
        this.defense = def;
    }

    public int getDefense() {
        return defense;
    }

    public void setDexterity(int dex) {
        this.dexterity = dex;
    }

    public int getDexterity() {
        return dexterity;
    }

    public void setIntelligence(int inte) {
        this.intelligence = inte;
    }

    public int getIntelligence() {
        return intelligence;
    }

    public void setEvasion(int eva) {
        this.evasion = eva;
    }

    public int getEvasion() {
        return evasion;
    }

    public void checkLevel(int playerExp, int playerLevel, Character a) {

        int[] levelArray = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1200, 1400, 1600, 1800, 2000, 2200,
                2400, 2600, 2800, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000 }; // 30

        if (playerExp >= levelArray[playerLevel]) {
            // level up player
            if (playerExp >= levelArray[playerLevel + 1]) {
                a.setEXP(levelArray[playerLevel]);
            } else {
                System.out.println("Level up!");
                a.setLevel(playerLevel + 1);
            }
        }

    }

    public String chooseClass() {

        return "";
    }

}

I want to write method for choosing player class in Character class,then to call it in main function, but i am not sure if that is good practice. And also, i don't know should it return String?

In other words, class Character should have method chooseClass() and with it you can choose classes, for example:

If(name == "warrior") {
    Warrior player = new Warrior();
} else if (name == "mage") {
    Mage player = new Mage();
} else
  System.out.println("Invalid class");

And then returns player to main class so i can use that variable for doing stuffs like: player.setHealth(), etc..

More classes:

package com.rpg.classes;

import com.rpg.entities.Character;
public class Archer extends Character {

    public Archer() {

        this.setMaxHP(100);
        this.setHP(100);
        this.setMaxMP(100);
        this.setMP(100);
        this.setLevel(1);
        this.setDamage(10); // 
        this.setGold(0);
        this.setCritChance(0);  
        this.setStrength(0);
        this.setDefense(0);
        this.setDexterity(10);
        this.setIntelligence(0);
        this.setEvasion(0);
        this.setEXP(100);
    }

}

package com.rpg.classes;

import com.rpg.entities.Character;

public class Mage extends Character {

    public Mage() {

        this.setMaxHP(100);
        this.setHP(100);
        this.setMaxMP(100);
        this.setMP(100);
        this.setLevel(1);
        this.setDamage(10); // popravi posle
        this.setGold(0);
        this.setCritChance(0); // dodaj mehanizam
        this.setStrength(0);
        this.setDefense(0);
        this.setDexterity(0);
        this.setIntelligence(10);
        this.setEvasion(0);
        this.setEXP(100);

    }

}
Dejan Ilic
  • 99
  • 1
  • 4
  • 13
  • Help with what specifically? This is way too broad, and you haven't even given a specification of what the "class choosing feature" should include. – Carcigenicate Mar 28 '17 at 18:17
  • I couldn't understand what you want exactly but you may use switch/case and return String , depends on user selection. – nerdicsapo Mar 28 '17 at 18:20
  • I want to have a method in class Character and if user select option 1, it will choose Warriro class and make instance of warrior class and return it to main so i can use it – Dejan Ilic Mar 28 '17 at 18:24

2 Answers2

1

Not sure what you require. But in your main method you have some way to retrieve from user what char type want. for this eg. i assumed mage. So if it a mage will initalise with mage, etc

    Character character;
    String char_type = "Mage";
    switch(char_type){    
        case "Mage":    
            character = new Mage();
            break;
        case "Archer":    
            character = new Archer();   
            break;
   }

I hope that help or gives you some idea.

EDITED: for some reason i failed to read all your description

I want to write method for choosing player class in Character class,then to call it in main function, but i am not sure if that is good practice. And also, i don't know should it return String?

In other words, class Character should have method chooseClass() and with it you can choose classes, for example:

If(name == "warrior") {
    Warrior player = new Warrior();
} else if (name == "mage") {
    Mage player = new Mage();
} else
  System.out.println("Invalid class");

And then returns player to main class so i can use that variable for doing stuffs like: player.setHealth(), etc..

//public Static Character chooseClass() {
public Character chooseClass() { //you might need to make this method static. not too sure
    String char_type = "Mage"; //you would get the player they want using Scanner i think it is.
    switch(char_type){    
        case "Mage":    
            return new Mage();
        case "Archer":    
            return new Archer();
        default:
            System.out.println("Error that play doesnt exist!")
   }
    return null;
}

Then in your main method you would have something like this.

public static void main(String[] args){
    Character player = Character.chooseClass();
    player.setHealth(100);
}
Mohammad C
  • 1,321
  • 1
  • 8
  • 12
0

For me it looks like all character classes have the same attributes and the same behavior with different initial values.

I would think about an enum representing the class and a factory creating characters by class. The factory could clone some prototypes from a singleton map, deserialize from resources like JSON or simply initialize using some custom code.

Custom code initializing would typically create the character, set common attributes like level and gold and possibly override special attributes in a switch by class.

I know this is not best OO practice but it would keep code simple and hide the "ugly" in the factory. And it keeps flexibility if you would like to do more sophisticated level up changing more attributes later.

You should make the class enum attribute final and consider to make some setters non public especially for the class specific attributes to ensure consistent character instances.

Think about where to put attribute change responsibility. Inside the character or outside, maybe at the factory from above. As you character looks currently more like a POJO I would avoid to put in too much logic there ending up in a mixed type.

As I have no idea about you target vision of your game I can give only some thoughts and ideas. There is no one way, it depends.

Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94
  • Hey, thanks for your answer and opinion. My game is in early stage and i didn't add all attributes, skills, etc.. I want to have some basic functionality, after that i'll add more stuff. I am still new so i don't know good part of Java, but i am learning every day.. I am coding this game so i can practice me knowledge and there is no point to this game. I like rpg games so i choose to practice on that kind of project. – Dejan Ilic Mar 28 '17 at 20:00