package demo04;
import java.util.Random;
/*
* 玩家类
* 属性:名字、类型、生命值、防御力、攻击力
* 方法:
* */
public class Player {
//封装:把属性设为public
public String name; //名字
public String type; //职业
public int life; //生命值
public int defense; //防御值
public int maxattack; //攻击力
public int minattack; //攻击力下降
public String equip; //武器
public double baoji; //暴击率
//判断当前对象是否存活
public boolean ks(){
return life >0;
}
/*
* 攻击Player对象
* */
public void attack(Player other) {
//判断双方是否存活
if (!(ks() && other.ks())) {
//其中一人阵亡
return;
}
//计算本次攻击力
int a = getRandomAttack();
//判断暴击率
double x = Math.random();
if (x < baoji) {
a *= 2;
System.out.print("[暴击!!]");
}
//减去对方的生命值
other.life -= a;
System.out.println(name + "对" + other.name + "造成了" + a + "点伤害" + other.name + "剩余生命值:" + other.life+"装备"+other.equip);
//判断对手是否死亡
if (other.life <= 0) {
System.out.println(other.name + "挂了");
System.out.println(other.equip+ "掉落");
}
}
private int getRandomAttack() {
Random r = new Random();
int x = r.nextInt(maxattack - minattack +1) +minattack;
return x;
}
}
package demo04;
public class Test {
public static void main(String[] args) {
Player p1 = new Player();
p1.name = "龙傲天";
p1.type = "战士";
p1.life = 2600;
p1.defense = 1000;
p1.maxattack = 400;
p1.minattack = 200;
p1.baoji = 0.2;
p1.equip = "星辉剑";
Player p2 = new Player();
p2.name = "赵日天";
p2.type = "法师";
p2.life = 1800;
p2.defense = 600;
p2.maxattack = 600;
p2.minattack = 400;
p2.baoji = 0.2;
p2.equip = "天权法杖";
while (p1.ks() && p2.ks()){
p1.attack(p2);
if (p2.life>0){
if (p2.life<100){
System.out.print("掉落装备");
}
}
else {
break;
}
p2.attack(p1);
if (p1.life>0)
if (p1.life>0){
if (p1.life<100){
System.out.print("掉落装备");
}
}
else {
break;
}
}
}
}