Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java game select player class

Tags:

java

class

I'm learning java by creating a game and i have :

public abstract class Char {
    private int hp;
    private int def;
    private int power;
    private int intelligence;
    privat1e int lvl;
    private int xp;

public void levelUp()
{
    lvl += 1;
    hp *= 2;
    def *= 2;
    power *= 2;
    intelligence *= 2;
}

public void basicalAttack(Char ennemy)
{
    ennemy.hp = ennemy.hp - (power - ennemy.def);
}

public int getHp() {
    return hp;
}
public void setHp(int hp) {
    this.hp = hp;
}
public int getDef() {
    return def;
}
public void setDef(int def) {
    this.def = def;
}
public int getPower() {
    return power;
}
public void setPower(int power) {
    this.power = power;
}
public int getIntelligence() {
    return intelligence;
}
public void setIntelligence(int intelligence) {
    this.intelligence = intelligence;
}
public int getLvl() {
    return lvl;
}
public void setLvl(int lvl) {
    this.lvl = lvl;
}
public int getXp() {
    return xp;
}
public void setXp(int xp) {
    this.xp = xp;
}

}

Then i have 2 class of characters :

public class Wizard extends Char {

public Wizard() {
    setLvl(1);
    setHp(400);
    setDef(150);
    setPower(20);
    setIntelligence(200);
}

public void fireBallAttack(Char ennemy)
{
    ennemy.setHp(ennemy.getHp() - (this.getIntelligence() + 20 - ennemy.getDef()));
}
}

and

public class Warrior extends Char {

public Warrior() {
    setLvl(1);
    setHp(1000);
    setDef(250);
    setPower(100);
    setIntelligence(20);
}

public void swordAttack(Char ennemy)
{
    ennemy.setHp(ennemy.getHp() - (this.getPower() + 10 - ennemy.getDef()));
}
}

So I want my playable Character to choose a class :

public class PlayableChar extends Char {
private String name;
private Char playable;
public PlayableChar(String name, String job) {
    this.setName(name);
    switch (job)
    {
        case "warrior" :
            setPlayable(new Warrior());
            break;
        case "Wizard" :
            setPlayable(new Wizard());
            break;
    }
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public Char getPlayable() {
    return playable;
}
public void setPlayable(Char playable) {
    this.playable = playable;
}

}

So I ask to the user which class he wants to use in my main with

    Scanner readline = new Scanner(System.in);
    String jobName = "";
    String Name = "";
    System.out.print("Your name: ");
    Name = readline.nextLine();
    jobName = readline.nextLine();
    Playable mainChar = new Playable(Name, jobName);

Actually it works but when I want to use a method with mainChar.getPlayable().fireBallAttack(ennemy) it doesn't work and I get a

fireBall(ennemy) is undefined for the Type Char;

Do I need to change my Warrior and Wizard classes ? or something else ? Thanks.

like image 900
Terry Rapt Avatar asked May 16 '26 02:05

Terry Rapt


1 Answers

Based on your response and code, I assume theres few typos and error`s in the question. I would like to point those out, before I provide the solution.

a)

Playable mainChar = new Playable(Name, jobName); should have been

PlayableChar mainChar = new PlayableChar (Name, jobName);  
//Note: Capitalisation is not recommended for variable names in java. 
//Referring to variable *Name* here

b) Reference to fireBall(ennemy) should have been fireBallAttack(ennemy).

You are narrow casting the playable attribute within PlayableChar class from Wizard / Warrior to Char class, when you call mainChar.getPlayable().fireBallAttack(ennemy). mainChar.getPlayable().fireBallAttack(ennemy) return a Char. At this stage compiler has no way of knowing if its Wizard / Warrior. It just knows, its a instance of Char class. The method fireBallAttack(ennemy) is defined only in Warrior class and not in Char class. This causes the compiler to throw the compile time error.

This is more of a design issue and there are multiple solutions to address this. The easiest one is to add the following two methods to Char class.

public void fireBallAttack(Char ennemy)
{

}

public void swordAttack(Char ennemy)
{

}
like image 109
Sajeev Avatar answered May 18 '26 15:05

Sajeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!