# Thread: calculating attack and defense

1. ## calculating attack and defense

im stuck whats a good formula for calculating attack and defense?
grr im stumped i dont need anyn code i just need a good formula?

2. Need a little more to work with than just that. Presumably you have two (or more) entites/objects that have some sort of statistics related to these attributes to which you need to add a little bit of randomness to things in order to perform some calculation/check?

3. err im just using attack and defense variables nothing special
im looking for something like attack + 10% / defense but obviously thats no good ..

4. Of course, because that helps.

For all we know it could be a fork attacking a pea.

5. You're that stats guy from the recent thread about pointers. I know you have more code than you're suggesting: you already said you were making a text-based RPG. In that case using the stats as part of the calculation would be perfect.

damage = (strength + speed) * luck
or something.

6. haha ya yur write lol
but there are no speed or luck variables ill post my code

Code:
```#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

void create();

void createdata(string race);

void attack();

int yn;
struct stats{
string huname;
string elname;
string maname;
string dename;
string race;
int lvl;
int hel;
double att;
double def;
double mage;
double arch;
double agi;
double mon;
double exp;
};
stats player;
stats opponent;
int main()
{
string select;
do
{
cout<<"welcome to my little game i like to call\n";
cout<<"FABLE RIPOFF\n";
cout<<"\n\n\n";
cout<<"tutorial is recommended for beginners\n\n";
cout<<"play game(type play)\n";
cout<<"tutporial(type tut)\n";
cout<<"quit(type quit)\n";
cin>>select;
cout<<endl;
if(select == "play" || select == "Play")
{
system("cls");
create();
cin.get();
}
if(select == "tut" || select == "Tut")
{
cout<<"under construction\n";
cin.get();
}
if(select == "quit" || select == "Quit")
{
break;
}
cin.get();
}while(select != "quit" || select != "Quit");
}
void create(){
string race;
string yn;
cout<<"which race do you want to be?\n";
cout<<"\n\n";
cout<<"human(press h)\n";
cout<<"elf(press e)\n";
cout<<"mage(press m)\n";
cout<<"demon(press d)\n";
cin>>race;
if(race == "h" || race == "H" )
{
cout<<"humans are not a preferred race for battles mainly because of there\n";
cout<<"stupidity,they are however the only race that shops will talk to\n";
cout<<"but because of this they cant go into other lands shops other than\n";
cout<<"philostone.at the moment no human has survived even a minimal attack\n";
cout<<"from another monster,will you?\n";
cout<<"are you sure you want to be human\n";
cin>>yn;
if(yn == "y" || yn == "Y")
{
createdata(race);
}
else
{
system("cls");
create();
};
}
else if(race == "e" ||race == "E")
{
cout<<"elves are an amazing race,known for there absoloute mastery in \n";
cout<<"archery,choosing this race will assure you a spot in the archers\n";
cout<<"guild,they are also the only race other than mages to be\n";
cout<<"able to do magic,but dont let that fool you there no mages\n";
cout<<"but there very small which means this is what they have to rely\n";
cout<<"on just magic and archery but being small gives them amazing speed\n";
cout<<"allowing them to flee from every battle they face\n";
cout<<"are you sure you want to be an elf?\n";
cin>>yn;
if(yn == "y" || yn == "Y")
{
createdata(race);
}
else
{
system("cls");
create();
};
}
else if(race == "m" || race == "M")
{
cout<<"mages are the most powerfull magicians and also most likely\n";
cout<<"to finish the game to the very end,and being able to join\n";
cout<<"the magic guild gives them the arsenal of magic they need\n";
cout<<"provided they have the money to buy the spells of coarse\n";
cout<<"are you sure you want to be a mage?\n";
cin>>yn;
if(yn == "y" || yn == "Y")
{
createdata(race);
}
else
{
system("cls");
create();
};
}
else if(race == "d" || race == "D")
{
cout<<"BRUTE strenth there dumb as a doorknob but stronger than \n";
cout<<"anything else in the world\n";
cout<<"nobody whose fought a demon in a brute strenth battle\n";
cout<<"has lived to tell the tale\n";
cout<<"are you sure you want to be a demon?\n";
cin>>yn;
if(yn == "y" || yn == "Y")
{
createdata(race);
}
else
{
system("cls");
create();
};
};
}
void createdata(string race){
if(race == "h" || race =="H")
{
string huname;
cin>>huname;
system("cls");
cout<<"hmm "<< huname <<" if your sure\n";
player.lvl = 1;
player.hel = 100;
player.att = 3;
player.def = 2;
player.agi = 3;
player.mon = 0;
player.exp = 0;
cout<<"level "<<player.lvl<<endl;
cout<<"health "<<player.hel<<endl;
cout<<"attack "<<player.att<<endl;
cout<<"defense "<<player.def<<endl;
cout<<"agility "<<player.agi<<endl;
cout<<"money "<<player.mon<<endl;
cout<<"experience "<<player.exp<<endl;
}
else if(race == "e" || race == "E")
{
string elname;
cin>>elname;
system("cls");
cout<<"hmm "<< elname <<" if your sure\n";
player.lvl = 1;
player.hel = 100;
player.att = 2;
player.def = 2;
player.agi = 3;
player.arch = 4;
player.mage = 1;
player.mon = 0;
player.exp = 0;
cout<<"level "<<player.lvl<<endl;
cout<<"health "<<player.hel<<endl;
cout<<"attack "<<player.att<<endl;
cout<<"defense "<<player.def<<endl;
cout<<"agility "<<player.agi<<endl;
cout<<"archery "<<player.arch<<endl;
cout<<"mage "<<player.mage<<endl;
cout<<"money "<<player.mon<<endl;
cout<<"experience "<<player.exp<<endl;
}
else if(race == "m" || race == "M")
{
string maname;
cin>>maname;
system("cls")
cout<<"hmm "<< maname <<" if your sure\n";
player.lvl = 1;
player.hel = 100;
player.att = 1;
player.def = 1;
player.agi = 2;
player.mage = 5;
player.mon = 0;
player.exp = 0;
cout<<"level "<<player.lvl<<endl;
cout<<"health "<<player.hel<<endl;
cout<<"attack "<<player.att<<endl;
cout<<"defense "<<player.def<<endl;
cout<<"agility "<<player.agi<<endl;
cout<<"mage "<<player.mage<<endl;
cout<<"money "<<player.mon<<endl;
cout<<"experience "<<player.exp<<endl;
}
else if(race == "d" || race == "D")
{
string dename;
cin>>dename;
system("cls")
cout<<"hmm "<< dename <<" if your sure\n";
player.lvl = 1;
player.hel = 100;
player.att = 5;
player.def = 5;
player.agi = 2;
player.mon = 0;
player.exp = 0;
cout<<"level "<<player.lvl<<endl;
cout<<"health "<<player.hel<<endl;
cout<<"attack "<<player.att<<endl;
cout<<"defense "<<player.def<<endl;
cout<<"agility "<<player.agi<<endl;
cout<<"money "<<player.mon<<endl;
cout<<"experience "<<player.exp<<endl;
}
attack()
{
//where my attack formual will be
}
};```
that there is about 260 lines of kik @\$\$ code sorry just had to say that

