-
Help with a method
I'm trying to create a method called beercanblitz that will double my attacks such as punch() which returns the value of 20. Also when I double my attacks with beercanblitz I lose 25 health. I want this attack beercanblitz to only be able to be used once. Any Ideas how I can do this??
Code:
class senior:public teacher{
public:
int headbutt();
int beercanblitz(punch()));
void setHealth();
senior();
~senior();
};
senior::senior(){
health = 100;
name = "SuperSeniors!";
}
senior::~senior(){
}
int senior::beercanblitz(senior.punch()){
return senior.punch()*2;
}
Code:
else if{(enemyAttack == 5){
f5.beercanblitz(f5.punch());
// f5.kick(f5.beercanblitz());
// f5.headbutt(f5.beercanblitz())
}
cout<<"Super Human Senior USES BEER CAN BLITZ!!"<<endl;
cout<<"Your Health: ";
f1.showHealth();
}
-
You can have a variable that says "usedBeerCanBlitz" and then use that as a flag to indicate if it's been used or not. Or a "beerCanBlitzBoost" which is set to 2 initially, then downrated to 1 when it's been used, and multiply the "punch" by the beerCanBlitzBoost.
Code:
int beercanblitz(punch()));
This won't compile. It should be a punch argument with the correct type (int?) - the call to beercanblitz could use the result of punch() as an input, but it's not declared this way.
--
Mats
-
so would I just return 2 for beercanblitz?
Code:
int senior::beercanblitz(int){
return 2;
}
How could I then multiply my attacks?
-
Make it "int punch" and then return "punch * 2" [or perhaps "force" is a better name than "punch"].
--
Mats
-
hmm..still not sure.. I've attached my source code for this game. I haven't split the code up so its very large. I'm practicing my inheritance from classes that why there are multiple classes instead of mapped. I'm just trying to get beercanblitz to work..and i'll be happy...haha
-
Code:
int senior::beercanblitz(int force){
return 2 * force;
}
And I expect the right call would be:
Code:
f1.reducehealth(f5.beercanblitz(f5.punch()));
or some such...
Of course, you still have to figure out how to reduce the "beercanblitz" to 1 when it's been used. I'll let you ponder that one.
--
Mats
-
-
So I could just decrement beercanblitz?
Code:
else if(enemyAttack == 5){
f1.reduceHealth(f5.beercanblitz(f5.punch()));
f1.reduceHealth(f5.beercanblitz(f5.kick()));
f1.reduceHealth(f5.beercanblitz(f5.headbutt()));
beercanblitz()--;
cout<<"Super Human Senior USES BEER CAN BLITZ!!"<<endl;
cout<<"Your Health: ";
f1.showHealth();
}
-
Ok so basically the beercanbliltz attack is supposed to double all the senior opponents attack power and can only be used once. I also want seniors health to decrement by 25 when he uses it. I changed my code to have a flag to know when it has been used but I don't know how to double the attacks persay..
Code:
class senior:public teacher{
private:
bool blitz;
public:
int headbutt();
int beercanblitz(int force);
bool didbeercanblitz();
void changebeercanblitz(bool);
void setHealth();
senior();
~senior();
};
senior::senior(){
health = 100;
blitz = false;
}
senior::~senior(){
}
bool senior::didbeercanblitz(){
return blitz;
}
void senior::changebeercanblitz(bool state){
blitz = state;
}
int senior::beercanblitz(int force){
return 2 * force;
}
int senior::headbutt(){
return 20;
}
void senior::setHealth(){
health = 100;
}
In Main....
Code:
/* Senior's Attack */
cout<<endl;
if(enemyAttack == 1){
if(attack == 5){
f1.reduceHealth(f1.block(flag * f5.punch()));
}
else{
f1.reduceHealth(f5.punch());
cout<<"Your Health: ";
f1.showHealth();
}
cout<<"Super Human Senior Punched You!!"<<endl;
cout<<"Your Health: ";
f1.showHealth();
}
else if(enemyAttack == 2){
if(attack == 5){
f1.reduceHealth(f1.block(flag * f5.kick()));
}
else{
f1.reduceHealth(f5.kick());
cout<<"Your Health: ";
f1.showHealth();
}
cout<<"Super Human Senior Kicked You!!"<<endl;
cout<<"Your Health: ";
f1.showHealth();
}
else if (enemyAttack == 3){
cout<<"Super Human Senior blocked the attack"<<endl;
}
else if(enemyAttack == 4){
if(attack == 5){
f1.reduceHealth(f1.block(f5.headbutt()));
cout<<"Your Health: ";
f1.showHealth();
}
else{
f1.reduceHealth(f5.headbutt());
}
cout<<"Super Human Senior Headbutts You!!"<<endl;
cout<<"Your Health: ";
f1.showHealth();
}
else if(enemyAttack == 5){
if (!f5.didbeercanblitz()){
f5.changebeercanblitz(true);
flag = 2;
}
else {
flag = 1;
}
/* f1.reduceHealth(f5.beercanblitz(f5.kick()));
f1.reduceHealth(f5.beercanblitz(f5.headbutt()));
cout<<"Super Human Senior USES BEER CAN BLITZ!!"<<endl;
cout<<"Your Health: ";
*/
f1.showHealth();
}
/*Check for Winner*/
if(f5.getHealth() <= 0 && f1.getHealth() > 0){
cout<<endl;
cout<<"You Killed the Super Human Senior!!"<<endl;
cout<<"Your Health: ";
f1.showHealth();
seniorOpponent--;
if(seniorOpponent > 0){
f5.setHealth();
}
}
}
-
I would have something like a message system or so:
Code:
class FightingUnit
{
public:
FightingUnit( )
{
units_list.push_back(this);
}
template<class T_TYPE>
struct Message
{
FightingUnit *source, *target;
std::string action_name;
int target_data, source_data;
enum ActionType
{
AT_OFFENSIVE, AT_DEFENSIVE, AT_SPECIAL
} action_type;
};
static void ProcessEvents( )
{
while(messages_list.size() > 0)
{
const Message msg = messages_list.front();
switch(msg.action_type)
{
case AT_OFFENSIVE:
source->InflictDamages(target, target_data);
break;
case AT_DEFENSIVE:
if(action_name == "block" || action_name == "unblock")
source->SetBlockStatus(action_name);
break;
case AT_SPECIAL:
source->ProcessSpecialMove(msg);
break;
}
}
}
protected:
static std::vector<FightingUnit*> units_list;
static std::vector<Message> messages_list;
int health;
std::string unit_name;
private:
};
class Teacher : public FightingUnit
{
/* ... */
};
class Senior : public Teacher
{
/* ... */
};
This should give you a general idea of what I'd do.
This is a tad sloppy (and untested) and so I think I will maybe try to take an hour or so and write something much more robust.
-
Wow looks like some awesome coding thanks for the help. The only thing is I haven't made it that far to templates and pointers. So I can't understand the code all that much. I'm still in the learning process...