EBU4201(2021/22) Lab5 Q2

该代码示例展示了Java中如何使用抽象类`Monster`定义怪物,包括特殊攻击概率和常规攻击方法。`Dragon`和`Troll`类继承自`Monster`,并实现了各自的特殊攻击方法。`TestingMonster`类用于测试不同怪物的攻击和移动行为。代码演示了面向对象编程中的继承和抽象概念。

Lab5 Q2代码基础建立在Q1所写等等程序上:

EBU4201(2021/22) Lab5 Q1icon-default.png?t=M4ADhttp://t.csdn.cn/baQ14代码1:Monster.java

/**
 * @author andtrees
 * @version 1.0
 */
public abstract class Monster {
    String name;
    double spAttackProbability = 0.2;

    public Monster(String name){
        this.name = name;
    }
    public Monster(String name, double spAttackProbability) {
        this.name = name;
        this.spAttackProbability = spAttackProbability;
    }
    public abstract int specialAttack();

    public final int attack() {
        int damage;
        //随机数小于sp的时候用specialAttack,否则用general Attack(常规攻击
        double probability = Math.random();
        if(probability < spAttackProbability){
            damage = specialAttack();
        }
        else {
            damage = (int) (Math.random() * 5 + 1);
            System.out.println(this.name + ", of type "
                    + getClass() + ", attacks generically: "
                    + damage + " points damage caused. ");
        }
        return damage;
    }

    public void move(int direction) {
        switch(direction) {
            case 1: {
                System.out.println(this.name +" is moving 1 step North. ");
                break;
            }
            case 2:{
                System.out.println(this.name +" is moving 1 step East. ");
                break;
            }
            case 3:{
                System.out.println(this.name +" is moving 1 step South. ");
                break;
            }
            default:
                System.out.println(this.name +" is moving 1 step West. ");
                break;
        }
    }
}

代码2:Dragon.java

/**
 * @author andtrees
 * @version 1.0
 */
//public abstract class Dragon extends Monster{
public class Dragon extends Monster{
    public Dragon(String name){
        super(name);
        this.name = name;
    }
    public Dragon(String name, double spAttackProbability){
        super(name,spAttackProbability);
        this.name = name;
        this.spAttackProbability = spAttackProbability;
    }
    public int specialAttack(){
        int x = (int)(Math.random()*50 + 1);
        System.out.println(this.name + ", of type "
                + getClass() + ", attacks generically: "
                + x + " points damage caused. ");
        return x;
    }

}

代码3:Troll.java

/**
 * @author andtrees
 * @version 1.0
 */
public class Troll extends Monster{
    public Troll(String name){
        //super(name);
        //this.name = name;
        super(name);
        if(name.equals("Saul")||name.equals("Salomon")){
            System.out.println("!!ERROR!! This name is not available!!!");
            this.name = "Detritus";
        }else{
            this.name = name;
        }
    }
    public Troll(String name, double spAttackProbability){
        super(name,spAttackProbability);
        //this.name = name;
        if(name.equals("Saul")||name.equals("Salomon")){
            System.out.println("!!ERROR!! This name is not available!!!");
            this.name = "Detritus";
        }else{
            this.name = name;
        }
        this.spAttackProbability = spAttackProbability;
    }
    public int specialAttack(){
        int x = (int)(Math.random()*15 + 1);
        System.out.println(this.name + ", of type "
                + getClass() + ", attacks by hitting with a stick "
                + x + " points damage caused. ");
        return x;
    }
}

代码4:TestingMonster.java


import java.util.ArrayList;

public class TestingMonster {
    public static void main(String[] args){
        ArrayList<Monster> monsters = new ArrayList<Monster>();
        monsters.add(new Dragon("Dragon1"));
        monsters.add(new Dragon("Dragon2"));

        monsters.add(new Troll("Troll1"));
        monsters.add(new Troll("Saul",20));
        int damageDone = 0;
        while (damageDone < 100) {
            for (Monster m : monsters) {
                m.move((int)(Math.random()*4) + 1);
                damageDone = damageDone + m.attack();
            }
        }
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值