7. What are you wanting to calculate.

8. ...the attack...

9. Lilhawk, it has been bugging me for near the whole time you have been here. If you want help on something, and or need to reply. Please use whole words, and decent grammar.

Now onto your code, you have a TON of redunant code in there. You can probably cut 60-70% of the code out of there and make it a ton more readable.

EDIT: Also else does not get a ; at the end.

Another thing:
Code:
```#include <iostream>
#include <string.h>
#include <ctype.h>

using std::cin;
using std::cout;
using std::string;
using std::transform;

int main(int argc, char *argv[])
{
string test = "G";
transform (test.begin(),test.end(), test.begin(), tolower);
if(test == "g") {
cout<<"AMAZING";
} else {
cout<<"Nope";
}
cin.get();
}```
Check that out. Transform() is from ctype.h.

10. How?
And about my grammer,im just used to using m.s.n.talk.Yah so how could i limit down my code?

Oh and lol ive been here longer than you.XD

11. Originally Posted by lilhawk2892
...the attack...
The attack of what?

12. He wants to make a function for calculating an attack.

13. Hmm,it appears that maybe i should work on the rest of the code before going back to this so maybe i can give more imformation...
does anyone agree?

14. lilhawk joined the forum and started posting very general threads about C++ and stuff. Around the tenth one he actually tells us he is making a game and wants to shorten his code.

Of course, since general questions get general answers, and because he hasn't programmed for very long (most likely) he has not been able to take the advice of our members and apply it within the scope of his project.

15. You can work on more than one thing at a time. Some advice, if I may, think of what you can reuse and put those parts into a function. Then consider creating utility files for your functions, so that it isn't a wall of code crashing down upon us. It really does help with a lot of things. Oh yeah, comments are good things to use too... and not only for others.