//你的问题里面角色成员变量好像没有给出防御,应该是忘记了
package com.mansh.answer;
//防具
public class Equip {
private String equipName;
private int defend;
public Equip(String equipName, int defend) {
this.equipName = equipName;
this.defend = defend;
}
public Equip() {
}
public String getEquipName() {
return equipName;
}
public void setEquipName(String equipName) {
this.equipName = equipName;
}
public int getDefend() {
return defend;
}
public void setDefend(int defend) {
this.defend = defend;
}
}
package com.mansh.answer;
//武器
public class Weapon {
private String weaponName;
private int hurt;
public Weapon(String weaponName, int hurt) {
this.weaponName = weaponName;
this.hurt = hurt;
}
public Weapon() {
}
public String getWeaponName() {
return weaponName;
}
public void setWeaponName(String weaponName) {
this.weaponName = weaponName;
}
public int getHurt() {
return hurt;
}
public void setHurt(int hurt) {
this.hurt = hurt;
}
}
package com.mansh.answer;
//角色
public class Role {
private String roleName;
private int hp;
private int attack;
private int defend;
public Role(String roleName, int hp, int attack, int defend) {
this.roleName = roleName;
this.hp = hp;
this.attack = attack;
this.defend = defend;
}
public Role() {
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefend() {
return defend;
}
public void setDefend(int defend) {
this.defend = defend;
}
public void addWeapon(Weapon weapon){
this.attack += weapon.getHurt();
}
public void addEquip(Equip equip){
this.defend += equip.getDefend();
}
}
package com.mansh.answer;
//战斗
public class Battle {
public static Role[] attackEach(Role[] roles){
if(roles.length != 2){
throw new RuntimeException("目前仅支持2个英雄互相攻击");
}
roles[0].setHp(roles[0].getHp()-(roles[1].getAttack()/roles[1].getDefend()));
roles[1].setHp(roles[1].getHp()-(roles[0].getAttack()/roles[0].getDefend()));
return roles;
}
}
package com.mansh.answer;
//测试主方法
public class MainTest {
public static void main(String[] args) {
Role a = new Role("A",100,15,5);
Role b = new Role("B",200,10,8);
Role[] roles = {a,b};
Equip e = new Equip("皮甲",7);
Weapon w = new Weapon("木棍",15);
b.addWeapon(w);
a.addEquip(e);
int i=1;
while(true){
Battle.attackEach(roles);
System.out.println("进行了"+i+"次攻击,双方状态:\n"+a.getRoleName()+" : "+a.getHp() +"\n"+b.getRoleName()+" : "+b.getHp());
i++;
if(a.getHp()<=0 && b.getHp()<=0){
System.out.println("双方平手");
break;
}else if (a.getHp()<=0){
System.out.println(b.getRoleName()+"获胜");
break;
}
else if (b.getHp()<=0){
System.out.println(a.getRoleName()+"获胜");
break;
}
}
}
}
//目前是仅支持两个角色互相攻击的,扩展的话需要自己增加对应逻辑