题目描述
每一个网络游戏的角色都有很多属性,比如攻击力值,防御力值,法力值,血量值,法术值,幸运值等。当遇到怪物时,最关键的是攻击力值和防御值。现在输入角色和怪物的攻击力值和防御力值和血量 ,请判断角色是否能够打赢怪物。
规则是:
角色和怪物之间是回合制,由角色先手
血量的减少值是双方攻击值与防御值的差(保证攻击值均大于防御值)
如果角色能战胜怪物请输出“win”,否者输出“false”。
输入格式
两行,每行三个整数分别代表角色和怪物的血量值,攻击值,防御值。
输出格式
一个字符串,角色能战胜怪物输出“win”,否者输出“false”。
输入输出样例
输入 #1
100 50 10
40 20 5
输出 #1
win
输入 #2
100 30 10
200 35 8
输出 #2
false
关于 SOLO
函数:
while
循环模拟战斗,因为是角色先进行攻击,所以怪物先扣血 MonsterBloodValue -= HumanEAttackValue;
这时判断怪物血量是否为空,是就说明人类获胜,再进行人类被扣血,判断……
#include <iostream>
using namespace std;
class Game
{
public:
Game() {m_XL = 0; m_GJ = 0; m_FY = 0;}
~Game() {cout << "Game over~" << endl;}
int GetCurrentXL(); // 获取当前血量值
int GetCurrentGJ(); // 获取当前攻击力
int GetCurrentFY(); // 获取当前防御值
friend bool SOLO(Game& g1, Game& g2);
friend istream& operator >> (istream &in, Game& m); // 重载 >> 操作符,以便输入角色属性值
private:
int m_XL; // 血量值
int m_GJ; // 攻击力
int m_FY; // 防御值
};
int Game::GetCurrentXL()
{
return m_XL;
}
int Game::GetCurrentGJ()
{
return m_GJ;
}
int Game::GetCurrentFY()
{
return m_FY;
}
bool SOLO(Game& g1, Game& g2)
{
int HumanEAttackValue = g1.GetCurrentGJ() - g2.GetCurrentFY();
int HumanBloodValue = g1.GetCurrentXL();
int MonsterEAttackValue = g2.GetCurrentGJ() - g1.GetCurrentFY();
int MonsterBloodValue = g2.GetCurrentXL();
while (1)
{
MonsterBloodValue -= HumanEAttackValue;
if (MonsterBloodValue <= 0)
{
return true;
}
HumanBloodValue -= MonsterEAttackValue;
if (HumanBloodValue <= 0)
{
return false;
}
}
}
istream& operator >> (istream &in, Game& m)
{
in >> m.m_XL >> m.m_GJ >> m.m_FY;
return in;
}
int main()
{
Game human;
cout << "Please input human's attribute value: ";
cin >> human;
Game monster;
cout << "Please input monster's attribute value: ";
cin >>monster;
if (SOLO(human, monster))
{
cout << "Win." << endl;
}
else
{
cout << "False." << endl;
}
}