#include<iostream>
#include <string>
using namespace std;
void battle(string*, int*, int*, int*, string*, int*, int*, int*);
struct shuxing
{
string name;
int str;
int hp;
int def;
};
int main()
{
int *st, *h, *de;
//定义指针*st,*h,*de
string *nam;
string name;
cout << "输入你的游戏角色名:________\b\b\b\b\b\b\b\b";
getline(cin,name);
shuxing player = { name,10,100,0 };
//初始化player属性为str=10,hp=100,def=0
nam = &player.name;
st = &player.str; //指针st 为player的的str的内存地址
h = &player.hp; //指针h 为player的hp的内存地址
de = &player.def; //指针de为player的def的内存地址
shuxing diren1 = { "史莱姆",5,50,0 };
//初始化敌人
string *nam2, name2;
int *st2, *h2, *de2;
nam2 = &diren1.name;
st2 = &diren1.str;
h2 = &diren1.hp;
de2 = &diren1.def;
battle (nam, st, de, h, nam2, st2, de2, h2);
system("pause");
return 0;
}
void battle(string*nam, int*st, int*de, int*h, string*nam2, int*st2, int*de2, int*h2)
{
system("cls");
int a = *st;
int b = *de;
int c = *h;
int d = *h2;
cout << "勇士 " << *nam << " 遇见一个敌人:" << *nam2 << endl;
cout << "——————————" << endl;
cout << "你的属性为:" << endl;
cout << "攻击力:" << *st << endl;
cout << "防御力:" << *de << endl;
cout << "生命值:" << *h << endl;
cout << "——————————" << endl;
cout << "敌人:" << *nam2 << "的属性为" << endl;
cout << "攻击力:" << *st2 << endl;
cout << "防御力:" << *de2 << endl;
cout << "生命值:" << *h2 << endl;
cout << "——————————" << endl;
int mov1 = 0;
//初始化move为敌人行动回合数
unsigned int change = 0;
//初始化主角的选项
while (*h2 > 0)
{
cout << "请选择你的行动:\n1.攻击\n2.防御" << endl;
cin >> change;
cout << endl;
if (change > 2) { cout << "选择错误,请重新选择...";cin >> change; cout << endl;}
if (change == 0) { cout << "选择错误,请重新选择...";cin >> change; cout << endl;}
if (change == 1)
{
mov1 = mov1 + 1;
if (*st <= *de2) { cout << "你的攻击无法攻破对方防御,对方没有损失血量。"<< endl; }
else
{
*h2 = *h2 - *st + *de2;
cout << "你的攻击对" << *nam2 << "造成了" << *st - *de2 << "点伤害。" << *nam2 << "还剩下" << *h2 << "点生命。" << endl;
}
if (*st2 <= *de) { cout << "敌人的攻击无法攻破你的防御,你没有损失血量。" << endl;}
else
{
*h = *h - *st2 + *de;
cout << *nam2 << "的攻击对你造成了" << *st2 - *de << "点伤害。你还剩下" << *h << "点生命" << endl;
}
if (mov1 == 3)
{
mov1 = 0;
*h = *h - *st2 - *st2;
cout << *nam2 << "发动的追加攻击,对你造成了" << *st2 + *st2 << "点伤害。你还剩下" << *h << "点生命" << endl;
}
cout << endl;
}
if (change == 2)
{
mov1 = mov1 + 1;
if (*st2 <= (*de * 2 + 5)) { cout << "敌人的攻击无法攻破你的防御,你没有损失血量。" << endl; }
else
{
*h = *h - *st2 + (*de * 2 + 5);
cout << *nam2 << "的攻击对你造成了" << *st2 - (*de * 2 + 5) << "点伤害。你还剩下" << *h << "点生命" << endl;
}
if (mov1 == 3)
{
mov1 = 0;
if ((*st2 * 2) <= (*de * 2 + 10)) { cout << "敌人发动了追加攻击,但并未对你造成任何伤害" << endl; }
else
{
*h = *h - (*st2 * 2) + (*de * 2 + 10);
cout << *nam2 << "发动的追加攻击,对你造成了" << (*st2 * 2) - (*de * 2 + 10) << "点伤害。你还剩下" << *h << "点生命" << endl;;
}
}
cout << endl;
}
}
cout << "恭喜你,勇士!你成功击杀了" << *nam2 << endl;
*h = c + 0.2*d;
*st = a + 0.5*(*st2);
*de = b + 1 + 0.5*(*de2);
cout << "你获得血量增长" << 0.2*d << "点";
cout << "你获得攻击力增长" << 0.5*(*st2) << "点";
cout << "你获得防御力增长" << 1 + 0.5*(*de2) << "点";
cout << "————————————" << endl;
cout << "你目前属性为:" << endl;
cout << "攻击力:" << *st << endl;
cout << "防御力:" << *de << endl;
cout << "生命值:" << *h << endl;
}