public class Hunter {
String name; //名字
String weapon; //武器
int life; //生命值
int power; //攻击力
boolean isLive; //是否存活
public void fight(){ //攻击
System.out.println("猎人发起攻击");
}
public int injured(Monster monster){ //受伤
this.life=this.life-monster.power;
return this.life;
}
public void dead(){ //死亡
System.out.println("猎人死亡");
}
public int show(){ //显示状态
return this.life;
}
}
public class Monster {
String name; //名字
String weapon; //武器
int life; //生命值
int power; //攻击力
boolean isLive; //是否存活
public void fight(){ //攻击
System.out.println("僵尸发起攻击");
}
public int injured(Hunter hunter){ //受伤
this.life=this.life-hunter.power;
return this.life;
}
public void dead(){ //死亡
System.out.println("僵尸死亡");
}
public int show(){ //显示状态
return this.life;
}
}
public class TestGame {
public static void main(String[] args) {
Hunter hunter = new Hunter(); //创建猎人
hunter.name="小猎";
hunter.weapon="斧子";
hunter.isLive=true;
hunter.life=100;
hunter.power=20;
Monster monster = new Monster(); //创建僵尸
monster.name="小僵";
monster.weapon="爪子";
monster.isLive=true;
monster.life=50;
monster.power=10;
hunter.fight(); //攻击
monster.fight();
while(hunter.life>0&&monster.life>0){ //互相伤害
hunter.injured(monster);
monster.injured(hunter);
}
if(hunter.life>0){ //判断存活
monster.dead();
System.out.println("猎人剩余生命:"+hunter.show()); //输出状态
}else{
hunter.dead();
System.out.println("僵尸剩余生命:"+monster.show());
}
}
}