传递给dAttack方法的Player对象未在dAttack(player)上更新其hp。巨龙受到的伤害完全可以,但是不会对玩家造成伤害。我知道用C ++在您的主要函数中定义类可能不是最佳实践,但是当我尝试将它们放入自己的.cpp文件时,出现错误,指出“ Player”和“ Dragon”对象未初始化。
我试图将dAttack()中的hit变量重命名为dHit,但没有其他想法。
#include <iostream>
#include <string>
int main()
{
class Player {
public:
float hMul;
float dMul;
float aMul;
float hp;
void setHMul(float h) { hMul = h; };
void setDMul(float d) { dMul = d; };
void setAMul(float a) { aMul = a; };
void setHP() { hp = hMul * 100; };
};
class Dragon {
public:
float dHp;
float dAt;
void dAttack(Player player) {
float dhit = dAt * 2;
player.hp -= dhit;
std::cout << "The dragon has hit you for: " << dhit << std::endl;
std::cout << "You have " << player.hp << " hp left!" << std::endl;
}
void pAttack(Player player) {
float hit = player.dMul * 2 + 3;
dHp -= hit;
std::cout << "You have hit the dragon for: " << hit << std::endl;
std::cout << "The dragon has " << dHp << " hp left!" << std::endl;
}
};
bool playing = true;
std::cout << "Hello, please pick a character, (W)arrior, (Wi)zard or (R)ogue: ";
std::string choice;
std::cin >> choice;
float hMul;
float dMul;
float aMul;
if (choice == "W" || choice == "w" || choice == "Warrior" || choice == "warrior" || choice == "WARRIOR") {
hMul = 10.0;
dMul = 0.5;
aMul = 3.0;
}
else if (choice == "Wi" || choice == "wi" || choice == "WI" || choice == "Wizard" || choice == "wizard" || choice == "WIZARD") {
hMul = 5.0;
dMul = 5.0;
aMul = 1.0;
}
else if (choice == "R" || choice == "r" || choice == "Rogue" || choice == "rogue" || choice == "ROGUE") {
hMul = 3.0;
dMul = 10.0;
aMul = 1.5;
}
Player player;
player.setHMul(hMul);
player.setDMul(dMul);
player.setAMul(aMul);
player.setHP();
Dragon dragon;
dragon.dAt = 100.0;
dragon.dHp = 10000.0;
while (playing) {
if (dragon.dHp > 0 && player.hp > 0) {
dragon.pAttack(player);
if (dragon.dHp > 0) {
dragon.dAttack(player);
}
else {
std::string ch;
std::cout << "The dragon has been defeated, would you like to play again? (y/n): ";
std::cin >> ch;
}
}
else if (dragon.dHp <= 0) {
std::string ch;
std::cout << "The dragon has been defeated, would you like to play again? (y/n): ";
std::cin >> ch;
}
else if (player.hp <= 0) {
std::string ch;
std::cout << "You have been defeated, would you like to play again? (y/n): ";
std::cin >> ch;
}
}
}
示例输出:
You have hit the dragon for: 4
The dragon has 20 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 16 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 12 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 8 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 4 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 0 left!
The dragon has been defeated, would you like to play again? (y/n):
答案 0 :(得分:2)
一些一般性建议首先
该问题的解决方案可能是这样的:
void take_dmg(int x) { x -= 1; }
int main() {
int hp = 10;
while (hp > 0) {
take_dmg(hp);
}
}
这将永远循环,并且hp
中的main
的值永远不会与10
相同。这是因为函数中的x
是hp
的副本。您想要的是
void take_dmg(int& x) { x -= 1; }
//^^ ------------------ pass by reference
int main() {
int hp = 10;
while (hp > 0) {
take_dmg(hp);
}
}
现在,x
是对hp
的引用。松散地说,引用只是原始值的别名,因此函数中的x
递减会hp
中的main
递减。
您应该注意的术语是“按值传递”(第一个示例已损坏)和“按引用传递”(固定的)。
最后,不要在函数范围内声明类。在某些情况下,这是一种必要的语言功能,但一般而言,类应在函数外部(即也在main
外部)声明。