Attack function for a game

At my university, we have to build a prototype for a game, and my team and I have decided to make it in visual studio to show off the feathers of our game. The code is almost done, but I'm having an issue with my attack function. Once the enemy troop's health hit 0 or goes below 0, then I want the attacker to move and take the enemy position in the vector map. However, the attacker never moves if the enemy troop's health goes negative or if it hits 0 and the enemy's troop remains on the field. I can't seem to find my logical error in my code and was hope that one of you guys could help me out.

I believe the logical error exist in the following block of code because the second if statement never gets executed.

1
2
3
4
5
6
7
8
9
10
     if(board[fr][fc]->legal_attack(fr,fc,tr,tc,board))
                    {
                        board[fr][fc]->attack(board[tr][tc]);
                        if(board[tr][tc]->getHealth()<=0)
                        {
                            board[tr][tc]=NULL;
                            board[tr][tc]=board[fr][fc];
                            board[fr][fc]=NULL;
                            cout<<"Enemy troop was killed in battle!"<<endl;
                        }


The get health function returns the health of the troop at board[tr][tc], so when that number is <= 0 that code should set board[tr][tc]=NULL (killing the troop) and move the attacker to that position in the vector map. However, the if statement is not executed and I don't know why.
Last edited on
Because board[fr][fc]->legal_attack(fr,fc,tr,tc,board) is returning false (assuming that the function return type is bool). You gotta figure out why that function is always returning false.
Whoops, you said second if statement. In that case, health is not being properly decreased so that it gets down to zero. Check whatever code you use to decrease an entity's health (presumably the attack function) and make sure your logic is correct.
Last edited on
Ok, so I figured it out. board[fr][fc]->legal_attack(fr,fc,tr,tc,board) is returning true, but the logic error wasn't there in the main.cpp it was in the another function called do_damage in a header file.

So, I have a question about my logic error. I was setting the damage equal to health_ (a private value) so the code looked like this:

1
2
3
4
5
6
7
8
9
10
11
void NormalSoldier::do_damage(int attack, bool validAttack)
{
    if(validAttack)
    {
        health_-=(attack-defense_);
    }
    else
    {
        health_=health_;
    }
}


Then, I changed the code to the following, and it now works:
1
2
3
4
5
6
7
8
9
10
11
void NormalSoldier::do_damage(int attack, bool validAttack)
{
    if(validAttack)
    {
        setHealth(getHealth()-(attack-defense_));
    }
    else
    {
        setHealth(health_);
    }
}


I believe that these two pieces of code do the same thing, so way does the second piece of code work and the first piece of code fail (getHealth and setHealth are in a parent header file and being used in a different header file)?
Last edited on
Topic archived. No new replies allowed.