所做东西的需求:
定义英雄类和Boss类,创建3个英雄对象,分别拿不同的武器,对敌人循环回合制攻击,输出战斗过程。
英雄类:特征:HP、MP、类型(枚举类型:弓箭手、法师、骑士)、武器 行为:攻击、防御
Boss类:特征:HP、攻击力、防御力 行为:攻击
using System;
using System.Collections;
using System.Collections.Generic;
namespace Exam
{
/// <summary>
/// 根据武器类型判断是否能进行装备
/// boss死亡掉落装备
/// 能够穿戴多件装备
/// 多人组队一起打Boss
/// </summary>
class MainClass
{
public static void Start()
{
bool flag1, flag2,flag3;
Weapon we1 = new Wand (40);
Weapon we2 = new Yataghan (20, 20);
Weapon we3 = new Bow (40);
Weapon we4 = new Bow (50);
Boss boss = new Boss (1000,50,50);
Hero hero = new Hero (500,60,"战神",20,30,heroType.弓箭手);
hero.addWeapon (we3);
hero.addWeapon (we4);
//Hero hero1=new Hero(500,60,"擎天",20,30,heroType.战士);
//hero1.addWeapon (we2);
//如果装备无法匹配
hero.addWeapon(we2);
while (true)
{
flag1=hero.hitBoss (boss);
if (!flag1)
{
break;
}
//flag3 = hero1.hitBoss (boss);
//if (!flag3)
//{
// break;
//}
flag2=boss.hitHero (hero);
if (!flag2)
{
break;
}
}
Console.WriteLine ();
Console.WriteLine (hero);
Console.WriteLine ("其中的装备属性如下:");
foreach (var item in hero.list)
{
Console.WriteLine (item);
}
}
public static void Main(string[] args)
{
Start ();
}
}
enum heroType
{
弓箭手,战士,法师
}
/// <summary>
/// 英雄类
/// </summary>
class Hero
{
private int hp;//英雄血量
private int ap;//英雄攻击力
private string name;//英雄名称
private int dp;//英雄防御力
private int speed;//英雄移动速度
private heroType type;//英雄的类型
//装备背包
public List<Weapon> list=new List<Weapon>();
public int Hp
{
get
{
return hp;
}
set
{
hp = value;
}
}
public int Ap
{
get
{
return ap;
}
set
{
ap = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Dp
{
get
{
return dp;
}
set
{
dp = value;
}
}
public int Speed
{
get
{
return speed;
}
set
{
speed = value;
}
}
public heroType Type
{
get
{
return type;
}
set
{
type = value;
}
}
//当前装备的武器的攻击力总和
public int sumWeapon=0;
//构造方法
public Hero(int hp,int ap,string name,int dp,int speed,heroType type)
{
this.hp = hp;
this.ap = ap;
this.name = name;
this.dp = dp;
this.speed = speed;
this.type = type;
}
//加装备
public void addWeapon(Weapon weapon)
{
if (this.type==heroType.弓箭手)
{
if (weapon.Type=="弓箭")
{
this.ap += weapon.addap;
list.Add (weapon);
Console.WriteLine ("装配弓箭成功!!!,英雄添加攻击力:{0}",weapon.addap);
Console.WriteLine ("-------------------------------------------");
}
else {
Console.WriteLine ("无法成功装配,因为武器类型不符!!!");
Console.WriteLine ("-------------------------------------------");
}
}
else if (this.type==heroType.战士)
{
if (weapon.type=="长剑")
{
this.ap += weapon.addap;
this.dp += weapon.addport;
list.Add (weapon);
Console.WriteLine ("装配长剑成功!!!,英雄添加攻击力:{0},防御力:{1}",weapon.addap,weapon.addport);
Console.WriteLine ("-------------------------------------------");
}
else {
Console.WriteLine ("无法成功装配,因为武器类型不符!!!");
Console.WriteLine ("-------------------------------------------");
}
}
else if (this.type==heroType.法师)
{
if (weapon.type=="法杖")
{
this.ap += weapon.addap;
list.Add (weapon);
Console.WriteLine ("装配法杖成功!!!,英雄添加攻击力:{0}",weapon.addap);
Console.WriteLine ("-------------------------------------------");
}
else
{
Console.WriteLine ("无法成功装配,因为武器类型不符!!!");
Console.WriteLine ("-------------------------------------------");
}
}
}
//攻击Boss
public bool hitBoss(Boss boss)
{
Random random = new Random ();
if (boss.Hp<=0)
{
Console.WriteLine ("Boss死亡!!!!!");
Console.WriteLine ("----------------------------------------------");
Console.WriteLine ("掉落装备如下:");
boss.DropEquip ();
Console.Write ("您是否要拾取全部装备(Y:拾取,N:不拾取):");
char ch =Convert.ToChar(Console.Read ());
switch (ch)
{
case ‘Y‘:
case ‘y‘:
for (int i = 0; i < boss.list.Count; i++)
{
list.Add (boss.list [i]);
}
Console.WriteLine ("拾取成功!!!!!");
break;
case ‘N‘:
case ‘n‘:
Console.WriteLine ("拾取失败!!!!!");
break;
default:
Console.WriteLine ("没有此选择,默认拾取了!!!!!");
for (int i = 0; i < boss.list.Count; i++)
{
list.Add (boss.list [i]);
}
Console.WriteLine ("拾取成功!!!!!");
break;
}
return false;
}
else if(boss.Hp>0&&hp>0)
{
for (int i = 0; i < list.Count; i++)
{
sumWeapon += list [i].addap;
}
int defend = random.Next (0, boss.Dp);
boss.Hp -= (this.ap +sumWeapon - defend);
Console.WriteLine ("Boss受到了{2} {0}点攻击,剩余血量:{1}",(ap + sumWeapon - defend),boss.Hp,name);
return true;
}
return true;
}
public override string ToString()
{
return string.Format("{2}英雄身上有{0}件装备,其剩余血量为{1},攻击力为:{3},防御力为:{4},速度为:{5},职业为:{6}",list.Count, Hp, Name,Ap, Dp, Speed, Type);
}
}
/// <summary>
/// Boss类
/// </summary>
class Boss
{
private int hp;
private int ap;
private int dp;
public List<Weapon> list=new List<Weapon>();
public int Hp
{
get
{
return hp;
}
set
{
hp = value;
}
}
public int Ap
{
get
{
return ap;
}
set
{
ap = value;
}
}
public int Dp
{
get
{
return dp;
}
set
{
dp = value;
}
}
public void DropEquip()
{
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine ("第{0}件装备属性如下:攻击力提升:{1},防御力提升:{2}",(i+1),list[i].addap,list[i].addport);
}
}
public Boss(int hp,int ap,int dp)
{
this.ap = ap;
this.hp = hp;
this.dp = dp;
Weapon we1 = new Wand (100);
Weapon we2 = new Yataghan (60, 40);
Weapon we3 = new Bow (80);
list.Add (we1);
list.Add (we2);
list.Add (we3);
}
//攻击英雄
public bool hitHero(Hero hero)
{
if (hero.Hp<=0)
{
Console.WriteLine ("英雄死亡!!!!!");
Console.WriteLine ("-------------------------------------------------");
return false;
}
else if(hero.Hp>0&&hp>0)
{
hero.Hp -= (this.Ap - hero.Dp);
Console.WriteLine ("{2}英雄受到了{0}点攻击,剩余血量为{1}",(this.Ap - hero.Dp),hero.Hp,hero.Name);
return true;
}
return true;
}
}
/// <summary>
/// 装备类
/// </summary>
public class Equipment
{
private string name;
}
/// <summary>
/// 武器类
/// </summary>
public abstract class Weapon:Equipment
{
//等级
private int grade;
//攻击力
private int ap;
public string type;
public string Type
{
get
{
return type;
}
}
//增加攻击力
public int addap;
//增加防御力
public int addport;
//攻击
public abstract void Attack();
}
/// <summary>
/// 法杖类
/// </summary>
class Wand:Weapon
{
public Wand(int addap)
{
this.addap = addap;
this.type = Type;
type="法杖";
}
public override void Attack()
{
Console.WriteLine ("进行了魔法攻击!!!!!");
}
public override string ToString()
{
return string.Format("武器为:法杖,增加的攻击力为:{0},增加的防御力为:{1}",addap,addport);
}
}
/// <summary>
/// 弓箭类
/// </summary>
class Bow:Weapon
{
public Bow(int addap)
{
this.addap = addap;
type="弓箭";
}
public override void Attack()
{
Console.WriteLine ("进行了物理攻击!!!!!");
}
public override string ToString()
{
return string.Format("武器为:弓箭,增加的攻击力为:{0},增加的防御力为:{1}",addap,addport);
}
}
/// <summary>
/// 长剑类
/// </summary>
class Yataghan:Weapon
{
public Yataghan(int addap,int power)
{
this.addap = addap;
this.addport = power;
type="长剑";
}
public override void Attack()
{
Console.WriteLine ("进行了物理攻击!!!!!");
}
public override string ToString()
{
return string.Format("武器为:长剑,增加的攻击力为:{0},增加的防御力为:{1}",addap,addport);
}
}
/// <summary>
/// 防具类
/// </summary>
class Power:Equipment
{
//防御力
private int dp;
//耐久度
private int durability;
//破损
public void Damaged()
{
}
}
}
在此应用设计的过程中遇到的问题为:当将一个类定义为抽象类的时候,抽象类中拥有的字段,在子类中千万不要进行定义,如果进行定义 那么子类所进行赋值的字段值将会覆盖父类其对应字段的值,这样会出现让你意想不到的错误,
当使用抽象类地时候 我们应该牢牢记住几点:
1.抽象类必须用abstract来修饰,抽象方法必须用abstract来修饰,抽象方法不能有方法体;
2.抽象类不能被实例化,无法用new来创建该类的实例;
3.包含抽象对象的类必须是抽象的。但是,可以定义一个不包含抽象方法的抽象类。在这种情况下,不能使用new操作符创建该类的实例。这种类是用来定义新子类的基类的。
4.含有抽象方法的类:直接在类中顶一个抽象的方法,或者继承了父类的抽象类,为完全实现,父类里面的方法,或者实现了一个接口,为全部实现里面的方法。
5.不能使用new操作符来从抽象类创建一个实例,但是抽象类可以作为一种数据类型。