java继承多态方法地牢怪物游戏

时间:2012-02-09 04:39:44

标签: java inheritance constructor polymorphism

我有一个家庭作业问题,我必须制作一个继承程序,该程序包含一个名为dungeonCharacter的类,有两个子类Hero和Monster,它们有3个自己的子类,有一定的差异等等。我有主类dungeonCharacter的问题。这是描述:

DungeonCharacter (base - abstract)
contains instance variables that any character in the game will have -- protected access     is ok (NO public access allowed!) 
o name - String
o hit points (how much damage a character can take before it expires) - integer
o attack speed - integer (1 is slowest)
o damage range (minimum and maximum amount of damage a character can do on an 
attack) - two integers
o chance to hit opponent when attacking - double
o anything else you deem necessary
 constructor to initialize instance variables get and set methods as you deem necessary
 an attack method 
o first checks if character can attack (based on chance to hit)
o if it can, a hit in range of minimum to maximum damage is generated and applied to 
opponent -- user should be informed of what happens
o if it cannot, a message should be displayed that says the attack failed

这是我的代码,我很难真正理解一些事情,特别是攻击方法和击中的机会。我不知道如何开始这个以及从哪里开始。到目前为止,这是我的代码。

public abstract class DungeonCharacter {
    protected String name;
    protected int hitPoints;
    protected int speed;
    protected int minRange;
    protected int maxRange;
    protected double chance;

    public DungeonCharacter(String name, int hitPoints, int speed,
int minRange, int maxRange, int chance) {
        this.name = name;
        this.hitPoints = hitPoints;
        this.speed = speed;
        this.minRange = minRange;
        this.maxRange = maxRange;
        this.chance = chance;
    }

    public void setString(String newName) {
        name = newName;
    }

    public String getName() {
        return name;
    }

    public void Attack() {

    }
}

请帮助我理解这一点并找到完成指示的必要代码,这个hwk在我看来非常模糊,我很难理解。老师没用谢谢你的帮助!如果我能得到这份书面文字,其余部分应该很容易。

1 个答案:

答案 0 :(得分:2)

如果没有任何硬数据,请自行制定标准。只要它合理,并且你可以在提问时证明它是合理的,你应该全力以赴。变量被称为“机会”施加伤害的事实向我指示概率分布的存在,这意味着0.0意味着没有击中的机会,而1.0意味着你绝对可以命中。你可以用0.0来思考它可能意味着角色太远而不能伤害敌人。 1.0意味着你站在敌人面前。因此,你可以对攻击施加的伤害量取决于攻击的几率和可以抛出的最小/最大伤害。

public void attack() {
    if(chance > 0.0 /* Some arbitrary value */) {
        double damageToApply = minRange + chance*(maxRange - minRange);
        System.out.println("Applying damage: " + damageToApply);
    } else {
        System.out.println("Unable to apply damage, flee!");
    }
}