1>创建一个Hero类特征name(英雄名字),hp(血量),mp(魔法值), physicsAttack(物理攻击力),magicAttack(魔法攻击力)
2>创建一个BattleControl战斗管理类,该类有两个字段分别为List ours(存储己方阵营中五个英雄)和List enemy(存储对方五个英雄)类型,分别创建五个英雄和五个敌人分别添加到对应List泛型列表中。添加int类型字段round 记录战斗开始到结束总的回合数。注:英雄名字自定义,血量200 魔法值100 攻击力自定义。
3>模拟回合制游戏逻辑,初始化完毕后首先己方攻击,每次攻击随机从对方阵营筛选一个敌人进行攻击,
a)每次造成的伤害为物理攻击和魔法攻击的叠加值,如果没有魔法值那么伤害值只是物理攻击力即可。
b)每次魔法攻击之后需要减去相应的魔法值,比如魔法攻击力为20,那么魔法值也需要消耗20个点。
c)攻击的同时输出信息格式:小一攻击了小二,小二剩余血量为80。
d)己方所有英雄攻击完毕之后,敌人攻击,攻击流程同上。
4>当某一个敌人或者己方英雄死亡后将死亡角色从对应List泛型列表中移除,某一个阵营全部死亡,游戏结束,开始结算,显示获胜方。显示总的回合数。
代码如下:
class Hero{ //英雄类
private string name; //英雄名字
private int hp; //血量
private int mp; //魔法值
private int physicsAttack; //物理攻击力
private int magicAttack; //魔法攻击力
public Hero(string name,int hp,int mp,int pA,int mA){
this.name=name;this.hp=hp;this.mp=mp;this.physicsAttack=pA;this.magicAttack=mA;
}
//set and get method:
public void setName(string name){
this.name = name;
}
public string getName(){
return this.name;
}
public void setHp(int hp){
this.hp = hp;
}
public int getHp(){
return this.hp;
}
public void setMp(int mp){
this.mp = mp;
}
public int getMp(){
return this.mp;
}
public void setPa(int pa){
this.physicsAttack = pa;
}
public int getPa(){
return this.physicsAttack;
}
public void setMa(int ma){
this.magicAttack = ma;
}
public int getMa(){
return this.magicAttack;
}
//judge hero whether liveing
public bool isLive(){
if(this.hp <= 0){
return false;
}
return true;
}
}
class BattleControl{ //战斗管理类
private List<Hero> ours; //存储己方阵营中五个英雄
private List<Hero> enemy; //(存储对方五个英雄)类型
public BattleControl(){
this.ours = new List<Hero>();
this.enemy = new List<Hero>();
}
//set get method:
public void setOurs(List<Hero> ours){
this.ours = ours;
}
public void setEnemy(List<Hero> enemy){
this.enemy = enemy;
}
public List<Hero> getOurs(){
return this.ours;
}
public List<Hero> getEnemy(){
return this.enemy;
}
public void addOurs(Hero h){
this.ours.Add(h);
}
public void addEnemy(Hero h){
this.enemy.Add(h);
}
//待改进:liveOurs和liveEnemy可修改为一个方法进行判断。。。
//有一个英雄活着则返回为true
public bool liveOurs(){
//traverse heros
int record = 0;
foreach (Hero h in (this.ours)){
if(h.isLive()){
record++;
return true;
}
}
if(0 == record)
return false;
return true;
}
public bool liveEnemy(){
//traverse heros
int record = 0;
foreach (Hero h in (this.enemy)){
if(h.isLive()){
record++;
return true;
}
}
if(0 == record)
return false;
return true;
}
}
class Test{
static void war(Hero a,Hero b){
//a 和 b 开始打斗
b.setHp(b.getHp()-a.getPa()); //b.hp = b.hp - a.physicsAttack;
b.setMp(b.getMp()-a.getMa()); //b.mp = b.mp - a.magicAttack;
a.setMp(a.getMp()-a.getMa());//a.mp = a.mp - a.magicAttack;
Console.WriteLine(a.getName()+"攻击了"+b.getName()+","+b.getName()+"剩余血量为"+b.getHp());
}
static int RandomNum(int len){
Random rd = new Random();
int random = rd.Next(len);
return random;
}
static void Main(){
//new some heros:
Hero h1 = new Hero("Sheldon",200,100,40,20);
Hero h2 = new Hero("Leonard",200,100,30,15);
Hero h3 = new Hero("Howard",200,100,20,14);
Hero h4 = new Hero("Raj",200,100,10,13);
Hero h5 = new Hero("Han",200,100,5,12);
Hero h6 = new Hero("Penny",200,100,40,30);
Hero h7 = new Hero("Bernadette",200,100,10,19);
Hero h8 = new Hero("Amy",200,100,10,16);
Hero h9 = new Hero("Max",200,100,45,10);
Hero h10 = new Hero("Caroline",200,100,25,7);
BattleControl bc = new BattleControl();
bc.addOurs(h1);
bc.addOurs(h2);
bc.addOurs(h3);
bc.addOurs(h4);
bc.addOurs(h5);
bc.addEnemy(h6);
bc.addEnemy(h7);
bc.addEnemy(h8);
bc.addEnemy(h9);
bc.addEnemy(h10);
//begin war
//己方开始对战
int round = 0; //记录战斗开始到结束总的回合数
int flag = 1; //己方为1,敌人为0
while(true){
if(1 == flag) {
int enemy_people = 0;
//算一下敌人活着的人数,用于随机敌人的抽取
foreach (Hero tmp in (bc.getEnemy())){
enemy_people++;
}
int ours_people = 0;
foreach (Hero tmp in (bc.getOurs())){
ours_people++;
}
//随机抽取一个敌人,使用RandomNum(ours_people);函数
List<Hero> enemy_list = bc.getEnemy();
Hero random_enemy = enemy_list[RandomNum(enemy_people)];
int record = 0;
foreach (Hero h_ours in (bc.getOurs())){
record++;
//让己方的每一个英雄轮流攻打敌人
war(h_ours,random_enemy);
if(random_enemy.getHp() < 0 ){
//若进此判断,证明random_enemy这个英雄应该被remove
bc.getEnemy().Remove(random_enemy);
if(!(bc.liveEnemy())){
break;
}
if(1 == enemy_people){
foreach (Hero tmp in (bc.getEnemy())){
if(tmp.isLive()){
random_enemy = tmp;
}
}
//random_enemy = enemy_list[0];
}
else if(enemy_people > 1){
random_enemy = enemy_list[RandomNum(enemy_people-1)];
}
else{
break;
}
}
}
}
//break;
if(-1 == flag) {
int enemy_people = 0;
foreach (Hero tmp in (bc.getEnemy())){
enemy_people++;
}
int ours_people = 0;
foreach (Hero tmp in (bc.getOurs())){
ours_people++;
}
List<Hero> ours_list = bc.getOurs();
Hero random_ours = ours_list[RandomNum(ours_people)];
int record = 0;
foreach (Hero h_enemy in (bc.getEnemy())){
record++;
war(h_enemy,random_ours);
if(random_ours.getHp() < 0 ){
bc.getOurs().Remove(random_ours);
if(!(bc.liveOurs())){
break;
}
if(1 == ours_people){
foreach (Hero tmp in (bc.getOurs())){
if(tmp.isLive()){
random_ours = tmp;
}
}
}
else{
random_ours = ours_list[RandomNum(ours_people-1)];
}
}
}
}
flag = -(flag);
//Console.WriteLine("flag = "+flag);
round++;
//当其中一方全部死亡时,跳出while loop
if( !(bc.liveOurs()) || !(bc.liveEnemy()) ){
break;
}
}
if( bc.liveOurs() && (!(bc.liveEnemy() ))){
Console.WriteLine("Ours is Successful! ");
}
else if( bc.liveEnemy() && (!(bc.liveOurs() ))){
Console.WriteLine("Enemy is Successful! ");
}
else {
Console.WriteLine("Equal! ");
}
Console.WriteLine("Sum Round = "+round);
}
}
使用dotnet run命令运行程序