【问题标题】:java inheritance polymorphism method dungeon monster gamejava继承多态方法地牢怪物游戏
【发布时间】:2012-03-01 14:35:09
【问题描述】:

我有一个作业问题,我必须制作一个继承程序,该程序由一个名为 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 非常模糊,我很难理解。而老师是无用的。感谢您的帮助!如果我能得到这篇书面文章,其余的应该很容易。

【问题讨论】:

  • 具体问题和疑问更有可能得到解答。像这样通用的东西开始看起来很像,“请为我做作业。”您有什么特别的疑问或问题吗?
  • 特别是它说首先检查角色是否可以攻击(基于命中的机会),我真的不明白如何检查。如果它像 0.80 一样通过,这意味着 80% 的命中率,我如何检查攻击是否有效?
  • @anthony,在没有任何硬数据的情况下,制定您自己的标准。只要它是合理的,并且你可以在质疑时证明它是合理的,你就应该做好准备。

标签: java inheritance constructor polymorphism


【解决方案1】:

在没有任何硬数据的情况下,制定您自己的标准。只要它是合理的,并且你可以在质疑时证明它是合理的,你就应该做好准备。变量被称为施加伤害的“机会”这一事实向我表明存在概率分布,这意味着 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!");
    }
}

【讨论】:

  • double damageToApply = chance*(maxRange - minRange); 应该是 double damageToApply = (chance*(maxRange - minRange))+minRange; 。我假设您希望他为 0 和 1 之间的值调用 java.lang.Math.random()。此外,它应该随随机值而不是 chance
  • @PeterT 在我看来,他还需要一个“位置”类成员,这给了他一个相对于其他角色的 (x,y) 点。然后,他可以根据与最近敌人的距离来确定“机会”的价值。这就是为什么我使用机会来衡量伤害,而不是一些随机值。至于方程,你是对的。我错过了 minRange。错字。
  • 它与位置无关。如果您阅读规格,则意味着该攻击范围只是角色命中时完全随机计算的最小和最大伤害量。所以是double damage=0.0; if(chance>Math.random()) damage = Math.random()*(maxRange-minRange) + minRange;
  • @PeterT,对不起,我在 OPs 帖子中没有看到“随机”这个词。我错过了什么吗?虽然 Random 是一种完全有效的方法,您可以随意向 OP 建议,这是我的方法。该问题还明确指出:“您认为必要的任何其他内容”。我认为立场是必要的。随意提出您自己的答案。
  • 我理解你们的意思。这就是我所说的模糊的方向。我已经按照你的方式完成了@siddhartha Shankar
猜你喜欢
  • 2020-09-19
  • 1970-01-01
  • 2018-11-26
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多