为什么我的布尔变量自动切换?

时间:2018-04-08 14:19:46

标签: c++ boolean

我有一个程序,玩家和敌人可以在一个循环中相互攻击,当生命值达到零时,bool isDead变为true,当两个玩家或两个敌人都死亡时allDead(c1, c2)变成了真的。当一个角色的死亡是真的时候,我就跳过它们。但我一直在设置断点,我注意到角色的isDead会自动转为true,即使它们仍然健康。我看了一切,我只是不明白我错过了什么。 isDead已初始化为false,与变量关联的所有行都用于检查其是真还是假。请有人帮忙指出我错过了什么?谢谢。

isDead Function

bool Character::isDead() {
if (dead = true)
    return true;
}

主要

int main()
{


srand(time(NULL));  

PlayerCharacter* p1 = new PlayerCharacter();
PlayerCharacter* p2 = new PlayerCharacter();
EnemyCharacter* e1 = new EnemyCharacter();
EnemyCharacter* e2 = new EnemyCharacter();

p1->printCharacter();
p2->printCharacter();
e1->printCharacter();
e2->printCharacter();


string enemyTarget, playerTarget;

list<Character*> players;
list<Character*> enemies;

players.push_back(p1);
players.push_back(p2);
enemies.push_back(e1);
enemies.push_back(e2);


list<Character*>::iterator PinsertIt = players.begin();
list<Character*>::iterator EinsertIt = enemies.begin();



for (; PinsertIt != players.end() && EinsertIt != enemies.end(); ++PinsertIt, ++EinsertIt)
{

    listShuffle(players);
    listShuffle(enemies);

    if (*PinsertIt == p1)
    {
        if (!p1->isDead())////isDead switches to true when I reach this line
        {
            cout << "Player One. Choose your Target (1 or 2)" << endl;
            cin >> playerTarget;
            playerAttack(e1, e2, p1->currentWeapon, playerTarget);
        }
        else
        {
            cout << p1->firstName << " " << p1->lastName << " is dead" << endl;
        }
    }
    else if (*PinsertIt == p2)
    {
        if (!p2->isDead())
        {
            cout << "Player Two. Choose your Target (1 or 2)" << endl;
            cin >> playerTarget;
            playerAttack(e1, e2, p2->currentWeapon, playerTarget);
        }
        else
        {
            cout << p2->firstName << " " << p2->lastName << " is dead" << endl;
        }
    }


    if (*EinsertIt == e1)
    {

        if (!e1->isDead())
        {
            cout << "Enemy One ";
            enemyAttack(p1, p2, e1->currentWeapon);
        }
        else
        {
            cout << e1->firstName << " " << e1->lastName << " is dead" << endl;
        }
    }
    else if (*EinsertIt == e2)
    {
        if (!e2->isDead())
        {
            cout << "Enemy Two ";
            enemyAttack(p1, p2, e2->currentWeapon);
        }
        else
        {
            cout << e2->firstName << " " << e2->lastName << " is dead" << endl;
        }
    }



    if (allDead(p1, p2)) 
    {
        cout << "Game Over!" << endl;
    }
    else if (allDead(e1, e2)) 
    {
        cout << "You Win!" << endl;
    }
}




system("pause");
return 0;
}

0 个答案:

没有答案