OJ第三批——Problem T: P4 游戏中的Human角色
来源:互联网 发布:上海一橙网络怎么样 编辑:程序博客网 时间:2024/05/06 19:06
问题及代码:
Problem T: P4 游戏中的Human角色
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 449 Solved: 270
[Submit][Status][Web Board]
Description
在一个平面打斗游戏中,任何的角色(Role)都有血量(blood)和位置loc(此处loc是Location类的实例)属性。有了Role类,可以派生出不同的角色,如人、神仙、怪兽等。如下程序中,定义了Location类和Role类,人类(Human)中新增了姓名和攻击力数据成员,请为Human类设计成员函数,并实现Role类中的moveTo和addBlood两个成员函数。
请在begin和end中间写下需要的代码。你只能编辑并提交begin和end之间的代码。
#include <iostream>
using namespace std;
class Location
{
private:
int x, y;
public:
Location(int a, int b):x(a),y(b) {}
int getX(){return x;}
int getY(){return y;}
void setXY(int a,int b) {x=a;y=b;}; //设置位置坐标
};
class Role
{
public:
Role(int rblood, int rx, int ry):blood(rblood),loc(rx,ry) {}
void moveTo(int rx, int ry); //移动到(rx, ty)处,要改变loc的值
void addBlood(int b); //增加血量,参数为负时,代表减少
protected:
int blood;
Location loc;
};
void Role::moveTo(int rx, int ry)
{
loc.setXY(rx,ry);
}
void Role::addBlood(int b)
{
blood+=b;
}
{
loc.setXY(rx,ry);
}
void Role::addBlood(int b)
{
blood+=b;
}
//************* begin *****************
class Human: public Role
{
public:
private:
string name; // 姓名
int attack; // 攻击力
};
//************* end *****************
int main()
{
string name;
int att, blood, x, y;
cin>>name>>att>>blood>>x>>y;
Human hum(name,att,blood,x,y); //人name的攻击力att,血量blood,在(x,y)处
hum.show();
int incAtt, incBlood, newx, newy ;
cin>>incAtt;
cin>>incBlood;
cin>>newx>>newy;
hum.addAttack(incAtt); //攻击力增加incAtt
hum.addBlood(incBlood); //血量增加incBlood
hum.moveTo(newx,newy); //人移到了(news,newy)处
hum.show();
return 0;
}
Input
第一行:名字 血量 攻击力 位置,其中位置处是两个整数,代表平面x、y坐标
第二行:增加的攻击力
第三行:要增加的血量
第四行:新位置,用两个整数代表
输入的各部分之间用空格隔开
Output
分别用两行显示游戏角色的初始状态和调整参数后的状态
如“Avanda has 500 attack and 1000 blood in (4,3)”表示Avanda有500攻击力1000血量,在(4,3)位置处
Sample Input
Avanda 500 1000 4 3-3002002 5
Sample Output
Avanda has 500 attack and 1000 blood in (4,3)Avanda has 200 attack and 1200 blood in (2,5)
HINT
#include <iostream>using namespace std;class Location{private: int x, y;public: Location(int a, int b):x(a),y(b) {} int getX() { return x; } int getY() { return y; } void setXY(int a,int b) { x=a; y=b; }; //设置位置坐标};class Role{public: Role(int rblood, int rx, int ry):blood(rblood),loc(rx,ry) {} void moveTo(int rx, int ry); //移动到(rx, ty)处,要改变loc的值 void addBlood(int b); //增加血量,参数为负时,代表减少protected: int blood; Location loc;};void Role::moveTo(int rx, int ry){ loc.setXY(rx,ry);}void Role::addBlood(int b){ blood+=b;}//************* begin *****************class Human: public Role,public Location{public: Human(string na,int att,int rblood,int rx,int ry) :Role(rblood,rx,ry),Location(rx,ry),name(na),attack(att) {} void show(); void addAttack(int n); void addBlood(int m); void moveTo(int rx,int ry);private: string name; // 姓名 int attack; // 攻击力};void Human::addAttack(int n){ attack+=n;}void Human::addBlood(int m){ blood+=m;}void Human::show(){ cout<<name<<" has "<<attack<<" attack and "<<blood<<" blood in ("<<Location::getX()<<","<<Location::getY()<<")"<<endl;}void Human::moveTo(int rx,int ry){ Location::setXY(rx,ry);}//************* end *****************int main(){ string name; int att, blood, x, y; cin>>name>>att>>blood>>x>>y; Human hum(name,att,blood,x,y); //人name的攻击力att,血量blood,在(x,y)处 hum.show(); int incAtt, incBlood, newx, newy ; cin>>incAtt; cin>>incBlood; cin>>newx>>newy; hum.addAttack(incAtt); //攻击力增加incAtt hum.addBlood(incBlood); //血量增加incBlood hum.moveTo(newx,newy); //人移到了(news,newy)处 hum.show(); return 0;}
运行结果:
0 0
- OJ第三批——Problem T: P4 游戏中的Human角色
- YTU-OJ-P4 游戏中的Human角色
- OJ积累--P4游戏中的human角色
- P4 游戏中的Human角色
- YTU 2635: P4 游戏中的Human角色
- [YTU]_2635(P4 游戏中的Human角色)
- OJ第三批——Problem I:投简历
- OJ第三批——Problem M: 虚基类练习:动物
- OJ第三批——Problem A :实现复数类中的加运算符重载【C++运算符重载】
- OJ第三批——Problem B:分数类的四则运算【C++】
- OJ第三批——Problem D: C++习题 输入输出--公有继承
- OJ第三批——Problem E:C++习题 输入输出--私有继承
- OJ第三批——Problem F:C++习题 输入输出--保护继承
- OJ第三批——Problem G:C++习题 多重继承
- OJ第三批——Problem H:C++习题 继承与组合
- OJ第三批——Problem J:C++复数运算符重载(+与<<)
- OJ第三批——Problem K:C++ 长方体继承自矩形
- OJ第三批——Problem L: C++继承(改错题)
- Android中控件ListView列表控件baseadapter方法介绍
- 给安卓(Android)手机装上Linux系统
- 《Java程序实习》日记(周三)
- 记录:C++11 新特性基本数据类型
- c语言数据类型陷阱
- OJ第三批——Problem T: P4 游戏中的Human角色
- Qtouch技术
- android之LruCache源码解析
- 鸟哥的Linux私房菜-----2、硬盘“充电”
- 删除字符串中出现次数最少的字符
- 统计图形-直方图
- 遇到的编码规范问题1
- 动态链表
- 鸟哥的Linux私房菜-----3、U盘装Linux系统