英雄打怪

英雄类

package LOLDemo;

/**
 * @ClassName: Hero
 * @Description: 
 *     自定义一个英雄类
 *    0.英雄类中加入“经验值”,“等级”,“攻击力”等属性变量,加入“击打”方法,击打的目标是怪物,击打的结果是怪物的生命值-英雄攻击力
 *	  1.英雄的级别最低为0,最高为30级,
 *	  2.经验值最低为0,最高为30级时的经验值,
 *	  3.该类中要求有一个含有 参数的构造函数,参数为英雄的经验值,初始化时要保证经验值在要求范围之内,
 *	  4.通过经验值计算英雄的级别,英雄的级别计算公式如下: N=当前级别,EXP (经验值) =30 (N^3+5N) -80
 *    5.构建一个无参的构造方法,将经验值设置为0
 *    6.利用英雄类无参和带有参数的构造函数分别构建英雄对象,并输出英雄的等级和经验值。
 *            自加:攻击力:0级100,每升一级攻击力加20
 * @Author: Wanglt   
 * @CreateDate: 2020年2月27日   
 *
 */
public class Hero {
     

	public static final int MAX_LEVEL = 30;

	private String name;// 英雄的名字
	private Integer experience;// 经验值
	private Integer level; // 英雄等级
	private Integer attack; // 攻击力

	public Hero() {
     
		this.name = "无名氏";
		this.experience = 0;
		this.level = countLevelByExperience(this.experience);
		this.attack = 100 + level * 20;
	}

	public Hero(String name) {
     
		this.name = name;
		this.experience = 0;// 初始经验值为0
		this.level = countLevelByExperience(this.experience);
		this.attack = 100 + level * 20;
	}

	public Hero(String name, int experience) {
     
		this.name = name;
		this.experience = experience;// 初始经验值为0
		this.level = countLevelByExperience(this.experience);
		this.attack = 100 + level * 20;
	}

	public String getName() {
     
		return name;
	}

	public void setName(String name) {
     
		this.name = name;
	}

	public Integer getExperience() {
     
		return experience;
	}

	public void setExperience(Integer experience) {
     
		this.experience = experience;
	}

	public Integer getLevel() {
     
		return level;
	}

	public void setLevel(Integer level) {
     
		this.level = level;
	}

	public Integer getAttack() {
     
		return attack;
	}

	public void setAttack(Integer attack) {
     
		this.attack = 100 + level * 20;
	}

	public int countExperienceByLevel(int level) {
     
		double res = 30 * (Math.pow(level, 3) + 5 * level) - 80;
		return (int) res;
	}

	public int countLevelByExperience(int experience) {
     
		double exp;
		int level = 0;
		for (int i = 1; i <= MAX_LEVEL + 1; i++) {
     
			exp = 30 * (Math.pow(i, 3) + 5 * i) - 80;// 达到i级最低的exp
			if (exp > experience) {
     
				level = i - 1;
				break;
			}
		}
		return level;
	}

	public void AttackMonster(Monster monster) {
     
		System.out.println(this.name + "攻击" + monster.getName() + "造成" + this.attack + "点伤害");
		int hp = monster.getHealthPoint();// 怪物被攻击前血量
		if (hp > this.attack) {
      // 怪物的血量大于攻击力
			hp = hp - this.attack;// 怪物的血量减少
		} else {
      // 怪物的血量等于或者小于攻击力
			hp = 0; // 怪物血量为0 死亡
		}
		monster.setHealthPoint(hp); // 改变怪物被攻击后的血量
		System.out.println(monster.getName() + "剩余血量" + hp);
		if (hp == 0) {
      // 怪物死亡
			System.out.println(monster.getName() + "已击败");
			// 加经验、升级
			if (this.level < MAX_LEVEL) {
      // 英雄等级没有达到最高级
				System.out.println(this.name + "的经验值增加" + monster.getExperience() + "点");
				this.experience = this.experience + monster.getExperience(); // 英雄的经验值增加
				if (this.experience >= countExperienceByLevel(this.level + 1)) {
      // 如果英雄增加后的经验值达到升级条件
					this.level = this.level + 1;// 英雄等级 +1
					this.attack = this.attack + 20; // 设定 每升一级英雄攻击力 +20
					System.out.println(this.name + "升级了,攻击力+20,当前等级" + this.level + ",当前攻击力" + this.attack);
				}
			} else {
     
				System.out.println(this.name + "已满级,无法升级");
			}
		}
	}

