package problem2;
public abstract class Monster
{
String name;
double spAttackPropability = 0.2;
// public Monster(String name, double spAttackPropability)
// {
// this.name = name;
// this.spAttackPropability = spAttackPropability;
// }
//
// public Monster(String name)
// {
// this.name = name;
// }
public Monster()
{
}
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;
}
}
public abstract int specialAttack();
public final int attack()
{
int damage;
// 题目要求 如果生成的随机数小于spAttackPropability 则使用specialAttack(),否则 使用 常规攻击
double probability = Math.random();
if (probability < spAttackPropability)
{
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;
}
}
Note: 要注意special 和 常规攻击的关系,把这二者的关系判断都写到 这个Monster(父类)当中是一个非常好的选择!!
为什么不在Monster里建立 constructor? 因为题目中要求:provide two constructor for each kind of monster!
package problem2;
public class Dragon extends Monster
{
public Dragon(String name)
{
this.name = name;
}
public Dragon(String name, double spAttackPropability)
{
this.name = name;
this.spAttackPropability = spAttackPropability;
}
@Override
public int specialAttack()
{
int x = (int) (Math.random() * 50 + 1);
System.out.println(this.name + " ," + "of type" + getClass() + ", attacks by breathing fire: " + x
+ " points damage caused.");
return x;
}
}
package problem2;
public class Troll extends Monster
{
public Troll(String name)
{
if (name.equals("Saul") || name.equals("Salomon"))
{
System.out.println("Error: name is illegal.");
this.name = "Detritus";
} else
{
this.name = name;
}
}
public Troll(String name, double spAttackPropability)
{
this(name);
this.spAttackPropability = spAttackPropability;
}
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;
}
}
Note:在Troll 类中,需要判断 命名是否正确
在 public Troll(String name) 和 public Troll(String name, double spAttackPropability)
都需要判断,但是在 public Troll(String name, double spAttackPropability) 可以通过使用
this(name) 调用public Troll(String name) 判断命名正确与否
package problem2;
import java.util.ArrayList;
public class TestingMonster
{
public static void main(String[] args)
{
ArrayList<Monster> monsters = new ArrayList<>();
monsters.add(new Dragon("Dragon1"));
monsters.add(new Dragon("Dragon2"));
monsters.add(new Troll("Troll1"));
monsters.add(new Troll("Saul", 20));
// System.out.println(monsters.get(3).spAttackPropability);
// System.out.println(monsters.get(3).name);
int damageDone = 0;
while (damageDone < 100)
{
for (Monster m : monsters)
{
m.move((int) (Math.random() * 4) + 1);
damageDone = damageDone + m.attack();
}
}
}
}