模拟简单对打(昨天代码的小修改)

精简了昨天的代码和加上了一些注释

加技能攻击,一挑一群,循环选择英雄战斗(前面设置血量那里得变)都有可能加的但没时间试验。

下面代码是创建的“Hero”类

package mt;

public class Hero {
	private  String name;    //英雄名字
	private  int ability_力量;   //力量值
	private  int ability_智力;    //智力值
	private int hp;       //生命值
	/**
	 * 构造器
	 * @param name  英雄名字
	 * @param life  英雄生命
	 */
	public Hero(String name,  int hp ) {
		this.name = name;
		this.hp = hp;
	}
	@Override
	public String toString() {
		String play = "英雄名\t生命值\t力量\t智力" + "\n" 
				+  getName() + "\t" + getLife() + "\t" 
				+ getAbility_力量() + "\t"+ getAbility_智力();
		return play;
	}
	/**
	 * 访问英雄名字
	 * @return  英雄名字
	 */
	public String getName() {
		return name;
	}
	/**
	 * 设置英雄姓名
	 * @param name  名字
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * 设置力量
	 * @param ability_力量   力量值
	 */
	public void setAbility_力量(int ability_力量) {
		this.ability_力量 = ability_力量;
	}
	/**
	 * 访问英雄力量
	 * @return 力量值
	 */
	public int getAbility_力量() {
		return ability_力量;
	}
	/**
	 * 访问英雄智力
	 * @return   智力值
	 */
	public int getAbility_智力() {
		return ability_智力;
	}
	/**
	 *  设置英雄智力
	 * @param ability_智力  智力值
	 */
	public void setAbility_智力(int ability_智力) {
		this.ability_智力 = ability_智力;
	}
	/**
	 * 访问英雄生命
	 * @return  生命值
	 */
	public int getLife() {
		return hp;
	}
	/**
	 * 设置英雄生命
	 * @param life  生命值
	 */
	public void setLife(int life) {
		this.hp = life > 0? life:0;
	}
/**
 * 普通攻击
 * @param h  被攻击对象
 */
	public void normalAttact(Hero h) {
		int a = (int) (Math.random()*51+20);
		int n = this.ability_力量  / 2 + this.ability_智力 / 3 + a ;
		h.setLife(h.getLife() -n);
	}
	/**
	 * 暴击
	 * @param h  被暴击的对象
	 */
	public void criticalAttact(Hero h) {
		int n = (int) (this.ability_力量  * 1 + this.ability_智力 * 1.3) ;
		h.setLife(h.getLife() -n);
	}
	/**
	 * 判断死没
	 * @return  true表示没死,false表示死了
	 */
	public boolean isAlive() {
		return hp> 0;
	}
}

下面是模拟对战的代码

package mt;

import java.util.Scanner;

public class Test {	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("设定英雄的数量:");  
		int nmb = sc.nextInt();              //设定英雄的数量
		Hero[] player = new Hero[nmb];
		for (int i = 0; i < player.length; i++) {
			System.out.print("设定英雄的名字:");
			String name = sc.next();        //设定英雄的名字
			System.out.print("设定英雄的生命值:");
			int hp = sc.nextInt();        //设定英雄的生命值
			Hero hero = new Hero(name , hp);
			System.out.print("设定英雄的力量值:");
			int power =sc.nextInt();     //设定英雄的力量值
			System.out.print("设定英雄的智力值:");
			int wit =sc.nextInt();      //设定英雄的智力值
			hero.setAbility_力量(power);
			hero.setAbility_智力(wit);
			player[i] = hero;      //用来存储输入的英雄的
		}
		for (int i = 0; i < player.length; i++) {
			System.out.println(player[i]+ "\t编号:" + (i+1));   //把英雄编号
		}
		System.out.print("选择第一位出战的英雄编号:");
		int  first = sc.nextInt()-1;                 //选择的第一个英雄编号
		System.out.print("选择第二位出战的英雄编号:");
		int  second = sc.nextInt()-1;               //选择的第二个英雄编号
		int i = 1;  //显示战斗第几轮了
		do {	
			System.out.println("********第" + i++  + "轮战斗********");
			double n = Math.random();   //模拟选择的第一英雄攻击的概率
			if (n <= 0.9) {              //90%的概率普通攻击
				System.out.println(player[first].getName() + "攻击了" +player[second].getName());
				player[first].normalAttact(player[second]);
			}else {                     //10%的概率暴击
				System.out.println(player[first].getName() + "暴击了" +player[second].getName());
				player[first].criticalAttact(player[second]);
			}
			System.out.println(player[second].getName() + "的生命值:" + player[second].getLife());
			
			if (player[second].isAlive()) {
				double m = Math.random(); //模拟选择的第二英雄攻击的概率
				if (m <= 0.9) { //90%的概率普通攻击
					System.out.println(player[second].getName() + "攻击了"
							+ player[first].getName());
					player[second].normalAttact(player[first]);
				} else { //10%的概率暴击
					System.out.println(player[second].getName() + "暴击了"
							+ player[first].getName());
					player[second].criticalAttact(player[first]);
				}
				System.out.println(player[first].getName() + "的生命值:"
						+ player[first].getLife());
			}

		} while (player[first].isAlive() && player[second].isAlive());  //有死就结束
		if (player[first].isAlive()) {
			System.out.println(player[first].getName() + "赢了!!!");
		}else {
			System.out.println(player[second].getName() + "赢了!!!");
		} 
		sc.close();
	}
}


posted @ 2015-05-06 21:11  乜天  阅读(174)  评论(0编辑  收藏  举报