设计一个游戏
注意:过程中定义的所有的成员变量,都要写成私有的!
定义一个装备类:
成员变量:名字、防御
构造方法:无参构造方法,带参构造方法
成员方法;设置名字(set方法)、获得名字(get方法)、设置防御、获得防御。
定义一个武器类
成员变量:名字、攻击
构造方法:无参构造方法,带参构造方法
成员方法;设置名字(set方法)、获得名字(get方法)、设置伤害、获得伤害。
定义一个角色类
成员变量:名字、血量、伤害
成员方法:设置名字(set方法)、获得名字(get方法)、设置防御、获得伤害,设置血量,获得血量。
成员方法:穿装备,穿武器。
定义一个战斗类
成员方法:相互攻击(英雄或BOSS血量为0时结束)
定义一个公共类
main方法,创建对象,执行游戏。
//你的问题里面角色成员变量好像没有给出防御,应该是忘记了
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;
}
}
}
}
//目前是仅支持两个角色互相攻击的,扩展的话需要自己增加对应逻辑
你这是什么语言啊。。。,语言都不说
//装备类
package timu;
public class Furnish {
private String furnishName;
private Integer defence;
public Furnish(String furnishName, Integer defence) {
super();
this.furnishName = furnishName;
this.defence = defence;
}
public Furnish(){
}
public String getFurnishName() {
return furnishName;
}
public void setFurnishName(String furnishName) {
this.furnishName = furnishName;
}
public Integer getDefence() {
return defence;
}
public void setDefence(Integer defence) {
this.defence = defence;
}
}
//武器类
package timu;
public class Weapons {
private String name;
private Integer attack;
public Weapons(){
}
public Weapons(String name, Integer attack) {
super();
this.name = name;
this.attack = attack;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAttack() {
return attack;
}
public void setAttack(Integer attack) {
this.attack = attack;
}
}
//角色类
package timu;
public class Role {
private String name;
private Integer blood;
private Integer hurt;
public Role(){
}
public Role(String name, Integer blood, Integer hurt) {
super();
this.name = name;
this.blood = blood;
this.hurt = hurt;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getBlood() {
return blood;
}
public void setBlood(Integer blood) {
this.blood = blood;
}
public Integer getHurt() {
return hurt;
}
public void setHurt(Integer hurt) {
this.hurt = hurt;
}
//穿装备
public void wearFurnish(Furnish fh){
this.setBlood(this.getBlood()+fh.getDefence());
}
//穿武器
public void wearWeapons(Weapons ws){
this.setHurt(this.getHurt()+ws.getAttack());
}
}
//相互攻击
package timu;
public class PVP {
public void attackEachOther(Role hero,Role boss){
hero.setBlood(hero.getBlood()-boss.getHurt());
boss.setBlood(boss.getBlood()-hero.getHurt());
Integer bossBlood = boss.getBlood();
Integer heroBlood = hero.getBlood();
System.out.println("user血量:"+heroBlood);
System.out.println("boss血量:"+bossBlood);
}
}
//启动
package timu;
public class StartGame {
public static void main(String[] args) {
PVP p = new PVP();
Role user = new Role("皇太极",100,20);
Role boss = new Role("皇太极的爹",1000,30);
Furnish fh = new Furnish("银鳞胸甲",500);
Weapons ws = new Weapons("屠龙宝刀",80);
user.wearFurnish(fh);
user.wearWeapons(ws);
while(user.getBlood()>0 && boss.getBlood()>0){
p.attackEachOther(user, boss);
if(user.getBlood()<=0){
System.out.println(boss.getName()+"WIN!");
}
if(boss.getBlood()<=0){
System.out.println(user.getName()+"WIN!");
}
}
System.out.println("完成");
}
}
//忘采纳!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!