PDA

View Full Version : C++ help


PlayaFoSho
08-30-2009, 04:39 AM
Hey I cant think right now and I need help with a calculation in my code... Ill post the whole function( its suppose to be a simple fight scene and is not finished. I need to figure out the "Phit" part first before I can finish it up.

void Fight()
{
int enemylife;
int playerlife;
int Phit;

string answer;

enemylife = 100;
playerlife = 100;
Phit = 0;

cout << " You encountered a giant serpent do you want to 'run away' or 'fight'?" <<endl;
cout << ">";
cin >> answer;

if (answer == "fight")
{
while (enemylife > 0 && playerlife > 0)
{


if (itemlocations[0] == 99)
{
bonusDamage = 20;
}
enemylife = enemylife - ((rand() % 5) + bonusDamage);

playerlife = playerlife - (rand() % 25);

long i;
for (i=1; i<=100000000;i++)
{
}
Phit = Phit +100 - enemylife ;
cout << "You have hit the Serpent for "<< Phit << "The Serpent has " << enemylife << " health remaining" << endl;
cout << endl;
if (enemylife > 0)
{
cout << " You have " << playerlife << " health remaining" << endl;
cout << endl;
}
}
if (enemylife <=0)
{
cout << " You have defeated the giant serpent!!!" << endl;
monsterDefeated = 1;
}
else
{
cout << " You have been defeated... you suck" << endl;
cin.get();
exit(0);
}

}
else
{
locationindex = 3;
}
cin.get();
}


it obviously doesnt work the way I want it to. I want the "Phit" to show how much I have hit the enemy. ive tried a few different things but I cant seem to get it to work correctly.


Thanks

PlayaFoSho
08-30-2009, 06:33 AM
I got it now. thanks to reality for the help.

keysx
08-30-2009, 03:08 PM
Are you making a text based adventure?!

PlayaFoSho
08-30-2009, 05:29 PM
yea... its for my programing class.

hellfighter87
09-01-2009, 01:01 PM
yea... its for my programing class.

nice :) but what was the mistake? cuase i looked over it a bit :o butcould not realy find bugs in 5 seconds :(

BTW 1 tip: my teacher said: a good function is no longer then 30 lines. it makes it easyer to overlook things and debug if you create more functions.
ALthouhg this was with java and c# but ithink it also counts for c++ :P

PlayaFoSho
09-01-2009, 04:09 PM
void Fight()
{
srand ( time(NULL) );

int enemylife;
int playerlife;
int Phit;
int Ehit;

string answer;

enemylife = 100;
playerlife = 100;
Phit = 0;
Ehit = 0;

cout << " You encountered a giant serpent do you want to 'run away' or 'fight'?" <<endl;
cout << ">";
cin >> answer;
cout << "\n\n";

if (answer == "fight")
{
//loop for the fight as long as both fighters HP is above 0
while (enemylife > 0 && playerlife > 0)
{
//Bonus damage if sword in inventory
if (itemlocations[0] == 99)
{
bonusDamage = 20;
}
//the fight calculations
Phit = ((rand() % 5) + bonusDamage);
enemylife = enemylife - Phit;
Ehit = (rand() % 25);
playerlife = playerlife - Ehit;

long i;
for (i=1; i<=100000000;i++)
{
}
//display fight sequence
if (enemylife > 0)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE), 3);
cout << "You have hit the Serpent for "<< Phit << " The Serpent has " << enemylife << " health remaining" << endl;
cout << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE), 12);
cout << " You have been struck for " << Ehit << " damage. You have "<< playerlife << " health remaining" << endl;
cout << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE), 15);
}
} //Prints win/loss
if (enemylife <=0)
{
cout << " You have defeated the giant serpent!!!" << endl;
monsterDefeated = 1;
}
else
{
cout << " You have been defeated... you suck" << endl;
cin.get();
exit(0);
}

} //bring you back to last room if you run away
else
{
locationindex = 3;
}
cin.get();
}

Thats what it is now that the fight part is finished/fixed.

hellfighter87
09-02-2009, 11:47 AM
nou you jsut need to add things like: multiple targets, multiple allies and maybe some thign siwth times s you can have something like attack speed :P i made a small php game that can handle this :P, although i never though of also making it in c++ :o

keysx
09-02-2009, 05:39 PM
You should probably pass the life of your combatants as arguments to the function to make it a bit more adaptive. then you can use the function to fight any enemy you want.

PlayaFoSho
09-02-2009, 07:31 PM
You should probably pass the life of your combatants as arguments to the function to make it a bit more adaptive. then you can use the function to fight any enemy you want.

I would if I decide to put more fights in the game... its only suppose to have 4 rooms, 3+ items, 1 enemy. we can make more if we want and I plan on making more in the future but for the moment its gonna have to stay the way it is.