模拟简单游戏创建类
新手写的还有很多需要完善
1.代码逻辑性还不够严密,比如输入值的有效性的判断就没写。
2.试了几个别的功能,比如循环选择英雄战斗、显示暴击伤害等,但加了以后代码漏洞更多了,所以删除了。
头都试晕了,所以决定暂时不改了
下面的代码是创建的一个叫“Hero”的类
package mt;
public class Hero {
private String name; //英雄名字
private int ability_力量; //力量值
private int ability_智力; //智力值
private int life; //生命值
/**
* 构造器
* @param name 英雄名字
* @param life 英雄生命
*/
public Hero(String name, int life ) {
this.name = name;
this.life = life;
}
/**
* 显示全部属性
* @return 属性值
*/
public String playAll() {
String play = "英雄名\t生命值\t力量\t智力\t普通伤害\t暴击伤害" + "\n"
+ getName() + "\t" + getLife() + "\t" + getAbility_力量() + "\t"
+ getAbility_智力() + "\t" + normalDamage() + "\t" +
(ability_力量 * 2 + ability_智力 * 3);
return play;
}
/**
* 显示英雄名字和生命(战斗是用)
* @return 属性值
*/
public String play() {
if (life < 0) {
setLife(0);
}
String play = "英雄名\t生命值" + "\n" + getName() + "\t" + getLife();
return play;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setAbility_力量(int ability_力量) {
this.ability_力量 = ability_力量;
}
public int getAbility_力量() {
return ability_力量;
}
public int getAbility_智力() {
return ability_智力;
}
public void setAbility_智力(int ability_智力) {
this.ability_智力 = ability_智力;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
/**
* 普通伤害
* @return 普通伤害值
*/
public int normalDamage() {
int normalDamage = ability_力量 / 2 + ability_智力 / 3;
return normalDamage;
}
/**
* 有3/20的概率产生暴击伤害
* @return 暴击伤害值
*/
public int criticalDamage() {
int nmb = (int) (Math.random()*21);
if (nmb == 0 || nmb == 3 || nmb== 5 ) {
int criticalDamage = ability_力量 * 2 + ability_智力 * 3;
return criticalDamage;
}
return 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 life = sc.nextInt();
Hero hero = new Hero(name , life);
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].playAll() + "\t编号:" + (i+1));
}
System.out.print("选择第一位出战的英雄编号:");
int first = sc.nextInt();
System.out.print("选择第二位出战的英雄编号:");
int second = sc.nextInt();
int i = 0;
do {
int a = player[first-1].getLife() - player[second-1].normalDamage() - player[second-1].criticalDamage();
player[first-1].setLife(a);
int b = player[second-1].getLife() - player[first-1].normalDamage() - player[first-1].criticalDamage();
player[second-1].setLife(b);
i++;
System.out.println("第" + i +"轮战斗结束");
System.out.println(player[first-1].play());
System.out.println(player[second-1].play());
} while (player[first-1].getLife() > 0 && player[second-1].getLife() > 0);
if (player[first-1].getLife() == 0) {
System.out.println(player[first-1].getName() + "输了!!!");
}else {
System.out.println(player[second-1].getName() + "输了!!!");
}
sc.close();
}
}