	@Override
	public String toString() {
     
		StringBuilder builder = new StringBuilder();
		builder.append("Hero [name=");
		builder.append(name);
		builder.append(", experience=");
		builder.append(experience);
		builder.append(", level=");
		builder.append(level);
		builder.append(", attack=");
		builder.append(attack);
		builder.append("]");
		return builder.toString();
	}

}

怪物类

package LOLDemo;

/**
 * @ClassName: Monster
 * @Description:
 * 
 *  怪物类中加入生命值属性、等级属性、生命值=等级*1000
 * @Author: Wanglt   
 * @CreateDate: 2020年2月27日   
 *
 */
public class Monster {
     
	private String name; // 怪物的名字
	private Integer healthPoint;// 生命值
	private Integer level;// 怪物等级
	private Integer experience; // 怪物的经验值

	public Monster(String name, Integer level) {
     
		this.name = name;
		this.level = level;
		this.healthPoint = level * 1000;
		this.experience = level * 10;
	}

	public Integer getHealthPoint() {
     
		return healthPoint;
	}

	public void setHealthPoint(Integer healthPoint) {
     
		this.healthPoint = healthPoint;
	}

	public Integer getLevel() {
     
		return level;
	}

	public void setLevel(Integer level) {
     
		this.level = level;
	}

	public String getName() {
     
		return name;
	}

	public void setName(String name) {
     
		this.name = name;
	}

	public Integer getExperience() {
     
		return experience;
	}

	public void setExperience(Integer experience) {
     
		this.experience = experience;
	}

	@Override
	public String toString() {
     
		StringBuilder builder = new StringBuilder();
		builder.append("Monster [name=");
		builder.append(name);
		builder.append(", healthPoint=");
		builder.append(healthPoint);
		builder.append(", level=");
		builder.append(level);
		builder.append(", experience=");
		builder.append(experience);
		builder.append("]");
		return builder.toString();
	}

}

战斗

package LOLDemo;

/**
 * @ClassName: Fight
 * @Description:
 * @Author: Wanglt
 * @CreateDate: 2020年2月27日
 *
 */
public class Fight {
     
	public static void main(String[] args) {
     

		System.out.println("====================战斗0====================");
		Hero hero0 = new Hero(); // 新建一个英雄
		Monster monster0 = new Monster("小怪", 1);// 新建一个怪物
		do {
     
			hero0.AttackMonster(monster0); // 攻击怪物
		} while (monster0.getHealthPoint() != 0); // 如果怪物没死 一直攻击

		System.out.println("====================战斗1====================");
		Hero hero1 = new Hero("唐三"); // 新建一个英雄
		Monster monster1 = new Monster("比比东", 1);// 新建一个怪物
		do {
     
			hero1.AttackMonster(monster1); // 攻击怪物
		} while (monster1.getHealthPoint() != 0); // 如果怪物没死 一直攻击

		System.out.println("====================战斗2====================");
		Hero hero2 = new Hero("唐三", (int) (30 * (Math.pow(30, 3) + 5 * 30) - 80)); // 新建一个英雄
		Monster monster2 = new Monster("比比东", 10);// 新建一个怪物
		do {
     
			hero2.AttackMonster(monster2); // 攻击怪物
		} while (monster2.getHealthPoint() != 0); // 如果怪物没死 一直攻击
	}
}

运行结果
英雄打怪_第1张图片

你可能感兴趣的:(#,CodeTraining)