Lab5 Q2代码基础建立在Q1所写等等程序上:
EBU4201(2021/22) Lab5 Q1http://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();
}
}
}
}