/2016.5.10修改/
问题及代码:
/*
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:main.cpp
* 作 者:赵志君
* 完成日期:2016年5月10日
* 版 本 号:v1.0
*
* 问题描述:在上周的游戏角色类的基础上加上武器
*/
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
class Weapen
{
private:
int li;
string nam;
public:
Weapen(string,int l=1);
int getli(){return li;}
string getname(){return nam;}
};
Weapen::Weapen(string n,int l)
{
nam=n;
li=l;
}
class Role
{
public:
Role(string nam,int b,string na,int l);
void show();
void attack(int);
void eat(int );
void beAttack(int );
private:
string name;
int blood;
Weapen weapen;
};
Role::Role(string nam,int b,string na,int l):name(nam),blood(b),weapen(na,l){}
void Role::show() //展现角色当前状态
{
cout<<"角色名: "<<name<<" 剩余血量: "<<blood<<endl;
cout<<"武器名: "<<weapen.getname()<<"武器攻击力: "<<weapen.getli()<<endl;
if(blood>0)
cout<<"alived"<<endl;
else
cout<<"dead"<<endl;
}
void Role::attack(int n) //攻击他人
{
blood=blood+n+weapen.getli();
}
void Role::eat(int n) //吃东西加血
{
switch(n)
{
case 0: blood=blood+1;
case 1: blood=blood+2;
case 2: blood=blood+3;
}
}
void Role::beAttack(int n) //被人攻击失n血
{
blood=blood-n;
}
int main()
{
srand(time(0));
Role mary("Mary",20,"yitian",rand()%3);
mary.show();
mary.attack(rand()%2);
mary.eat(rand()%3);
mary.beAttack(rand()%5);
mary.beAttack(rand()%5);
mary.beAttack(rand()%5);
mary.beAttack(rand()%5);
mary.beAttack(rand()%5);
mary.beAttack(rand()%5);
mary.show();
return 0;
}
运行结果:
学习心得:由于上一个代码未运用类的组合,故作此修改
/
问题及描述:
/*
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:main.cpp
* 作 者:赵志君
* 完成日期:2016年4月5日
* 版 本 号:v1.0
*
* 问题描述:在上周的游戏角色类的基础上加上武器
*/
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
class Role
{
public:
void setRole(string ,int);
void show();
void attack(int );
void eat(int );
void beAttack(int );
void wuqi(int l=1,int mo=100);
private:
string name;
string nam;
int blood;
int li;
int mosun;
};
void Role::wuqi(int l,int mo) //武器建立
{
li=l;
mosun=mo;
nam="倚天剑";
}
void Role::setRole(string nam,int n) //角色创建,名字命名,血量设置
{
name=nam;
blood=n;
}
void Role::show() //展现角色当前状态
{
cout<<"角色名: "<<name<<" 剩余血量: "<<blood<<endl;
cout<<"武器名: "<<nam<<" 攻击力: "<<li<<" 耐久值: "<<mosun<<endl;
if(blood>0)
cout<<"alived"<<endl;
else
cout<<"dead"<<endl;
}
void Role::attack(int n) //攻击他人
{
if(mosun<=0)
{
cout<<"武器已损坏"<<endl;
blood=blood+n;
}
else
{
blood=blood+(n+li);
mosun=mosun-li-n;
}
}
void Role::eat(int n) //吃东西加血
{
switch(n)
{
case 0: blood=blood+1;
case 1: blood=blood+2;
case 2: blood=blood+3;
}
}
void Role::beAttack(int n) //被人攻击失n血
{
blood=blood-n;
}
int main()
{
srand(time(0));
Role mary;
mary.setRole("Mary",20);
mary.wuqi(rand()%3,rand()%200);
mary.show();
mary.attack(rand()%2);
mary.eat(rand()%3);
mary.beAttack(rand()%5);
mary.beAttack(rand()%5);
mary.beAttack(rand()%5);
mary.beAttack(rand()%5);
mary.beAttack(rand()%5);
mary.beAttack(rand()%5);
mary.show();
return 0;
}
运行结果:
学习心得:学习不够深入,一些功能无法打出来,还需加强努力