Disabling structures

Started by
6 comments, last by BioSquirrel 22 years, 3 months ago
Yes, I am asking another question...I am running into so many problems when making this text game! Anyway, after you defeat an enemy, his HP is negative and you can still attack him and lower his HP even more. This is a problem because there are two enemies in this map and a variable "enemies" is set to 2. When you defeat an enemy, the variable named enemies lowers by 1. So if you defeat an enemy, it lowers once, and then you attack his dead body and it gets counted as a kill, ending the game with the other enemy untouched. Is there a way to disable Enemy1''s structure after you kill him so that you can''t attack him again and have to go for the second enemy?
Advertisement
Seems simple to me. Just don''t allow the player to kill dead bodies. At the relevant point in your code just check if the enemy''s hp is greater than zero.
I am pretty new at C++ so these things never cross my mind somehow. OK well this was the original ''if'' statement:

if (Enemy1.ycoord == Player1.ycoord)

Then I did this:

if (Enemy1.ycoord == Player1.ycoord, Enemy.HP > 0)

But that makes it an "or" statement and it is if enemy ycoord equals player1 ycoord OR Enemy HP is greater than 0...

Is there a way to replace "or" with "and"?
can''t you simply do a check like if enemy.hp<=0 then do not attack
else allow attack
or simply put a flag like alive for each enemy
Well, you can''t disable structures, but you can easily just not update them. Do as the previous poster said and check to see if the health of the enemy is less than or equal to 0, then break which loop or however you are doing it that allows the player to keep attacking and that should end it. Something like:

struct NPC_Enemy
{
char strName[81];
int iAttackStr;
int iHealth;
// etc....
}

// Must be const so we use it as an array size.
const int iNumEnemies = 2;

int iDeadEnemies = 0;

// Array of Enemies
NPC_Enemy EvilDwarfs[iNumEnemies];

// While we have less dead enemies than the number of enemies
// we have
while ( iDeadEnemies < iNumEnemies )
{
// Reset counter since we check everybody in the for
// loop anyways, and if it got here, everything is cool.
iDeadEnemies = 0;

for ( int i = 0; i < iNumEnemies; i++ )
{
// If it has no life, increment the dead counter.
if ( EvilDwarfs.iHealth <= 0 )
{
iDeadEnemies++;
}
else
{
// Whoever it''s turn is, have them attack, or defend.
}

}

}


"Artificial Intelligence: the art of making computers that behave like the ones in movies."www.CodeFortress.com
Well I tried:

if (Enemy1.HP <= 0, Player1.ycoord != Enemy1.ycoord){
}

else {
//Battle code here
}

This way it resets every turn so you can still attack a dead body.
The proper way to do an or in C++ is

if (condition1 || condition2) {
//code goes here
}

To do an and

if (condition1 && condition2) {
//code goes here
}

So you should have

if (Enemy1.ycoord == Player1.ycoord && Enemy1.HP > 0) {
//do stuff
}

In the future I''d suggest posting basic questions like this to the beginners forum.
Thanks!

This topic is closed to new replies.

Advertisement