Search code examples
javatext-based

Java text based game - How to implement class choosing feature?


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);

    }

}

Solution

  • 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);
    }