如何将枯竭的健康带到下一个循环?

时间:2018-03-31 14:49:51

标签: c++ loops while-loop

我有这个程序来实例化玩家角色,敌人角色和他们的武器。玩家角色选择攻击者,敌人随机攻击玩家。他们随意设置自己的健康,位置和武器伤害。我还有一个武器功能dodamage(),它会通过武器伤害降低玩家的健康状况。在main()中,我设置了一个while(true)循环,每个角色攻击并耗尽健康。问题是在第一次之后的每个循环,健康和损害与开始时相同。它们在第一次循环后永远不会改变。我想知道如何将所造成的伤害带到下一个循环,直到生命值达到<= 0。

Main.cpp的

void playerAttack(Character c1, Character c2, Weapon w, string target)
{
if (target == "1")
{
    w.DoDamage(c1.healthPoints, w.Damage);
    if (c1.healthPoints <= 0)
        c1.die();
 }
    else if (target == "2") 
{
    w.DoDamage(c2.healthPoints, w.Damage);
    if (c2.healthPoints <= 0)
        c2.die();
}
}

void enemyAttack(Character c1, Character c2, Weapon w)
{
srand(time(NULL));
int randTarget = rand() % 2 + 1;
if (randTarget = 1)
{
    w.DoDamage(c1.healthPoints, w.Damage);
    if (c1.healthPoints <= 0)
        c1.die();
}
else if (randTarget = 2)
{
    w.DoDamage(c1.healthPoints, w.Damage);
    if (c2.healthPoints <= 0)
        c2.die();
}
}

int main()
{
PlayerCharacter p1, p2;
EnemyCharacter e1, e2;
Weapon p1W = p1.currentWeapon;
Weapon p2W = p2.currentWeapon;
Weapon e1W = e1.currentWeapon;
Weapon e2W = e2.currentWeapon;

string enemyTarget, playerTarget;

p1.printCharacter();
p2.printCharacter();
e1.printCharacter();
e2.printCharacter();


while (true) 
{
    cout << "Player One. Choose your Target (1 or 2)" << endl;
    cin >> playerTarget;
    playerAttack(e1, e2, p1W, playerTarget);


    cout << "Enemy One Attacks" << endl;
    enemyAttack(p1, p2, e1W);

    cout << "Player Two. Choose your Target (1 or 2)" << endl;
    cin >> playerTarget;
    playerAttack(e1, e2, p2W, playerTarget);


    cout << "Enemy Two Attacks" << endl;
    enemyAttack(p1, p2, e2W);   
}

if (p1.isDead && p2.isDead)
    cout << "Game Over!" << endl;
else if (e1.isDead && e2.isDead)
    cout << "You Win!" << endl;

system("pause");
return 0;
}

Weapon.cpp

void Weapon::printWeapon() 
{
srand(time(NULL));
cout << "Weapon: " << WeaponType[randomName] << endl;;
cout << "Type: " << object->WeaponName[randomType] << endl;
cout << "Damage: " << Damage << endl;;
}
int Weapon::DoDamage(int health, int damage) 
{
int resultHealth = health - damage;

cout << "Damage - " << damage << endl;
cout << "Health: " << resultHealth << endl << endl;
health = resultHealth;
return health;
}

Character.cpp(玩家角色和敌人角色的基类)

Character::Character()
{
srand(time(NULL));
populatePosition(3);
}

void Character::printCharacter() 
{
srand(time(NULL));
healthPoints = rand() % 100;



cout << "Enter First Name" << endl;
cin >> firstName;
cout << "Enter Last Name" << endl;
cin >> lastName;
cout << endl;
cout << firstName << " " << lastName << ": " << endl;
cout << "Health: " << healthPoints << endl;
cout << "Position: "<<endl;

for (auto p:position) 
{   
    cout << p << ", " << endl;
}
cout << endl;

}

void Character::populatePosition(int vectorSize) 
{
for (int i = 0; i < vectorSize; i++) 
{
    f = rand() % 20;
    position.push_back(f);
}
}

void Character::die()
{
cout << firstName << " " << lastName << " is dead " << endl << endl;
isDead = true;
}

1 个答案:

答案 0 :(得分:1)

问题在于DoDamage永远不会更新其范围之外的任何内容。 您按值传递健康状况,因此DoDamage的结果永远不会被使用。您也没有捕获返回值的结果,也可以用来更新值。

要修复它,要么A,要使DoDamage的heath参数通过引用传递

int Weapon::DoDamage(int & health, int damage) 
{
int resultHealth = health - damage;

cout << "Damage - " << damage << endl;
cout << "Health: " << resultHealth << endl << endl;
health = resultHealth;
return health;
}

引用基本上就像指向数据的指针,但是你不必记得取消引用它们,编译器会为你记住它。您只需要记住,进入的值与您正在使用的值处于同一地址。

或者B,使用返回的值重新分配攻击函数中角色的健康点。

e.g:

w.DoDamage(c1.healthPoints, w.Damage);

看起来像:

c1.healthPoints = w.DoDamage(c1.healthPoints, w.Damage);